In this series, the goal is to focus on comparing older ABAP language features with a detailed explanation of the new ABAP syntax. The full posts were originally published on the SAP Community Network.
I recently wrote a program to explain older and newer ways to do a summary on an internal table. This article will dissect that program. We will show the various ways to accomplish the same thing for a table summary, and how the ABAP language has evolved over the years. This article will be useful for both new ABAPers, as well as experienced ABAPers trying to get familiar with the new ABAP.
This Blog is organized as follows:
- Program Overview
- Radio Button Options
- Record Generator
- Table Processing Options
- Standard Table
- Sorted Table
- New ABAP
- Performance
- Complete Program
- References
Software Versions:
- 52 of SAP, SAPGUI Version 750.
- Eclipse Version: Oxygen.1a Release (4.7.1a)
Program Overview
The program will have the following select options:
The program will perform the following steps:
- Take the “Number of Records” field from the selection screen and generate a table of sample records.
- Process the table according to the selected Radio Button option.
- Display the resulting summary table in ALV.
Newer ABAP supports method chaining, so to run the program, we simply do the following at START-OF-SELECTION:
lcl_my_class=>main( )->execute( ).
Broken into parts:
- lcl_my_class=>main( ) – Instantiates a new instance of LCL_MY_CLASS. This is a static factory method, which simply returns an instance of itself.
- execute( ) – All of the main logic is contained in the EXECUTE method of LCL_MY_CLASS.
Radio button Options
- Standard Table: With this option, we will show the old school method for performing a summary by sorting and collecting.
- Sorted Table: With this option, we will do a read on a sorted table, then update the summary field using a field symbol.
- New ABAP: With this option, we will utilize the new ABAP language features to update the records and increment the summary field.
All 3 options, above, will perform the same logic, described below, but with different table reads and updates.
Here is an overview of what the 3 methods will do:
You may have guessed, this simply doubles the value in SUMMARY_FIELD, since the table was cloned from the original.
Example:
IT_TEST_TABLE, passed into the method (unsorted):
Resulting ALV (sorted by keys), after adding the temporary tables. Simply doubles the SumField (SUMMARY_FIELD):
When executing each of the 3 options, the results will be identical to the above.
Record Generator
All 3 of the above methods will use the following generated sample table. We will use the new ABAP features to create the random records with the following method lm_build_records:
The parameter p_recs is the “Number of Records” field on the selection screen. To further explain the above new ABAP code, here is how the random records are generated…
We will loop and create P_RECS number of records.
For each loop pass, it will dynamically generate a new record of type LTY_STRUCT. No intermediate variables needed with the new ABAP.
VALUE lty_struct(…) – This tells the compiler to generate a new record of type LTY_STRUCT. This structure was declared in our private global section for our class:
This will randomly generate the following values for the fields:
- KEY1 – Take 900 plus the current loop pass number contained in SY-INDEX (i.e. 901, 902, etc.)
- KEY2 – The current loop pass contained in SY-INDEX.
- FIELD1 – The text “A Text Field”, plus the current loop pass number in SY-INDEX. The new string templates in the new ABAP, generates the exact string contained within the Pipe (|) symbols (no more CONCATENATE statement needed!). Also, within your string, you can dynamically specify a variable within your string within curly braces {}. So, |A Text Field – { sy-index }| will be “A Text Field – 1” on the first loop pass.
- A_DATE_FIELD – Take today’s date and add the current loop pass number (SY-INDEX) in days.
- SUMMARY_FIELD – Take the current loop pass multiplied by 10.
After this record is generated, it is appended to ET_TEST_TABLE, an export table parameter for the method.
To continue on with table code and the rest of the blog post, which goes into much greater detail, head over to the full original version on SAP Community Network.