Sometimes we do require to know whether a program was scheduled as a background job via its tcode or from se38/sa38 (via menu->execute as background):
Whenever a program is executed as a background sy-tcode is blank and sy-batch is set to 'X',due to which we cannot be sure.
If a program has two Tcodes from which it can be invoked but depending on the tcode the displayed list should be modified then to achieve the same functionality in background we need to know the TCODE from which it has been triggered.
As the program has multiple tcodes and also can be executed via se38/sa38 hence we cannot rely on TSTC table also.
CODE:
*&---------------------------------------------------------------------*
*& Report ZSEC_TEST_DISPLAY
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZSEC_TEST_DISPLAY.
****data declaration****
Parameters:P1 type sy-tcode NO-DISPLAY,p2 typec. "P2 is a dummy parameter only used as P1 is not displayed.
DATA:VAR type sy-tcode,CHECKtypeC. "Global variable to hold the tcode
INITIALIZATION.
VAR = SY-TCODE.
CHECK = 'X'.
IF VAR ISINITIAL.
GETPARAMETERID'ABC'FIELD VAR.
P1 = VAR.
SETPARAMETERID'ABC'FIELD VAR.
ELSE.
SETPARAMETERID'ABC'FIELD VAR.
P1 = VAR.
ENDIF.
START-OF-SELECTION.
IfCHECKisnotINITIAL. "Check is set is initialization has occured"
if p1 isnotINITIAL.
Write:/ 'This program was invoked from:', P1.
else.
P1 = 'SM36'.
Write:/ 'This program was invoked from:', P1.
endif.
else.
P1 = 'SM36'.
Write:/ 'This program was invoked from:', P1.
endif.
***********************************
Prerquisites:
Create Parameter ID 'ABC' and maintain the same in TPARA
Result:It will write in spool the tcode from which it was invoked