Thursday, May 24, 2012

Get Current Function Name


Dynamic programming is essential for keeping code clean, tidy and reusable. For this purpose we always need to get information about objects, types, elements (whatever) at runtime. For example, you have to log some kind of event and you need to include the report name which triggered the related event. Basically ABAP developers check sy fields in the first place. In this situation we can get the solution by using sy-repid however when you need the current function name at runtime that variable gives the function group program name which won't work for us. For finding function name at runtime, you can use the code below.




  DATA lt_callstack TYPE sys_callst ,
         ls_callstack LIKE LINE OF lt_callstack.

*** Get Stack
  CALL FUNCTION 'SYSTEM_CALLSTACK'
    EXPORTING
      max_level    0
    IMPORTING
      et_callstack lt_callstack.

  LOOP AT lt_callstack INTO ls_callstack
      WHERE eventtype 'FUNC'" Get Last FUNC Call
    EXIT.
  ENDLOOP.

*** Set Function Name
  e_function_name ls_callstack-eventname.

No comments:

Post a Comment