判断ABAP代码是否处于update模式下运行的工具类

The class cl_system_transaction_state contains several useful utility methods:

  • get_in_update_task: return the flag whether current code is running with normal work process or in update work process
  • get_on_commit: return flag whether current code is called because of a previous registration via PERFORM ON COMMIT and triggered by COMMIT WORK
  • get_sap_luw_key: return current LUW ID

I just use a very simple report to test them. First I call the FM ZSQF in a normal way, then call it via update task, then register it with PERFORM ON COMMIT and trigger it via COMMIT WORK.

WRITE: / 'Direct call ZSQF begin...'.

DATA(lv_luw_key) = cl_system_transaction_state=>get_sap_luw_key( ).

WRITE:/ 'LUW key in main program:', lv_luw_key.

CALL FUNCTION 'ZSQF'.

WRITE: / 'Direct call ZSQF end...'.

CALL FUNCTION 'ZSQF' IN UPDATE TASK.

PERFORM call_fm ON COMMIT.

COMMIT WORK AND WAIT.

lv_luw_key = cl_system_transaction_state=>get_sap_luw_key( ).

WRITE:/ 'LUW key in main program after COMMIT WORK:', lv_luw_key.

FORM call_fm.

WRITE:/ 'ZSQF is called on COMMIT begin...'.

CALL FUNCTION 'ZSQF'.

WRITE:/ 'ZSQF is called on COMMIT end...'.

ENDFORM.

In the function module ZSQF, I just print out the three flags.

DATA(lv_in_update) = cl_system_transaction_state=>get_in_update_task( ).

DATA(lv_on_commit) = cl_system_transaction_state=>get_on_commit( ).

DATA(lv_luw_key) = cl_system_transaction_state=>get_sap_luw_key( ).

WRITE: / 'Am I in update task? ' , lv_in_update.

WRITE: / 'Am I triggered via PERFORM ON COMMIT?', lv_on_commit.

WRITE: / 'Current LUW Key' , lv_luw_key.

The execution result shows the fact that the normal FM call, the FM registered to COMMIT WORK and the update task all run within the same LUW, and also proves the explanation of COMMIT WORK in ABAP help: “The COMMIT WORK statement closes the current SAP LUW and opens a new one”.

The WRITE keyword executed in update task will not generate any output in SE38 list, and apart from switching on “update debugging” and check the three flags in debugger, there is also another way to log the content of the variable like lv_luw_key:
Just create a new checkpoint group via tcode SAAB, specify option “Log” for Logpoints and maximum validity period.

Then append the following code in the FM implementation:

IF lv_in_update = 1.
   LOG-POINT ID ZUPDATELOG SUBKEY 'Current LUW KEY' FIELDS lv_luw_key.
ENDIF.

Now after report execution, go to tcode SAAB, click Log tab, and we can find the content of lv_luw_key which is logged by the above ABAP code LOG-POINT ID ZUPDATELOG SUBKEY ‘Current LUW KEY’ FIELDS lv_luw_key.

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

阅读 81

推荐阅读