Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Find variable name in runtime

adrianos_campanhola
Participant
0 Likes
2,129

Hello,

I'll explain this question in 2 parts , the general idea and it's intents

I've been trying to do some dynamic coding and I still didn't manage a way to find out a variable name at runtime.

I wanted to pass the variable into a method/subroutine and find out what is the variable name along with some other properties:

  o_dynsel->add_select_option( s_matnr[] ).

Here's the idea:

While using the dynamic selection FM's (FREE_SELECTION_INIT), I wanted to map the select-options I have on screen to their corresponding fields inside the function. This can be achieved If:

  1. I know the name of the select option variable and its table-field. -> So I can map it and ensure unicity.
  2. I know it's contents -> to map from/to the dynamic selection screen/

Plus, I wanted to do this as generic as possible.

I could do this using a text with the variable name:

  o_dynsel->add_select_option( i_selop = 'S_MATNR'

                               i_ref_field = 'MARA-MATNR' ).

But I simply don't like it that way(plus, I can always get spelling errors! )

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,000

You will only have access to actual parameter, not that format parameter.

If the variable name is not being passed, you can read the call stack , read the code where it was called, and then do string manipulation to get the variable name.

Look at this snippet where method is able to return the names select-options being passed.

  1. *----------------------------------------------------------------------*
  2. CLASS main DEFINITION.
  3.   PUBLIC SECTION.
  4.     METHODS:
  5.     find_name
  6.       IMPORTING selopt TYPE ANY TABLE
  7.       RETURNING value(result) TYPE string.
  8. ENDCLASS.                    "main DEFINITION
  9. *----------------------------------------------------------------------*
  10. CLASS main IMPLEMENTATION.
  11.   METHOD find_name.
  12.     TYPES:
  13.       BEGIN OF formatted_entry,
  14.               stack_depth TYPE tpda_stack_level,
  15.               kind TYPE tpda_event_type,
  16.               progname TYPE tpda_program,
  17.               includename TYPE tpda_include,
  18.               line TYPE tpda_sc_line,
  19.               event TYPE tpda_event,
  20.            END OF formatted_entry,
  21.       formatted_entry_stack TYPE STANDARD TABLE OF formatted_entry WITH DEFAULT KEY .
  22.     DATA lt_stack TYPE formatted_entry_stack.
  23.     lt_stack cl_abap_get_call_stack=>format_call_stack_with_struct(
  24.                   cl_abap_get_call_stack=>get_call_stack( )
  25.                 ).
  26.     DATA ls_stack TYPE formatted_entry.
  27.     DATA lt_report TYPE TABLE OF string.
  28.     DATA lv_report TYPE string.
  29.     READ TABLE lt_stack INTO ls_stack INDEX 2.
  30.     IF sy-subrc EQ 0.
  31.       READ REPORT ls_stack-includename INTO lt_report.
  32.       IF sy-subrc EQ 0.
  33.         READ TABLE lt_report INTO lv_report INDEX ls_stack-line.
  34.         IF sy-subrc EQ 0.
  35.           TRANSLATE lv_report TO UPPER CASE.
  36.           FIND REGEX '->FIND_NAME\W*(\w*)\W*' IN lv_report SUBMATCHES result.
  37.         ENDIF.
  38.       ENDIF.
  39.     ENDIF.
  40.   ENDMETHOD.                    "find_name
  41. ENDCLASS.                    "main IMPLEMENTATION
  42. TABLES mara.
  43. SELECT-OPTIONS:
  44.   s_matnr FOR mara-matnr,
  45.   s_mtart FOR mara-mtart.
  46. START-OF-SELECTION.
  47.   DATA lv_name TYPE string.
  48.   DATA lr_main TYPE REF TO main.
  49.   CREATE OBJECT lr_main.
  50.   lv_name = lr_main->find_name( s_matnr[] ).
  51.   WRITE / lv_name.
  52.   lv_name = lr_main->find_name( s_mtart[] ).
  53.   WRITE / lv_name.

/.

1 REPLY 1
Read only

Former Member
0 Likes
1,001

You will only have access to actual parameter, not that format parameter.

If the variable name is not being passed, you can read the call stack , read the code where it was called, and then do string manipulation to get the variable name.

Look at this snippet where method is able to return the names select-options being passed.

  1. *----------------------------------------------------------------------*
  2. CLASS main DEFINITION.
  3.   PUBLIC SECTION.
  4.     METHODS:
  5.     find_name
  6.       IMPORTING selopt TYPE ANY TABLE
  7.       RETURNING value(result) TYPE string.
  8. ENDCLASS.                    "main DEFINITION
  9. *----------------------------------------------------------------------*
  10. CLASS main IMPLEMENTATION.
  11.   METHOD find_name.
  12.     TYPES:
  13.       BEGIN OF formatted_entry,
  14.               stack_depth TYPE tpda_stack_level,
  15.               kind TYPE tpda_event_type,
  16.               progname TYPE tpda_program,
  17.               includename TYPE tpda_include,
  18.               line TYPE tpda_sc_line,
  19.               event TYPE tpda_event,
  20.            END OF formatted_entry,
  21.       formatted_entry_stack TYPE STANDARD TABLE OF formatted_entry WITH DEFAULT KEY .
  22.     DATA lt_stack TYPE formatted_entry_stack.
  23.     lt_stack cl_abap_get_call_stack=>format_call_stack_with_struct(
  24.                   cl_abap_get_call_stack=>get_call_stack( )
  25.                 ).
  26.     DATA ls_stack TYPE formatted_entry.
  27.     DATA lt_report TYPE TABLE OF string.
  28.     DATA lv_report TYPE string.
  29.     READ TABLE lt_stack INTO ls_stack INDEX 2.
  30.     IF sy-subrc EQ 0.
  31.       READ REPORT ls_stack-includename INTO lt_report.
  32.       IF sy-subrc EQ 0.
  33.         READ TABLE lt_report INTO lv_report INDEX ls_stack-line.
  34.         IF sy-subrc EQ 0.
  35.           TRANSLATE lv_report TO UPPER CASE.
  36.           FIND REGEX '->FIND_NAME\W*(\w*)\W*' IN lv_report SUBMATCHES result.
  37.         ENDIF.
  38.       ENDIF.
  39.     ENDIF.
  40.   ENDMETHOD.                    "find_name
  41. ENDCLASS.                    "main IMPLEMENTATION
  42. TABLES mara.
  43. SELECT-OPTIONS:
  44.   s_matnr FOR mara-matnr,
  45.   s_mtart FOR mara-mtart.
  46. START-OF-SELECTION.
  47.   DATA lv_name TYPE string.
  48.   DATA lr_main TYPE REF TO main.
  49.   CREATE OBJECT lr_main.
  50.   lv_name = lr_main->find_name( s_matnr[] ).
  51.   WRITE / lv_name.
  52.   lv_name = lr_main->find_name( s_mtart[] ).
  53.   WRITE / lv_name.

/.