Code examples · ABAP RAP · Advanced
RAP draft handling: let users save work in progress and activate it later
The problem
Employees fill in leave requests in a long form and are often interrupted.
How we solve it
- 1Draft tableZLEAVE_REQ_D
- 2Behaviorwith draft
- 3Projectionuse draft
The result
The Fiori app now shows Edit, Save and Discard.
1. The requirement
Employees fill in leave requests in a long form and are often interrupted. A half-filled request must not be lost when the browser closes, and it must not appear in manager lists or trigger validations until the employee presses Save.
2. How we design it
Draft handling keeps two states of the same object: the active data in the normal table, and the work in progress in a separate draft table. The framework gives you the Edit, Activate, Discard, Resume and Prepare operations. The Prepare step is where the rules run before activation, so we attach the date validation to it. The projection must also expose the draft operations, otherwise Fiori cannot use them.
Why we decided it this way
- 1A separate draft tableDrafts must not show up in reports or be read by other business logic, so they live in their own table with the same fields plus administrative draft fields.
- 2Total ETag on the last-changed fieldWith drafts, a request can change in the draft table and in the active table. The total ETag protects both states against overwriting.
- 3Validation on PrepareA draft may be incomplete while it is typed. The rules run when the user wants to activate it, through the Prepare step.
- 4Draft is a design decisionIt adds a table, extra operations and test cases. Use it for long forms, not for every small object.
Before you start
- The Leave Request business object from 'Build a managed RAP business object'.
- A system that supports draft in managed RAP (ABAP Environment, or S/4HANA 2020 and later; check your release).
3. The code, step by step
Step 1 of 3: Create the draft table
Same fields as the active table plus the administrative draft fields.
// Table header
@EndUserText.label : 'Leave request draft'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zleave_req_d {
// Same business and administrative fields
key client : abap.clnt not null;
key leaveuuid : sysuuid_x16 not null;
employeeid : abap.numc(8);
startdate : abap.dats;
enddate : abap.dats;
status : abap.char(1);
createdby : abp_creation_user;
createdat : abp_creation_tstmpl;
lastchangedby : abp_lastchange_user;
lastchangedat : abp_lastchange_tstmpl;
locallastchangedat : abp_locinst_lastchange_tstmpl;
// Draft administration include
"%admin" : include sych_bdl_draft_admin_inc;
}
Part by part (plain text)
- Table header. The draft table is an ordinary table. In ADT, the quick fix 'Create draft table' on the BDEF can generate it for you; this shows what it contains.
- Same business and administrative fields. Every field of the active table appears again, with the same types and the same key fields (client and UUID).
- Draft administration include. '%admin' is a structure the framework needs to remember who owns the draft, when it was last changed and whether it is being edited. Do not rename it.
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 Fiori app now shows Edit, Save and Discard. A new request is created as a draft, survives closing the browser, appears as a draft in the list for its owner, and only becomes a real request after Save passes the validations.
What you should see
Start a new request, fill part of the form and close the browser tab. Open the app again: the list shows the request as 'Draft' and you can continue. Press Save with an end date before the start date: the error appears and the request stays a draft. Press Save with valid dates: the draft becomes an active request.
Try it and check the result
- Activate the draft table, the BDEF, the behavior projection (in this order), then the service. Refresh the preview.
- Click Create, enter an employee and close the tab. Reopen the app: the list should show the entry with a draft marker.
- Open the draft, set the end date before the start date and press Save: you should see the validation message and the entry stays a draft.
- Fix the dates and Save: the draft becomes an active request.
Common mistakes
- The draft table must have every field of the active table, with identical names after mapping. A missing field fails the activation. If you also added the Reject example, add rejectreason : abap.char(100) to the draft table too.
- Forgetting 'use draft' or one of the draft actions in the projection behavior gives errors on activation.
- Choosing a total ETag field that is not updated when the active instance changes lets users overwrite each other.
- Draft data is not active data: never read it in reports or other logic as if it were a real request.