What is the best-practice to replace that TABLES statement in OOABAP? - abap

In the following code:
TABLES: lqua.
CLASS TEST DEFINITION.
PRIVATE SECTION.
TYPES: BEGIN OF tt_data,
lgpla TYPE lqua-lgpla,
matnr TYPE lqua-matnr,
END OF tt_data.
ENDCLASS.
How could I get rid of the TABLES statement? As far as I understand, it is best-practice to avoid the TABLES statement, and it is forbidden in classes. When I omit it, the definition of tt_data throws a syntax error because lqua-lgpla is unknown.

This does compile without any error.
REPORT zzpj_so.
CLASS test DEFINITION.
PRIVATE SECTION.
TYPES: BEGIN OF tt_data,
lgpla TYPE lqua-lgpla,
matnr TYPE lqua-matnr,
END OF tt_data.
ENDCLASS.
What does not is for example this piece of code.
REPORT zzpj_so.
CLASS test DEFINITION.
PRIVATE SECTION.
TYPES: BEGIN OF tt_data,
lgpla LIKE lqua-lgpla,
matnr LIKE lqua-matnr,
END OF tt_data.
ENDCLASS.

Related

Pass inline declared table/variable to subroutine in ABAP

I know when I need to pass an internal table to a subroutine, I need to declare a type so I can use it in the FORM statement.
What happens if the internal table is an inline declaration table from a SELECT statement like this:
SELECT * FROM KNA1 INTO TABLE #DATA(LT_KNA1)
Is there any way to pass this table to a subroutine?
Thank you.
The subroutines are obsolete since ABAP 7.02 (2009), so I use a method in my example.
Inline declarations are an easy way of declaring types implicitly, but the limit of this solution is that you can type the parameter of a method only generically (types STANDARD TABLE, INDEX TABLE, ANY TABLE, ANY) which prevents you from stating the component names statically in your code.
But inline declarations of type DATA(...) are always based on a complete "bound" data type, so you can declare the type explicitly with TYPES and use it to type both your parameter and your data object.
If you use the ABAP Development Tools (Eclipse), you may use the Quick Fix "Declare local variable ... explicitly" to simplify the task:
which gives this code:
REPORT.
CLASS lcl_app DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.
CLASS lcl_app IMPLEMENTATION.
METHOD main.
TYPES: BEGIN OF helper_type, " <=== automatically generated
carrid TYPE scarr-carrid,
carrname TYPE scarr-carrname,
END OF helper_type.
DATA: lt_scarr TYPE STANDARD TABLE OF helper_type. " <=== automatically generated
SELECT carrid, carrname FROM scarr
INTO TABLE #lt_scarr. " <=== automatically changed
ENDMETHOD.
ENDCLASS.
Now, declare manually the table type, use it to type a parameter of a method (a new one here):
REPORT.
CLASS lcl_app DEFINITION.
PUBLIC SECTION.
TYPES: BEGIN OF helper_type,
carrid TYPE scarr-carrid,
carrname TYPE scarr-carrname,
END OF helper_type.
TYPES: tt_scarr TYPE STANDARD TABLE OF helper_type. " <=== declare the type
CLASS-METHODS main.
CLASS-METHODS process_table " <=== new method with this type
IMPORTING table TYPE tt_scarr.
ENDCLASS.
CLASS lcl_app IMPLEMENTATION.
METHOD main.
DATA: lt_scarr TYPE STANDARD TABLE OF helper_type.
SELECT carrid, carrname FROM scarr
INTO TABLE #lt_scarr.
ENDMETHOD.
METHOD process_table. " <=== new method
LOOP AT table REFERENCE INTO DATA(line).
DATA(carrid) = line->carrid.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
Use TYPE ANY or generic table type for parameters, TABLE parameters are obsolete:
FORM fill_table USING tab TYPE any
CHANGING ptab TYPE INDEX TABLE.
APPEND LINES OF tab TO ptab.
ENDFORM.
And yes, subroutines are obsolete themselves, use them only if you absolutely must do this, e.g. in legacy environment.
You don't need to indicate structure type for perform variable table. But you can get error on runtime if any field name change which are used in perform. Two working example below.
SELECT * FROM kna1 INTO TABLE #DATA(lt_kna1).
PERFORM test TABLES lt_kna1.
FORM test TABLES pt_kna1 STRUCTURE kna1.
*
ENDFORM.
Risky one:
FORM test TABLES pt_kna1.
*
ENDFORM.

