ABAP - Rules for data definition
For the inline declaration of variables, there are special rules to watch out for. We want to give you a closer look in this article.
Table of contents
As we described in the last article, the definition of data types and variables has become a bit simpler and clearer. For simple assignments, this also works in all cases, especially if there is a returning parameter.
A returning parameter always returns a variable, structure or table and is uniquely defined. Therefore, you can save the result of the return directly in a new created variable.
" Create data for returning parameter
DATA(ld_erg) = lo_obj->get_data( ).
" Create with a function
DATA(ld_cnt) = LINES( lt_t001 ).
For explanation:
- The first example calls the get_data method of the object lo_obj, which returns a result as the returning parameter.
- In the second example, the number of rows of the table lt_t001 is determined and made available as a variable in the program.
Hint: A returning parameter of the REF TO DATA type is also clearly defined, which fits very well for generic data returns.
Methods
In addition to the returning parameters, export parameters of methods can also be defined at runtime in the program. If a method provides a large number of export parameters, they can be generated quickly at runtime when the method is called, which provides a lot of overview in your coding.
" Method call
lo_obj->get_parameter(
EXPORTING
id_key = ld_key
id_name = 'Text Example'
IMPORTING
et_data = DATA(lt_data)
et_address = DATA(lt_address)
et_num = DATA(lt_num)
es_header = DATA(ls_head)
ed_count = DATA(ld_cnt)
).
Normally in this case 5 variables would have to be created in the head of the method or the form, which will then save the values, but this will now be omitted.
Function module
For function modules this "fast" feature does not work. Returning parameters do not work with function modules, changing parameters also and exporting parameters must be defined in the calling program. Even for the parameters that have a clear type, the inline declaration does not work.
This has two reasons:
- The compiler would complain if there are not clearly defined data types in the interface
- RFC-able function modules do not necessarily have to be known in the system, so the data types that are used are not known
Trick 17
However, to ensure that the logic still works for BAPIs or function modules, you can also embed them in a method, especially if you then need that BAPI more often. Thus, an inline declaration of the variables is possible, especially since BAPIs usually have quite extensive return structures.
So you realize, even for this problem, there is a solution. But you should not use it in any case, only when it's worth and you use it more often.
Summary
So you have learned that there are additional special rules when using function modules and methods, but these are not so complicated and easy to implement.