Code examples · ABAP RAP · Intermediate
Build a managed RAP business object: Leave Request with approve action
The problem
HR wants employees to create leave requests in a Fiori app.
How we solve it
- 1Database tableZLEAVE_REQ
- 2Interface viewZI_LeaveRequest
- 3BehaviorBDEF + class
- 4Projection + UIZC_LeaveRequest
- 5ServiceDefinition + binding
The result
A published OData V4 UI service with create, edit, delete and an Approve button, where all business rules live once, in the business object, not in the UI.
1. The requirement
HR wants employees to create leave requests in a Fiori app. A new request must start with status 'N' (new). End date may never be before start date. A manager must be able to press Approve, which sets the status to 'A'. The same logic must also work when other ABAP code or an API creates a request.
2. How we design it
Put every business rule into the business object, not into the screen. We build bottom-up: table, interface view (the root), behavior (the rules), projection (what the service shows), service. Each layer has exactly one job, so the UI can change without touching the rules.
Why we decided it this way
- 1Managed implementationThe framework already knows how to create, update, delete and save. We only write what is special to leave requests: the initial status, the date check and the approval.
- 2UUID key with managed numberingNo number range to maintain, and no clash when two users create requests at the same moment. A readable request number can be added later.
- 3Validation for the dates, determination for the statusA validation rejects bad data and never changes it. A determination fills in derived values. Keeping them apart makes a failure easy to find.
- 4Approve as an actionApproval is a business event, not a field edit. An action can later get its own authorization and feature control.
- 5A separate projectionThe projection decides what the service exposes and carries the UI annotations, so the interface view stays reusable by other services and APIs.
Before you start
- ABAP Development Tools (ADT) in Eclipse and a system with ABAP Cloud / RAP (for example an ABAP Environment on BTP, or an S/4HANA release that supports managed RAP).
- A package and transport request where you can create Z objects.
- Basic CDS knowledge: define view entity, annotations.
3. The code, step by step
Step 1 of 6: Create the database table
Store leave requests. The managed runtime writes to this table for you; you do not write INSERT statements.
// Table annotations
@EndUserText.label : 'Leave request'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zleave_req {
// Client and UUID key
key client : abap.clnt not null;
key leave_uuid : sysuuid_x16 not null;
// Business fields
employee_id : abap.numc(8);
start_date : abap.dats;
end_date : abap.dats;
status : abap.char(1);
// Administrative fields
created_by : abp_creation_user;
created_at : abp_creation_tstmpl;
last_changed_by : abp_lastchange_user;
last_changed_at : abp_lastchange_tstmpl;
local_last_changed_at : abp_locinst_lastchange_tstmpl;
}
Part by part (plain text)
- Table annotations. These annotations tell the dictionary how to treat the table. #TRANSPARENT means it is a normal database table. deliveryClass #A marks it as application data (business data, not customizing). #NOT_EXTENSIBLE keeps customers from appending fields.
- Client and UUID key. The first key is the client (mandant). The second key is a 16-byte UUID. A UUID key lets RAP generate a unique key automatically (managed numbering), so two users creating requests at the same time can never collide.
- Business fields. The real content of the request: who (employee), from when, until when, and the current status. Status is a single character: 'N' new, 'A' approved. Dates use the dictionary type abap.dats (YYYYMMDD).
- Administrative fields. Created by/at and last changed by/at are filled by the framework. local_last_changed_at is special: the behavior definition uses it as the ETag, which stops one user from overwriting another user's newer change without noticing.
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
A published OData V4 UI service with create, edit, delete and an Approve button, where all business rules live once, in the business object, not in the UI.
What you should see
Create a request with an end date before the start date: the message 'End date must not be before start date' appears next to End Date and nothing is saved. Create a request with valid dates: a new row appears with Status N (you never typed it). Select the row and press Approve: Status changes to A and the list refreshes.
Try it and check the result
- Open the preview, press Create, enter an employee, a start date and an EndDate before the start date, and save. You should see the red message 'End date must not be before start date' next to End Date, and nothing is saved.
- Fix the end date and save. The new row appears with Status N, even though you never typed it. That is your determination.
- Select the row and press Approve. Status becomes A and the list refreshes. That is your action.
- Bonus: call the BO from ABAP with the 'Call your RAP business object from ABAP with EML' example. The same rules apply with no UI involved.
Common mistakes
- Forgetting the field mapping block makes the BDEF activation fail with unmapped-field errors.
- Without 'authorization master' in strict mode the BDEF will not activate; the empty get_global_authorizations method is a placeholder, not security.
- Setting a read-only field from outside the BO is rejected, but your own handler methods run in LOCAL MODE and may set it. Use LOCAL MODE inside handlers.
- No UI annotations means an empty Fiori preview. The service works; the screen simply has nothing to display.
- Never put the Approve rule only in the Fiori app. A UI-only rule is bypassed by APIs and ABAP code.