16.5    Dynamic Procedure Calls

Procedures can be called dynamically using dynamic tokens. For example, if you can determine the subroutine that needs to be called only at runtime, then you can pass the subroutine name using a dynamic token. When calling an external subroutine, the program name can also be passed dynamically as follows:

PERFORM (l_name) IN PROGRAM (l_program) IF FOUND. 

When calling a subroutine dynamically, the system will generate a runtime error if the specified subroutine or program is not found. To avoid the runtime error, you should use the IF FOUND addition.

Methods can also be called dynamically by passing the class name and the method name with a dynamic token. In fact, the parameters of the method can also be supplied dynamically using the PARAMETER-TABLE addition as follows:

CALL METHOD class_name=>method_name PARAMETER-TABLE para. 

In the preceding statement, we’re passing the parameters to the method dynamically using the PARAMETER-TABLE addition. Here, an internal table of type ABAP_PARMBIND_TAB is expected. The line type of ABAP_PARMBIND_TAB consists of three fields: NAME, KIND, and VALUE. The NAME field should contain the formal parameter name.

The KIND field should be filled with the parameter type (whether exporting, importing, returning, etc., from a caller’s point of view). The VALUE field should contain the reference of the actual parameter.

We can use the constants of the cl_abap_objectdescr class to fill the KIND field. As with PARAMETER-TABLE, non-class-based exceptions can be handled using the EXCEPTION-TABLE addition.

As with methods, the parameters for a function module can also be supplied dynamically using PARAMETER-TABLE. The only difference is that PARAMETER-TABLE when used for the function module is of type ABAP_FUNC_PARMBIND_TAB.

Listing 16.27 shows example code using a parameter table to pass parameters dynamically.

DATA: fm_name TYPE STRING,
filename TYPE STRING,
filetype TYPE C LENGTH 80,
text_tab LIKE STANDARD TABLE OF LINE,
fleng TYPE I,
ptab TYPE abap_func_parmbind_tab,
ptab_line LIKE LINE OF ptab,
etab TYPE abap_func_excpbind_tab,
etab_line LIKE LINE OF etab.

fm_name = 'GUI_DOWNLOAD'.
filename = 'C:\text.txt'.
filetype = 'ASC'.

ptab_line-NAME = 'FILENAME'.
ptab_line-KIND = abap_func_exporting.
GET REFERENCE OF filename INTO ptab_line-VALUE.
INSERT ptab_line INTO TABLE ptab.

ptab_line-NAME = 'FILETYPE'.
ptab_line-KIND = abap_func_exporting.
GET REFERENCE OF filetype INTO ptab_line-VALUE.
INSERT ptab_line INTO TABLE ptab.

ptab_line-NAME = 'DATA_TAB'.
ptab_line-KIND = abap_func_tables.
GET REFERENCE OF text_tab INTO ptab_line-VALUE.
INSERT ptab_line INTO TABLE ptab.

ptab_line-NAME = 'FILELENGTH'.
ptab_line-KIND = abap_func_importing.
GET REFERENCE OF fleng INTO ptab_line-VALUE.
INSERT ptab_line INTO TABLE ptab.

...

etab_line-NAME = 'OTHERS'.
etab_line-VALUE = 10.
INSERT etab_line INTO TABLE etab.

CALL FUNCTION fm_name
PARAMETER-TABLE
ptab
EXCEPTION-TABLE
etab.

Listing 16.27    Parameter Table

In Listing 16.27, we defined the parameter table PTAB as type ABAP_FUNC_PARMBIND_TAB. We’re filing the parameter of the function module GUI_DOWNLOAD dynamically in the internal table PTAB.

The function module is called dynamically, and the parameter table is passed using the PARAMETER-TABLE addition. We’re also passing the exceptions table ETAB, which is defined as type ABAP_FUNC_EXCPBIND_TAB.

This section looked at the process of calling procedures dynamically. In the next section, we look at a more advanced feature of ABAP: dynamic program generation.