ERPClimb logoERPClimb

Code examples · ABAP RAP · Intermediate

RAP instance feature control: disable Approve once a request is decided

The problem

A request that is already approved or rejected must not be approved again.

How we solve it

  1. 1DeclareBDEF
  2. 2Decide per rowget_instance_features
  3. 3Guardcheck in action

The result

Approve is enabled only for requests with Status N.

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

1. The requirement

A request that is already approved or rejected must not be approved again. The Approve button must be disabled for such rows in the list and on the object page, and a call through the API must be refused too.

2. How we design it

Feature control answers one question per row: is this operation allowed right now? We declare it in the behavior definition, implement one handler method that looks at the current status of each instance, and return enabled or disabled. The UI reads this and disables the button. The framework enforces the result for OData and external EML calls, not only in the UI. The action still checks the status itself as a second line of defence, because code that runs IN LOCAL MODE skips feature control, and feature control is not authorization.

Why we decided it this way

  1. 1Instance feature controlThe answer depends on the data of each row (its status), so it must be decided per instance, not once for the whole app.
  2. 2Also check inside the actionAn API client does not look at button states. The action must refuse an invalid call on its own.
  3. 3Feature control is not authorizationIt hides what is not meaningful. Who may do something is decided by authorization checks, which are a separate topic.

Before you start

  • The Leave Request business object from 'Build a managed RAP business object' with the approve action.
  • Optionally the Reject action from the previous example, so that status R exists.

3. The code, step by step

Step 1 of 3: Declare feature control on the action

Tell the framework that the availability of Approve depends on the instance.

BDEF · BDEF ZI_LeaveRequest
  // action ( features : instance )
  action ( features : instance ) approve result [1] $self;
Part by part (plain text)
  1. action ( features : instance ). Replace the existing approve line. The addition 'features : instance' makes the framework ask your handler, for every row, whether the action is enabled.

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

Approve is enabled only for requests with Status N. For A or R the button is disabled, and calling the action through the API returns an error.

What you should see

Request with Status N: Approve is clickable. Request with Status A or R: Approve is greyed out. Calling approve for an already approved request through the API: HTTP 4xx with the message 'Request is already decided'.

Try it and check the result

  • Activate the BDEF and the behavior class, then reload the service preview.
  • Open a request with Status N: the Approve button is active. Approve it: the button becomes disabled.
  • Create a second request and call the approve action twice through the API (see the OData example): the second call returns the error message.

Common mistakes

  • Feature control is not authorization. Code that runs IN LOCAL MODE skips it, so keep the status check inside the action as well.
  • Expecting the button state to refresh without a side effect or reload: after an action the app reads the instance again; if the state looks stale, check that the action returns $self.
  • Treating feature control as security. Use authorization master/dependent and instance authorization for who may act.

Keep learning