This is a test message to test the length of the message box.
Login
ABAP Quick dynamic method calls
Created by Software-Heroes

ABAP Quick - Dynamic method calls

5813

Not always is a direct method call, that brings you to your goal. Maybe you want to bring some dynamism in your programming, in addition to our today's tip.

In some situations, you may want to deviate from a predefined path and try a more dynamic solution.

For example, you need to do several calculations that always expect a set of parameters and return a result. The number of calculation steps and the sequence can change constantly or have certain preconditions. To achieve this you need to rely on dynamic programming.

 

Defined interface

The easiest way to do this is with a predefined interface. If all parameters are already specified and no longer change, a dynamic call within a class can be easily implemented.

To do this, we create an instance of our class and call the method, which is passed as text in the variable p_meth. If we put the whole thing in parentheses, then the dynamic call is already done. In this way, you can also call static methods and also specify the class dynamically.


" Variable definition
DATA(lo_class) = NEW zcl_test_class( ).

" Dynamic call
CALL METHOD lo_class->(p_meth)
  EXPORTING
    ed_num   = 3
  RECEIVING
    rd_value = DATA(ld_value).

 

Undefined interface

In most cases, you probably do not have the same method interface, but there are several parameters, different types or missing altogether.

In this case, there is the call with a parameter table. Here, the parameters are defined in a table and supplied with references from local variables, which give the values to the method and also get the values back over them.


" Variable definition
DATA: 
  lt_param TYPE abap_parmbind_tab,
  ld_value TYPE string,
  ld_num   TYPE i VALUE 3.

" Fill the table
INSERT VALUE #( name = 'ED_NUM' kind = cl_abap_objectdescr=>exporting value = REF #( ld_num ) ) INTO TABLE lt_param.
INSERT VALUE #( name = 'RD_VALUE' kind = cl_abap_objectdescr=>receiving value = REF #( ld_value ) ) INTO TABLE lt_param.

" Dynamic call
CALL METHOD lo_class->(p_meth)
  PARAMETER-TABLE lt_param.

 

In this case, you must first fill a parameter table that contains the references to the variables and the interface definition, and then finally call the method.

As you can see, we also used the new commands VALUE and REF. We have already explained Value in an earlier article and Ref generates and returns a data reference from the specified object.

 

Conclusion

You see, dynamic programming is also not rocket science and helps you to implement solutions with simple means and non-redundant elements. If you want to know more about the topic and the Call Method, just read the official documentation from SAP.

PS: In our article we have already pointed out that the use of Call Method is obsolete, but this does not apply to dynamic calls.

 

Source:
SAP Documentation Call Method


Included topics:
QuickDynamic
Comments (0)



And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


ABAP Quick - RFC Error handling

Category - ABAP

How do you actually handle errors when communicating via a destination in the function module? More about this in our tip.

03/05/2024

ABAP Quick - CLEAR right

Category - ABAP

Delete correctly? In this article we want to take a look at when it makes sense to delete and how you can do it effectively.

05/12/2023

ABAP Quick - Performance chained statements

Category - ABAP

Let's take a look at the performance when creating chained statements with DATA and FIELD-SYMBOL. Which variant will be ahead in terms of performance?

04/28/2023

ABAP - ALV still relevant in 2022?

Category - ABAP

Today the joking question, do we still need reports that generate ALV outputs in 2022? In this article, we want to look into this question.

07/01/2022

ABAP in Change

Category - ABAP

The programming language ABAP has been changing for years and is being modernized in various concepts. In this article, we'll look at it in detail.

06/24/2022