Skip to Content
Author's profile photo Guryanov Alexandr

Dynamically request values from user

Today I’d like to introduce a tool to request a random number of values from user. For example, you’d like to ask and then store a template of some structure to use this pattern further.

I used it to determine sample values for structure of an HR infotype to create a flexible data download program.

Initial screen of this report looks very ascetic:


                    The bottom button is used to request data from user. If you push it you’ll get:


Here you can see a list of P0009 structure fields with their names described at ABAP repository. So any changes of this structure will change this screen with requesting values.

Probably, you’d like to know how many instructions I was to code. Well, lets see it:

The first part is to describe my requesting tool:

DATA: lr_request_values TYPE REF TO zcl_bas_dyn_request_values.
= zcl_bas_dyn_request_values=>factory( ).

The second is to get my structure filled with values by user:

DATA: default_p0009 TYPE p0009,
TYPE REF TO cl_abap_structdescr,
TYPE REF TO cl_abap_elemdescr.
lr_descr ?= cl_abap_structdescr=>describe_by_data( default_p0009 ).
    CHECK lr_descr IS BOUND.
LOOP AT lr_descr->components ASSIGNING <component>.

      ASSIGN COMPONENT <component>name OF STRUCTURE default_p0009 TO <field>.
CHECK sysubrc = 0.
   lr_elemdescr ?= cl_abap_elemdescr
=>describe_by_data( <field> ).
= lr_elemdescr->get_ddic_field( ).
IF ls_fieldscrtext_m IS INITIAL.
scrtext_m = ls_fieldscrtext_s.
= <field>.
= ‘P0009-‘ && <component>name.
IF ls_fieldscrtext_m IS INITIAL.
scrtext_m = lv_paratype.
= ‘P’
= lv_paratype
= ls_fieldscrtext_m
= lv_defaultc
= <field> ).

And at last request user:

    lr_request_values->request_values( ).

When the last call ends my structure default_p0009 is filled with values user just filled. As you may count it’s just about 20 lines of code to ask user for values of any flat structure from ABAP repository.

And now my code to get this functionality:

TXT files attached for class ZCL_BAS_DYN_REQUEST_VALUES and for class ZCL_BAS_DYN_PARAM.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      Thanks for sharing! I've noticed P0009 is hardcoded, so how would one make this fully dynamic? Or is it not feasible? Also - any limitations on the structure definition? I guess at least it should be "flat", but is there anything else to consider?

      Author's profile photo Guryanov Alexandr
      Guryanov Alexandr
      Blog Post Author

      Yes, P0009 is hardcode here but you can replace it with any ather flat structure that described in ABAP repository. Furthermore you can use a structure that doesn't exist in the repository but then you're to carry out fields types as soon as they should reffer to a data element or to a base ABAP type.

      Author's profile photo Nahuel Elías Romanello
      Nahuel Elías Romanello

      Hi, I think this post is really useful... I'm very grateful.

      But one thing that call me my attention... is... when you define the attached class, you define the code of the methods inside the definition, I think the right thing is to put the code of the methods inside the implementation clause, and in the definition clause only define them only with their parameters.

      What do you think?

      Thanks in advance!

      Author's profile photo Guryanov Alexandr
      Guryanov Alexandr
      Blog Post Author

      Hi Nahuel,

      Soory for such long time for answer. Those classes are dictionary classes. They exist without any reports. So my texts are just copy-paste of code public-protexted-private sections and then code of each methods. Of course you can't copy-paste all of these codes into your report. Sorry if that's not very comfortable.

      Best regards,