There are several ways to determine if a system is a test or productive system. I found that the following code is the most robust way.
Table T000 is the clients table, which holds a field CCCATEGORY - "Client control: Role of client (production, test,...)". If the value for your client (MANDT) is set to P, the system is assumed to be a Production system. Test systems should never have this setting as P.
data: lv_cccategory type t000-cccategory,
gv_production_system type Boolean.
* Get system category
select single cccategory from t000
into lv_cccategory where mandt = sy-mandt.
if sy-subrc = 0 and lv_cccategory = 'P'.
gv_production_system = 'X'.
else.
clear gv_production_system.
endif.
if gv_production_system = space.
write 'This is a NON-productive system'.
else.
write 'This is a PRODUCTION system'.
endif.
This can also be applied in a more object-oriented manner, as a local class (which can also be made available as a global class or a method on a utility class).
class lcl_system definition.
PUBLIC SECTION.
class-methods:
is_production_system returning
value(is_production) type boolean.
endclass.
class lcl_system implementation.
method is_production_system.
data: lv_cccategory type t000-cccategory.
* Get system category
select single cccategory from t000
into lv_cccategory
where mandt = sy-mandt.
if sy-subrc = 0 and lv_cccategory = 'P'.
is_production = abap_true.
else.
clear is_production.
endif.
endmethod.
endclass.
The class and method can then be used/called like this:
if lcl_system=>is_production_system( ) = abap_false. write 'This is a NON-productive system'. else. write 'This is a PRODUCTION system'. endif.
