Code examples · Modern ABAP · Beginner
Modern ABAP: total per customer with VALUE, FOR, GROUP BY, REDUCE and an ABAP Unit test
The problem
Finance needs the total order amount per customer, biggest customer first.
How we solve it
- 1Types + methodclass definition
- 2LogicFOR + GROUP BY + REDUCE
- 3Runnerconsole output
- 4ABAP Unittests
The result
A reusable class method, a console runner that prints the result, and an ABAP Unit test class that passes.
SAP objects in play
1. The requirement
Finance needs the total order amount per customer, biggest customer first. Cancelled orders must not count. The logic must be reusable and covered by an automated test so a later change cannot silently break it.
2. How we design it
Put the logic in a class method that takes a table and returns a table, with no database or screen inside. That makes it easy to call from anywhere and easy to test. Build the method, prove it with a console runner, then lock the behavior in with ABAP Unit.
Why we decided it this way
- 1Static method with RETURNINGInput in, result out, no hidden state.
- 2decfloat34 for the amounts in this exampleSums stay exact. Real database fields would use currency amount types.
- 3FOR ... WHERE instead of FILTERFILTER needs a sorted or hashed table or a secondary key. FOR works on any table.
- 4Tests for the rule, the order and the empty caseEach test protects one promise of the requirement.
Before you start
- ADT (ABAP Development Tools) and a system with ABAP 7.54 or higher, or an ABAP Environment on BTP.
- Know what an internal table and a structure are.
3. The code, step by step
Step 1 of 4: Define types and the method
Describe the data and the one public method before writing any logic.
" Row types
CLASS zcl_order_totals DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
TYPES:
BEGIN OF ty_order,
order_id TYPE string,
customer TYPE string,
amount TYPE decfloat34,
status TYPE c LENGTH 1,
END OF ty_order,
tt_order TYPE STANDARD TABLE OF ty_order WITH EMPTY KEY,
BEGIN OF ty_total,
customer TYPE string,
total TYPE decfloat34,
END OF ty_total,
tt_total TYPE STANDARD TABLE OF ty_total WITH EMPTY KEY.
" Static method signature
CLASS-METHODS total_per_customer
IMPORTING orders TYPE tt_order
RETURNING VALUE(result) TYPE tt_total.
ENDCLASS.
Part by part (plain text)
- Row types. ty_order is one order line. WITH EMPTY KEY means the table has no key columns, the right default for tables you only loop over. Amounts use decfloat34, which avoids rounding surprises in sums. Status 'C' will mean cancelled.
- Static method signature. A static method with a RETURNING parameter is easy to call and easy to test: input in, result out, no hidden state. Callers write zcl_order_totals=>total_per_customer( orders ).
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.
ABAP dumps
If it does not work
4. The final outcome
A reusable class method, a console runner that prints the result, and an ABAP Unit test class that passes. It uses internal tables only, so it runs on any system with ABAP 7.54 or higher (S/4HANA, ABAP Environment).
What you should see
Console: 'Acme 350.00', 'Globex 80.00', 'Order 9999 not found', 'Order 1003 exists: X'. ABAP Unit: three green tests.
Try it and check the result
- Run ZCL_ORDER_TOTALS_DEMO with F9. Expected console: 'Acme 350.00', 'Globex 80.00', 'Order 9999 not found', 'Order 1003 exists: X'.
- Run the unit tests (Ctrl+Shift+F10 in the class): three green tests.
- Break it on purpose: delete the WHERE ( status <> 'C' ) part and run the unit tests again. The 'ignores_cancelled' test fails. That is what a test is for. Then undo your change.
Common mistakes
- Using type p without thinking about decimals can lose cents in sums; decfloat34 is safer for money arithmetic in examples. Real tables use currency amount types with CURRENCY handling.
- A table expression without OPTIONAL or TRY raises an exception on a missing row, an easy cause of short dumps.
- FILTER operator needs a sorted or hashed table or a secondary key, which is why this example uses FOR ... WHERE on a standard table.
- Tests that read real database data become slow and fragile. Pass the data in as parameters, as shown here.