I recently needed to call a method dynamically where the name was stored in a table. The call itself is pretty easy. Define a variable to store the name and just use the value in the call method.

You cant simply add a string lo_objekt->(lv_string_method) cause the compiler does not know what its gonna be at runtime and prevents it.

However a CALL METHOD takes a char format anyway and thus is able to work with it.

DATALV_METHOD TYPE STRING,
      LO_OBJECT TYPE REF TO YCL_SYSCPY_AUTOMATION.

CREATE OBJECT LO_OBJECT.

LV_METHOD 'ENTF_SDRUCK_NACHSCHUB'.

CALL METHOD LO_OBJECT->(LV_METHOD).

If you want to pass parameters dynamically, this is also possible by filling a local parameter table.

DATALV_METHOD TYPE STRING,
      LO_OBJECT TYPE REF TO YCL_SYSCPY_AUTOMATION,
      LD_STRING TYPE STRING,
      LS_PARAM TYPE ABAP_PARMBIND,
      LT_PARAMS TYPE ABAP_PARMBIND_TAB.

CREATE OBJECT LO_OBJECT.

LV_METHOD 'ENTF_SDRUCK_NACHSCHUB'.

LS_PARAM-NAME 'IV_PATTERN'.
LS_PARAM-KIND 'E'"Exporting
LD_STRING '*'.
GET REFERENCE OF LD_STRING INTO LS_PARAM-VALUE.
INSERT LS_PARAM INTO TABLE LT_PARAMS.

CALL METHOD LO_OBJECT->(LV_METHOD)
  PARAMETER-TABLE
  LT_PARAMS.


See that the parambind takes references to data as value. So you have to get that reference from any given variable.

To consider are the CX_SY_DYN_CALL_ILLEGAL_METHOD and CX_SY_DYN_CALL_PARAM_NOT_FOUND Exceptions when the Methodname or Params do not match.


So you might end up like this:

DATALV_METHOD TYPE STRING,
      LO_OBJECT TYPE REF TO YCL_SYSCPY_AUTOMATION,
      LD_STRING TYPE STRING,
      LS_PARAM TYPE ABAP_PARMBIND,
      LT_PARAMS TYPE ABAP_PARMBIND_TAB,
      LO_EX_METH TYPE REF TO CX_SY_DYN_CALL_ILLEGAL_METHOD,
      LO_EX_PARAM TYPE REF TO CX_SY_DYN_CALL_PARAM_NOT_FOUND.

CREATE OBJECT LO_OBJECT.

LV_METHOD 'YIF_SYSCPY_AUTO_METHODS~ENTF_SDRUCK_NACHSCHUB'.

LS_PARAM-NAME 'IV_PATTERN'.
LS_PARAM-KIND 'E'"Exporting
LD_STRING '*'.
GET REFERENCE OF LD_STRING INTO LS_PARAM-VALUE.
INSERT LS_PARAM INTO TABLE LT_PARAMS.

TRY.
    CALL METHOD LO_OBJECT->(LV_METHOD)
      PARAMETER-TABLE
      LT_PARAMS.

  CATCH CX_SY_DYN_CALL_ILLEGAL_METHOD  INTO LO_EX_METH.
    MESSAGE E000(YIWITH TEXT-002
                          LO_EX_METH->METHODNAME .

  CATCH CX_SY_DYN_CALL_PARAM_NOT_FOUND INTO LO_EX_PARAM.
    MESSAGE E000(YIWITH TEXT-001
                          LO_EX_PARAM->PARAMETER
                          LO_EX_PARAM->METHODNAME .
ENDTRY.



Serkan AKKAVAK
Computer Engineer
SAP Department Deputy Manager
Contact : serkurumsal@yandex.com