Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Calling and leaving program units →  Calling Processing Blocks →  Calling Procedures →  Method Calls →  Dynamic Method Call 

CALL METHOD

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


CALL METHOD dynamic_meth {
parameter_list 
                         | parameter_tables }.

Effect

This statement calls the method specified dynamically in dynamic_meth (Dynamic Invoke). Actual parameters are assigned to formal parameters of the method, either statically using parameter_list or dynamically using parameter_tables. The syntax of parameter_list is the same as that in the explicit parameter specification for the static method call.


Notes

  • In the dynamic method call, the parameters are not passed in parentheses. The syntax of the dynamic method call is like that of a function module call.
  • The statement CALL_METHOD should now only be used for the dynamic method call. It is unnecessary, and therefore obsolete, for the static method call.

Security Note

If the name of a program unit is specified dynamically when it is called, and this name is passed to a program from outside, the result is a serious security risk. Any names passed to a program from outside must be checked thoroughly before being used in calls. The system class CL_ABAP_DYN_PRG, for example, can be used to do this. See Dynamic Calls.

System Fields

The system field sy-subrc is set to 0 when a method is called. If a non-class-based exception is raised that was handled by the assignment of a value, sy-subrc is set to this value.


Example

Dynamic call of the static method GUI_DOWNLOAD of global class CL_GUI_FRONTEND_SERVICES for storing the content of an internal table in a file on the current presentation server. The names of the class and method are specified in the strings class and meth. The interface parameters are passed in the internal table ptab and return values are assigned to the exceptions of the method are assigned using table etab. Exceptions that are raised at the method call itself are handled in a TRY control structure with statement CATCH.

DATA: line     TYPE c LENGTH 80, 
      text_tab LIKE STANDARD TABLE OF line, 
      filename TYPE string, 
      filetype TYPE c LENGTH 10, 
      fleng    TYPE i. 

DATA: meth  TYPE string, 
      class TYPE string, 
      ptab TYPE abap_parmbind_tab, 
      etab TYPE abap_excpbind_tab. 

DATA: exc_ref TYPE REF TO cx_sy_dyn_call_error. 

class    = 'CL_GUI_FRONTEND_SERVICES'. 
meth     = 'GUI_DOWNLOAD'. 
filename = 'c:\temp\text.txt'. 
filetype = 'ASC'. 

ptab = VALUE #( ( name  = 'FILENAME' 
                  kind  = cl_abap_objectdescr=>exporting 
                  value = REF #( filename ) ) 
                ( name  = 'FILETYPE' 
                  kind  = cl_abap_objectdescr=>exporting 
                  value = REF #( filetype ) ) 
                ( name  = 'DATA_TAB' 
                  kind  = cl_abap_objectdescr=>changing 
                  value = REF #( text_tab ) ) 
                ( name  = 'FILELENGTH' 
                  kind  = cl_abap_objectdescr=>importing 
                  value = REF #( fleng ) ) ). 

etab = VALUE #( ( name = 'OTHERS' value = 4 ) ). 

TRY. 
    CALL METHOD (class)=>(meth) 
      PARAMETER-TABLE 
        ptab 
      EXCEPTION-TABLE 
        etab. 
    CASE sy-subrc. 
      WHEN 1. 
        ... 
      ... 
    ENDCASE. 
  CATCH cx_sy_dyn_call_error INTO exc_ref. 
    MESSAGE exc_ref->get_text( ) TYPE 'I'. 
ENDTRY. 

Exceptions

Handleable Exceptions

CX_SY_NO_HANDLER

CX_SY_DYN_CALL_EXCP_NOT_FOUND

  • Cause: Exception does not exist
    Runtime error: DYN_CALL_METH_EXCP_NOT_FOUND

CX_SY_DYN_CALL_ILLEGAL_CLASS

  • Cause: Specified class does not exist
    Runtime error: DYN_CALL_METH_CLASS_NOT_FOUND

CX_SY_DYN_CALL_ILLEGAL_METHOD

  • Cause: Method cannot be accessed.
    Runtime error: CALL_METHOD_NOT_ACCESSIBLE
  • Cause: The called method is not implemented.
    Runtime error: CALL_METHOD_NOT_IMPLEMENTED
  • Cause: Calls the static constructor
    Runtime error: DYN_CALL_METH_CLASSCONSTRUCTOR
  • Cause: Calls the instance constructor
    Runtime error: DYN_CALL_METH_CONSTRUCTOR
  • Cause: Method does not exist
    Runtime error: DYN_CALL_METH_NOT_FOUND
  • Cause: Method is not static
    Runtime error: DYN_CALL_METH_NO_CLASS_METHOD
  • Cause: Calls a non-visible method
    Runtime error: DYN_CALL_METH_PRIVATE
  • Cause: Calls a non-visible method
    Runtime error: DYN_CALL_METH_PROTECTED

CX_SY_DYN_CALL_ILLEGAL_TYPE

  • Cause: Type conflict during method call.
    Runtime error: CALL_METHOD_CONFLICT_GEN_TYPE
  • Cause: Type conflict during method call.
    Runtime error: CALL_METHOD_CONFLICT_TAB_TYPE
  • Cause: Type conflict during method call.
    Runtime error: CALL_METHOD_CONFLICT_TYPE
  • Cause: Incorrect parameter type
    Runtime error: DYN_CALL_METH_PARAM_KIND
  • Cause: Actual parameter cannot be filled
    Runtime error: DYN_CALL_METH_PARAM_LITL_MOVE
  • Cause: Incorrect table type for a parameter
    Runtime error: DYN_CALL_METH_PARAM_TAB_TYPE
  • Cause: Incorrect parameter type
    Runtime error: DYN_CALL_METH_PARAM_TYPE

CX_SY_DYN_CALL_PARAM_MISSING

  • Cause: Missing actual parameter
    Runtime error: DYN_CALL_METH_PARAM_MISSING
  • Cause: Parameter reference is empty
    Runtime error: DYN_CALL_METH_PARREF_INITIAL

CX_SY_DYN_CALL_PARAM_NOT_FOUND

  • Cause: Incorrect parameter name
    Runtime error: DYN_CALL_METH_PARAM_NOT_FOUND

CX_SY_REF_IS_INITIAL

  • Cause: Reference variable is initial
    Runtime error: DYN_CALL_METH_REF_IS_INITIAL

Non-Handleable Exceptions

  • Cause: Invalid parameters for dynamic method call. Relevant for instance constructors when instantiated dynamically.
    Runtime error: CALL_METHOD_PARMS_ILLEGAL

Continue

CALL METHOD - dynamic_meth

CALL METHOD - parameter_tables