Skip to Content

I was in the need to make a dynamic call from within an ABAP class method to a functional method, which isn’t known at compile time. The class can be subtyped, and from anywhere down the class hierarchy, this generic method should return the value of any functional method.

The short-cut statement would be great to use ..l like this:

   ev_value = me->(lv_dynamic_method_name)( ).

but the ABAP compiler only allows to use dynamic calls with the full statement

   CALL METHOD (lv_dynamic_method_name) RECEIVING rv_value = ev_value.

But in difference to the first short-cut solution, you have to know the name of the receiving parameter (likewise with a naming convention) or retrieve it somehow. As I disliked to create a development rule to name all returning parameter RV_VALUE, I was bound to use the runtime type description.

And here is the template to make such a dynamic call, without knowing the actual parameter name of the receiving element.

method GET_RECEIVING_PARAMETER.

    “Get the class description of myself
    DATA: lo_typeDescription                       TYPE REF TO CL_ABAP_TYPEDESCR.
    lo_typeDescription = CL_ABAP_TYPEDESCR=>DESCRIBE_BY_OBJECT_REF( me ).

    CHECK lo_typeDescription->type_kind = CL_ABAP_TYPEDESCR=>typekind_class.

           “Only ABAP OO Classes are allowed

    DATA: lo_classDescription                       TYPE REF TO CL_ABAP_CLASSDESCR.
    lo_classDescription ?= lo_typeDescription.

    FIELD-SYMBOLS: <ls_method_definition>           TYPE abap_methdescr.
    READ TABLE lo_classDescription->methods ASSIGNING <ls_method_definition>

                                            WITH KEY name = iv_methodName.
    CHECK sysubrc = 0.       “Yes, we really need the method to be

                              “existing to deliver the receiving type

    “Retrieve the name of the receiving parameter
    FIELD-SYMBOLS: <ls_receiving_parameter>         TYPE LINE OF abap_parmdescr_tab.
    READ TABLE <ls_method_definition>parameters ASSIGNING <ls_receiving_parameter>
                                                 WITH KEY parm_kind = CL_ABAP_CLASSDESCR=>RECEIVING.
    CHECK sysubrc = 0.

    ls_receiving_parametername  = <ls_receiving_parameter>name.
    ls_receiving_parameterkind  = <ls_receiving_parameter>parm_kind.

    “To-Be assigned by caller:  structure field  VALUE  filled with a type ref to data,
    “by using the statements:
    ”  GET REFERENCE OF <your-variable-name> INTO ls_receiving_parameter-value.
    ”  APPEND ls_receiving_parameter TO lt_parameter_table.
    ”  CALL METHOD (lv_method_name) PARAMETER TABLE lt_parameter_table.
   
    ”  After that call you can find the result in the variable, that you’ve used with
    ”  the GET REFERENCE statement.

endmethod.

And the dynamic call is simply something like this:

DATA: lt_parameter_table             TYPE ABAP_PARMBIND_TAB,
      ls_receiving_parameter         TYPE LINE OF ABAP_PARMBIND_TAB.
      ls_receiving_parameter = me->get_receiving_parameter( lv_methodname ).

IF ls_receiving_parameter IS INITIAL.
   “We have a problem, so we leave the returned value blank now
   EXIT.
ENDIF.

GET REFERENCE OF ev_value INTO ls_receiving_parametervalue.
INSERT ls_receiving_parameter INTO TABLE lt_parameter_table.


CALL METHOD (lv_methodname) PARAMETERTABLE lt_parameter_table.

Take care

   Florin Wach

   Systems-Integration

   SAP Business Workflow Senior-Expert

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply