Code examples · Modern ABAP · Intermediate
Modern ABAP expressions: VALUE, FOR, FILTER, COND, SWITCH and REDUCE
The problem
From a list of three orders the program must print the ids of the open orders, add up their amounts, give the total a size label (small, medium, big) and show the status text of the closed…
How we solve it
- 1BuildVALUE
- 2SelectFILTER, FOR
- 3ComputeREDUCE, COND, SWITCH
The result
The two open orders are listed, their total is 175.25 and medium, and the closed order is labelled Closed.
1. The requirement
From a list of three orders the program must print the ids of the open orders, add up their amounts, give the total a size label (small, medium, big) and show the status text of the closed order.
2. How we design it
Each task has an expression that returns a value, so the code reads as a list of results instead of loops with helper variables. VALUE creates the table. FILTER selects rows through a sorted key. FOR inside VALUE converts rows into text. REDUCE folds many rows into one number. COND and SWITCH turn a value into a label.
Why we decided it this way
- 1A secondary sorted key on the statusFILTER needs a sorted or hashed key to work on. With a key it also avoids scanning every row.
- 2Decimal floating point for the totalDECFLOAT34 adds money values exactly and prints without trailing zeros.
- 3SWITCH for exact values, COND for rangesSWITCH compares one operand with fixed values. COND takes any condition, so it fits ranges.
Before you start
- Basic ABAP: classes, internal tables and string templates.
3. The code, step by step
Step 1 of 3: Build the data with VALUE
A table with a unique key and a secondary key, filled in one expression.
" Class frame
CLASS zcl_expressions_demo DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_expressions_demo IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
" Row type and table type with two keys
TYPES: BEGIN OF ty_order,
id TYPE i,
status TYPE c LENGTH 1,
amount TYPE p LENGTH 8 DECIMALS 2,
END OF ty_order,
ty_orders TYPE SORTED TABLE OF ty_order WITH UNIQUE KEY id
WITH NON-UNIQUE SORTED KEY by_status COMPONENTS status.
" VALUE creates the rows
DATA(orders) = VALUE ty_orders(
( id = 1 status = 'O' amount = '100.00' )
( id = 2 status = 'C' amount = '250.50' )
( id = 3 status = 'O' amount = '75.25' ) ).
Part by part (plain text)
- Class frame. A runnable class with the console interface, the same frame as in the other examples.
- Row type and table type with two keys. The primary key is unique on ID. The secondary key BY_STATUS is sorted on STATUS and allows duplicates. A secondary key is what FILTER uses in the next step.
- VALUE creates the rows. Every inner pair of brackets is one row. The type before the bracket tells VALUE which table to build, so no helper variable and no APPEND is needed.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
The two open orders are listed, their total is 175.25 and medium, and the closed order is labelled Closed.
What you should see
Order 1 Order 3 Open total: 175.25 (medium) Order 2 is Closed
Try it and check the result
- Activate and run with F9. The console shows the four lines of the outcome.
- Change the amount of order 1 to 400.00 and run again: the total is 475.25 and the label becomes big.
- Remove the USING KEY part from FILTER and activate: the syntax check refuses it, which shows why the secondary key is declared.
Common mistakes
- A table expression such as orders[ id = 9 ] raises an exception if the row does not exist. Use the OPTIONAL addition or check with line_exists first.
- REDUCE with an integer start value silently rounds decimals. Give the start value the same type as the result.
- Very long nested expressions are hard to debug. Split them into steps once they stop reading like a sentence.