SAP ABAP: Check if you’re in the production environment using a FM

There comes a time when I am writing a program that I don’t want executed in any of the production environments [ie: like a data load that I would only allow to run in the DEV or QA systems]. ¬†There’s an obvious way to check which system ID you’re running on, but a better approach is to execute the following code:

INITIALIZATION.
* do not allow this program to be executed in a production system
  CALL FUNCTION 'PRGN_CHECK_SYSTEM_PRODUCTIVE'
    EXCEPTIONS
      client_is_productive = 1
      OTHERS               = 2.
  IF sy-subrc NE 0.
*    throw an error message
  ENDIF.

The contents of the Function Module is straight forward: a system table T001 is checked to see the active client is of type ‘P’.

function prgn_check_system_productive.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"       EXCEPTIONS
*"              CLIENT_IS_PRODUCTIVE
*"----------------------------------------------------------------------
* Wenn der Mandant auf Produktiv ist, Exception raisen:
  select single * from t000 where mandt = sy-mandt.
  if t000-cccategory = 'P'. "" or t000-cccoractiv = '2'.
    message e522(s#) raising client_is_productive.
  endif.
endfunction.

Leave a Reply

*

captcha *