ERPClimb logoERPClimb

Code examples · ABAP RAP · Advanced

RAP additional save: write an audit log row on every create, update and delete

The problem

A ticket business object is managed by RAP.

How we solve it

  1. 1Tablesticket and log
  2. 2Business objectview and BDEF
  3. 3SaverSAVE_MODIFIED
  4. 4Try itEML

The result

Creating and then changing a ticket leaves two log rows, in the order they happened.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

A ticket business object is managed by RAP. Every create, update and delete must also leave an audit row in a log table that records the action, the user, the time and, for updates, which fields were changed. The log row must be saved in the same transaction as the ticket, and never when the save fails.

2. How we design it

Managed RAP already writes the ticket table. We add the clause WITH ADDITIONAL SAVE to the behavior definition. The framework then calls the method SAVE_MODIFIED of a saver class at the end of the save sequence, with everything that changed in this transaction, split into create, update and delete. There we build the log rows and insert them. Because this runs in the late save phase, the log is committed or rolled back together with the ticket.

Why we decided it this way

  1. 1Additional save, not unmanaged saveAdditional save runs in addition to the managed write. Unmanaged save would replace it and force us to write the ticket table ourselves.
  2. 2Log in the saver, not in a determinationDeterminations run before the save and can still be rolled back by a failing validation. The saver only runs when the save is going through.
  3. 3Read changed fields from the control structureFor updates the %CONTROL structure tells which fields were changed in this transaction, so the log shows what changed and not every field.
  4. 4No COMMIT WORKThe RAP framework commits. A COMMIT WORK in the saver is not allowed and would break the transaction.

Before you start

  • A RAP managed business object is explained in the Leave Request example. This example uses a smaller one so the focus stays on the save.
  • ADT and a package for the ticket objects.

3. The code, step by step

Step 1 of 5: Create the ticket table and the log table

One table for the business data and one for the audit trail.

CDS · Database tables · ZTICKET and ZTICKET_LOG
// Ticket table
@EndUserText.label : 'Ticket'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zticket {
  key client      : abap.clnt not null;
  key ticket_uuid : sysuuid_x16 not null;
  title           : abap.char(80);
  status          : abap.char(1);
}
// Log table
@EndUserText.label : 'Ticket audit log'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zticket_log {
  key client     : abap.clnt not null;
  key log_uuid   : sysuuid_x16 not null;
  ticket_uuid    : sysuuid_x16;
  action         : abap.char(10);
  changed_by     : abap.char(12);
  changed_at     : abap.utclong;
  changed_fields : abap.char(60);
}
Part by part (plain text)
  1. Ticket table. The key is a UUID that RAP generates for new tickets. SYSUUID_X16 is the standard 16-byte UUID type.
  2. Log table. One row per change. ABAP.UTCLONG holds a time stamp with microseconds. CHANGED_FIELDS lists the names of the fields that were changed in an update.

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.

4. The final outcome

Creating and then changing a ticket leaves two log rows, in the order they happened.

What you should see

Audit log, oldest first: CREATE UPDATE Title

Try it and check the result

  • Activate the two tables, the view, the behavior definition and the behavior pool. In ADT the quick fix on the BDEF creates the saver stub and the handler stubs for you.
  • Run ZCL_TICKET_AUDIT_DEMO with F9. The console shows the three lines of the outcome.
  • Open ZTICKET_LOG with F8 to see the user and time stamp. Delete the ticket with EML DELETE and commit: a third row with the action DELETE appears.
  • Make the save fail on purpose: temporarily add ASSERT 1 = 0. after the INSERT in SAVE_MODIFIED and run the demo. It ends in a runtime error, the whole transaction is rolled back, and neither the ticket nor the log row is saved.

Common mistakes

  • Forgetting WITH ADDITIONAL SAVE in the BDEF: the saver class exists but is never called. The log stays empty and nothing reports an error.
  • Writing COMMIT WORK or reading from the database buffer inside SAVE_MODIFIED. Only change data there. The framework owns the commit.
  • For updates only the changed fields are reliable in %CONTROL. Do not assume that a field marked off in the control structure also has its old value in the row.
  • Logs for a draft-enabled business object need extra care, because the draft saves are not the same as the activation. Add the log to the activation, not to draft saves.

Keep learning