Code examples · ABAP RAP · Intermediate
RAP action with an input parameter: Reject a leave request with a reason
The problem
Managers must be able to reject a leave request.
How we solve it
- 1Store the reasontable + view
- 2Parameter entityabstract entity
- 3Behavioraction + handler
- 4Expose + UIprojection
The result
A Reject button that opens a dialog with one field, Reason.
1. The requirement
Managers must be able to reject a leave request. Rejecting without a reason is not allowed: the Fiori app must open a small dialog asking for the reason, and the reason must be saved on the request and shown to the employee.
2. How we design it
An action that needs input from the user takes a parameter. The shape of that input is described by an abstract entity (a CDS entity with no database table). The behavior definition links the action to that entity, the Fiori app builds the dialog from it automatically, and the handler reads the typed values from the action's parameter.
Why we decided it this way
- 1Abstract entity for the parameterIt describes the input structure once. The same description drives the dialog in Fiori, the OData action signature and your handler code.
- 2Store the reason on the requestThe employee must see it later, so it is a normal field of the business object, not only a message.
- 3Validate the reason in the actionAn empty reason is a business error. The action rejects it with a message instead of saving a half-finished rejection.
Before you start
- The Leave Request business object from 'Build a managed RAP business object' (table ZLEAVE_REQ, ZI_LeaveRequest, ZC_LeaveRequest, behavior class).
- Permission to add a field to ZLEAVE_REQ. In a transported system, activate the change in your own package.
3. The code, step by step
Step 1 of 4: Add a field for the reason
The rejection reason is part of the business object, so it needs a column, a view field and a mapping entry.
// New column in the table
reject_reason : abap.char(100);
// New field in the interface view
reject_reason as RejectReason,
// Mapping and read-only rule in the BDEF
field ( readonly ) RejectReason;
mapping for zleave_req
{
RejectReason = reject_reason;
}
Part by part (plain text)
- New column in the table. Add one text column to the table definition, below local_last_changed_at. Activate the table. Existing rows get an empty value.
- New field in the interface view. Expose the column with a CamelCase alias in ZI_LeaveRequest. Put it in the field list, for example after Status.
- Mapping and read-only rule in the BDEF. Add the new field to the mapping block of the interface BDEF. Mark it read-only: users must not type it directly. Only the Reject action will fill it, from inside the business object.
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 Reject button that opens a dialog with one field, Reason. Pressing OK with a reason sets the status to R and stores the text. Pressing OK with an empty reason shows an error and changes nothing.
What you should see
Select a request with Status N and press Reject: a dialog with the field Reason appears. Leave Reason empty and confirm: error 'A reason is required' and the status stays N. Enter 'Overlaps with project go-live' and confirm: Status becomes R and the reason is shown on the request.
Try it and check the result
- Activate everything in this order: table, interface view, abstract entity, interface BDEF, behavior class, projection view, projection BDEF, metadata extension. Refresh the service preview.
- Select a request with Status N and press Reject. A dialog with the field Reason must appear.
- Confirm with an empty reason: you must see 'A reason is required' and the request must stay unchanged.
- Confirm with a reason: Status becomes R and the reason appears on the object page.
Common mistakes
- Forgetting 'use action reject;' in the projection behavior: the button never shows up.
- Naming the field differently in table, view and mapping: the BDEF activation reports an unmapped field.
- Setting a read-only field from the handler without LOCAL MODE fails; inside handlers always use IN LOCAL MODE.
- Putting the empty-reason check only in the UI. The business object must enforce it, because APIs bypass the UI.