Code examples · Modern ABAP · Intermediate
Who changed what? Read change documents of a sales order from CDHDR and CDPOS
The problem
A customer says the delivery date of an order was changed behind his back.
How we solve it
- 1HeadersCDHDR
- 2ItemsCDPOS
- 3Showprint
The result
Each change shows who, when, which field, old value and new value.
1. The requirement
A customer says the delivery date of an order was changed behind his back. Support must show who changed which field, when, and from which value to which value, without opening the order screen and looking through menus.
2. How we design it
For many business objects SAP writes change documents. The header table CDHDR has one row per change event: the object class (VERKBELEG for sales documents), the object id (the order number), the user, the date and the time. The item table CDPOS has one row per changed field with the old and the new value. We read the five latest headers of the order, then the items of exactly those headers with FOR ALL ENTRIES, and print them together.
Why we decided it this way
- 1Two reads, not one joinCDPOS has been a cluster table in older releases, and joins on cluster tables are not allowed. Two reads work on every release.
- 2FOR ALL ENTRIES only after an emptiness checkWith an empty driver table FOR ALL ENTRIES reads the whole table. The check is not optional.
- 3Latest firstSupport wants the last change first. We sort by date and time descending.
Before you start
- An order that was changed at least once. Make a change in VA02 and save, then look at it via Environment > Changes in VA03.
- ADT and a package such as $TMP.
3. The code, step by step
Step 1 of 3: Read the change headers
The five latest changes of the order.
" SELECT from CDHDR
CLASS zcl_order_changes DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_order_changes IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
DATA(order_id) = CONV cdhdr-objectid( '0000012345' ).
SELECT objectclas, objectid, changenr, username, udate, utime
FROM cdhdr
WHERE objectclas = 'VERKBELEG'
AND objectid = @order_id
ORDER BY udate DESCENDING, utime DESCENDING
INTO TABLE @DATA(headers)
UP TO 5 ROWS.
IF headers IS INITIAL.
out->write( 'No change documents for this order' ).
RETURN.
ENDIF.
Part by part (plain text)
- SELECT from CDHDR. OBJECTCLAS says what kind of object changed, VERKBELEG is the sales document. OBJECTID is the number of the order as text. CHANGENR identifies one change event. The latest changes come first, and UP TO 5 ROWS keeps the list short.
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
Each change shows who, when, which field, old value and new value.
What you should see
Example output (your system has other data): 2026-11-20 14:03:27 JSMITH VBEP-EDATU 20261201 -> 20261215 2026-11-18 09:12:41 AROY VBAK-AUGRU (empty) -> 100
Try it and check the result
- Replace the order number with an order that you changed in VA02, activate and run with F9.
- Compare with VA03: Environment > Changes shows the same user, date and fields.
- Run with an order that was never changed: the program says that there are no change documents.
Common mistakes
- Not every change writes a change document. Only objects defined with change documents do, and only the fields flagged for it.
- Old and new values are stored as text. A date or an amount is shown as stored, not formatted for the user.
- Do not select CDPOS without the object class and the object id. The table is one of the largest in a system.
- Change documents are personal data of the user who changed. Show them only to people who have the authorization for it.