How to add old HR INCLUDE into local class?

So I need to use the INCLUDES rpcblo00 and rpcbdt00 to get the type of infotype change (create, update, delete). Beforehand I used a subroutine that had no problem with the includes, but I cannot get them into a class for the life of me.
If I try to put the include into the method as described here (it's even about the same HR include), I get the following error (because of the minus in lo-key):
Syntax error: Names may only consist of the characters "A-Z", "0-9"
and "_". In addition, they may not begin with a number.
minimal reproducible example:
CLASS lcl_infotypaenderungen DEFINITION.
PUBLIC SECTION.
TYPES: tty_aenderungs_operationen TYPE STANDARD TABLE OF pc403.
METHODS:
constructor
IMPORTING is_aenderungs_kopf TYPE pldoc_key,
get_aenderungs_operationen
RETURNING value(rt_aenderungs_operationen) TYPE tty_aenderungs_operationen.
PRIVATE SECTION.
DATA: s_aenderungs_kopf TYPE pldoc_key,
t_aenderungs_operationen TYPE tty_aenderungs_operationen.
METHODS:
select_aenderungs_operationen.
ENDCLASS. "lcl_infotypaenderungen DEFINITION
*----------------------------------------------------------------------*
TYPE-POOLS: abap.
DATA: lo_infotypaenderungen TYPE REF TO lcl_infotypaenderungen,
lv_fehler TYPE sy-subrc,
lt_log_kopf TYPE pldoc_key_tab WITH HEADER LINE,
lt_log_felder TYPE TABLE OF hrinftylog_fields,
lt_infotyp_vorher TYPE prelp_tab,
lt_infotyp_nachher TYPE prelp_tab,
lt_aenderungs_operationen TYPE STANDARD TABLE OF pc403.
FIELD-SYMBOLS: <log_kopfzeile> TYPE pldoc_key.
*----------------------------------------------------------------------*
CALL FUNCTION 'HR_INFOTYPE_LOG_GET_LIST'
EXPORTING
tclas = 'A'
begda = '20190315'
endda = '20190315'
IMPORTING
subrc = lv_fehler
TABLES
infty_logg_key_tab = lt_log_kopf.
CLEAR lv_fehler.
SORT lt_log_kopf DESCENDING BY infty bdate btime pernr.
LOOP AT lt_log_kopf ASSIGNING <log_kopfzeile>.
CALL FUNCTION 'HR_INFOTYPE_LOG_GET_DETAIL'
EXPORTING
logged_infotype = <log_kopfzeile>
IMPORTING
subrc = lv_fehler
TABLES
infty_tab_before = lt_infotyp_vorher
infty_tab_after = lt_infotyp_nachher
fields = lt_log_felder.
CREATE OBJECT lo_infotypaenderungen
EXPORTING
is_aenderungs_kopf = <log_kopfzeile>.
REFRESH lt_aenderungs_operationen.
lt_aenderungs_operationen = lo_infotypaenderungen->get_aenderungs_operationen( ).
ENDLOOP.
*----------------------------------------------------------------------*
CLASS lcl_infotypaenderungen IMPLEMENTATION.
METHOD constructor.
me->s_aenderungs_kopf = is_aenderungs_kopf.
me->select_aenderungs_operationen( ).
ENDMETHOD. "constructor
METHOD select_aenderungs_operationen.
INCLUDE rpcblo00. """ <---
INCLUDE rpcbdt00. """ <---
lo-key-tclas = me->s_aenderungs_kopf-tclas.
lo-key-pernr = me->s_aenderungs_kopf-pernr.
lo-key-infty = me->s_aenderungs_kopf-infty.
lo-key-bdate = me->s_aenderungs_kopf-bdate.
lo-key-btime = me->s_aenderungs_kopf-btime.
lo-key-seqnr = me->s_aenderungs_kopf-seqnr.
IMPORT header TO me->t_aenderungs_operationen FROM DATABASE pcl4(la) ID lo-key.
ENDMETHOD. "select_aenderungs_operationen
METHOD get_aenderungs_operationen.
rt_aenderungs_operationen = me->t_aenderungs_operationen.
ENDMETHOD. "get_aenderungs_operationen
ENDCLASS. "lcl_infotypaenderungen IMPLEMENTATION
Anyone know a good solution? Thanks in advance
Edit: The includes have some declarations and a makro reading from a data cluster. Of course I could just put those directly into the method, but I would like to avoid that (for now I did that).
Alternatively, does someone know of a different way to get the change operation per infotype line?
If you use your class as a local one then the only way to use these includes is to put them at the very beginning of the program. The downside is of course that the variables there become global but unfortunately there is no other way to do that and for sure not if you want to use a global class after all (not sure if your minimal working example is just simplified to use a local class instead of global or not).
REPORT ZZZ.
INCLUDE rpcblo00. """ <---
INCLUDE rpcbdt00. """ <---
CLASS lcl_infotypaenderungen DEFINITION.
" ...
Thanks to Jagger I can make it work with a local class, but in case anyone later wonders how you need to change the include code to be able to use it in a global method, you basically just need to get rid of INCLUDE STRUCTURE declarations and exchange tables with a header line.
So
DATA BEGIN OF LO-KEY.
INCLUDE STRUCTURE PC400.
DATA END OF LO-KEY.
becomes
DATA: lo_key TYPE pc400.
And
DATA BEGIN OF BELEGE_00 OCCURS 100.
DATA:
SPLKZ(01) TYPE X,
FIELD(10) TYPE C,
FTYPE(04) TYPE C,
FLENG(03) TYPE N,
DECIM(02) TYPE N,
OLDDT(50) TYPE C,
NEWDT(50) TYPE C.
DATA END OF BELEGE_00.
becomes
TYPES: BEGIN OF ty_belege,
splkz(01) TYPE x,
field(10) TYPE c,
ftype(04) TYPE c,
fleng(03) TYPE n,
decim(02) TYPE n,
olddt(50) TYPE c,
newdt(50) TYPE c,
END OF ty_belege.
DATA: belege_00 TYPE STANDARD TABLE OF ty_belege.
The macro can stay the same (or I guess you could rewrite it).

ABAP: pass data type to form

I want to pass my own datatype to a form - but it doesn´t work:
TYPES: BEGIN OF my_type,
v1 TYPE i,
v2 TYPE i,
END OF my_type.
PERFORM calc using ...some parameters... .
FORM calc using ...some parameters... .
DATA values TYPE my_type " <- ERROR type my_type does not exist
...some code...
ENDFORM.
Remark: Based on more information in comments: The code is defined in a function module.
A function module is its own programm (the name is SAPL...function group name...). Each function module is its own include.
If you define a type in a report, a function module can't know the type definition. If you need to share a type definition between a reports and function modules (groups), you should define it as a global type in SE11.
If you run your code only inside a function module, you may define types in the top include of the function group. But you should not use this definition in the function module interfaces.

Syntax of a functional method call as an FM parameter?

I have the following piece of code.
REPORT ZZY.
CLASS lcl_main DEFINITION FINAL CREATE PRIVATE.
PUBLIC SECTION.
CLASS-METHODS:
convert_to_xstring
IMPORTING
i_param1 TYPE i
i_param2 TYPE i
RETURNING
VALUE(rv_result) TYPE xstring,
main.
ENDCLASS.
CLASS lcl_main IMPLEMENTATION.
METHOD convert_to_xstring.
ENDMETHOD.
METHOD main.
DATA: lt_binary_tab TYPE STANDARD TABLE OF x.
DATA(lv_result) = convert_to_xstring( i_param1 = 1 i_param2 = 2 ).
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lcl_main=>convert_to_xstring(
EXPORTING
i_param1 = 1
i_param2 = 2
)
TABLES
binary_tab = lt_binary_tab.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
lcl_main=>main( ).
A functional method call that is not a part of a function module call can be written like that.
DATA(lv_result) = convert_to_xstring( i_param1 = 1 i_param2 = 2 ).
However when I want to use it exactly as written above
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lcl_main=>convert_to_xstring( i_param1 = 1 i_param2 = 2 )
TABLES
binary_tab = lt_binary_tab.
I get the following syntax error.
Field "CONVERT_TO_XSTRING(" is unknown. It is neither in one of the
specified tables nor defined by a "DATA" statement. "DATA" statement.
It looks like the compiler needs some guidance in this case to distinguish between an attribute and a method. Why would it be ambiguous for the compiler to let such a case without writing EXPORTING?
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lcl_main=>convert_to_xstring( EXPORTING i_param1 = 1 i_param2 = 2 )
TABLES
binary_tab = lt_binary_tab.
The design of abap is quite bad. There is something like functional method calls, but you can't use it in combination with all commands. For example the WRITE command doesn't work in combination with functional method calls. This seems to be some kind of "partial compatible" with function method calls.
I don't know why(maybe the sap dev folks were drunk), but it is just a fact we have to live with.

Method lcl_util called only once in a loop why?

The following program runs fine, however, the SELECT..ENDSELECT part runs only once, whereas it should be calling the class method lcl_util multiple times once for each rows in the table? Why is this due to?
*&---------------------------------------------------------------------*
*& Report ZDYNAMIC_PROG2
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZDYNAMIC_PROG2.
PARAMETER:
p_from(30) TYPE c DEFAULT 'T001L',
p_where(255) TYPE c
DEFAULT 'WERKS = ''PL01'' AND LGORT = ''SL01'' '.
* ----------------------------------------------------------------------*
* CLASS lcl_util DEFINITION
* ----------------------------------------------------------------------*
*
* ----------------------------------------------------------------------*
CLASS lcl_util DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
write_struct IMPORTING p_struct TYPE any.
ENDCLASS. "lcl_util DEFINITION
* ----------------------------------------------------------------------*
* CLASS lcl_util IMPLEMENTATION
* ----------------------------------------------------------------------*
*
* ----------------------------------------------------------------------*
CLASS lcl_util IMPLEMENTATION.
METHOD write_struct.
FIELD-SYMBOLS:
<field> TYPE any.
WRITE / '('.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE p_struct TO <field>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
WRITE /4 <field>.
ENDDO.
WRITE / ')'.
ENDMETHOD. "write_struct
ENDCLASS. "lcl_util IMPLEMENTATION
DATA:
data_ref TYPE REF TO data,
where_tab LIKE TABLE OF p_where.
FIELD-SYMBOLS:
<line> TYPE any.
START-OF-SELECTION.
CREATE DATA data_ref TYPE (p_from).
ASSIGN data_ref->* TO <line>.
* APPEND p_where TO where_tab.
SELECT * FROM (p_from) INTO <line> WHERE (p_where).
CALL METHOD lcl_util=>write_struct
EXPORTING
p_struct = <line>.
ENDSELECT.
That SELECT-ENDSELECT structure is dependent on the selection criteria, so if there is no match, the call to the class is skipped or it will only execute based on how many rows are returned.
From the structure of the logic, at execution time, its hard to know how many times the loop will execute.
Typically, SELECT-ENDSELECT structures should be avoided as they can affect performance and in this case make it difficult knowing how many rows matched the selection criteria.
My suggestion would be to: (1) Select the entries into an internal table. (2) Then loop on that internal table, calling your method to write the table entries out.
Here is a good blog post regarding performance tips.
Here is a good blog post regarding SELECT INTO vs. SELECT-ENDSELECT