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.
DATA: LV_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. DATA: LV_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:
DATA: LV_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.
Social Plugin