RAP – Popup Default values
Share

[[{“value”:”

How can you provide the user with default values in the popup of an action in RAP  ?

Introduction

When a user triggers an action in the Fiori Elements UI, you usually want to have some information from the user. To make life easier for the user, you can also provide default values in many situations so that the user receives suggestions for input and thus works faster. To do this, we will extend our Report Pattern app with another function.

Behaviour definition

To do this, we have a current definition of the “Approve” action in the behaviour definition. We are currently using a simple action with a parameter, which ensures that a popup is displayed when triggered.

sanjay22_0-1789972495441.png

To do this, we add DEFAULT FUNCTION in curly brackets to define a new function in which we can set the default values.

The name of the function must begin with “GetDefaultsFor” or “GetDfltsFor“. Here you already get the information.

managed implementation in class zbp_sm_i_trav_req unique;
strict ( 2 );
with draft;

define behavior for ZSM_I_TRAV_REQ //alias <alias_name>
persistent table zsm_travel_req
draft table zsm_trv_draft
lock master
total etag LastModifiedOn
authorization master ( instance )
//etag master <field_name>
{
create ( authorization : global );
update;
delete;
field ( readonly, numbering : managed ) RequestId;

action ( features : instance ) Approve parameter ZA_APPROVE_PARAM
result [1] $self { default function GetDefaultsFor_Appr; }

action ( features : instance ) Reject result [1] $self;
action ( features : instance ) cancel result [1] $self;

draft action Activate optimized;
draft action Edit;
draft action Discard;
draft action Resume;

draft determine action Prepare;

mapping for zsm_travel_req
{
RequestId = request_id;
EmployeeId = employee_id;
Destination = destination;
Status = status;
CreatedBy = created_by;
CreatedOn = created_on;
EmployeeName = employee_name;
LastModifiedBy = last_modified_by;
LastModifiedOn = last_modified_on;
TravelDate = travel_date;
}
}

Behaviour implementation

If you place the cursor on the name of the function, you can use CTRL + 1 to generate the method in the behaviour implementation. Let’s take a look at the signature of the new method.

sanjay22_1-1789972242803.png

We receive the keys of the currently selected entries and can return our values via RESULT. There, next to the key, you will find the %PARAM field, where our parameter structure is defined. To do this, we define a small logic that defines a default value for Approved date, Approved by and remarks.

sanjay22_2-1789972284256.png

CLASS lhc_ZSM_I_TRAV_REQ DEFINITION INHERITING FROM cl_abap_behavior_handler.
PRIVATE SECTION.

METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION
keys REQUEST requested_authorizations FOR zsm_i_trav_req RESULT result.

METHODS get_global_authorizations FOR GLOBAL AUTHORIZATION
REQUEST requested_authorizations FOR zsm_i_trav_req RESULT result.
METHODS approve FOR MODIFY
keys FOR ACTION zsm_i_trav_req~approve RESULT result.

METHODS cancel FOR MODIFY
keys FOR ACTION zsm_i_trav_req~cancel RESULT result.

METHODS reject FOR MODIFY
keys FOR ACTION zsm_i_trav_req~reject RESULT result.
METHODS get_instance_features FOR INSTANCE FEATURES
keys REQUEST requested_features FOR zsm_i_trav_req RESULT result.
METHODS getdefaultsfor_appr FOR READ
keys FOR FUNCTION zsm_i_trav_req~getdefaultsfor_appr RESULT result.

ENDCLASS.

CLASS lhc_ZSM_I_TRAV_REQ IMPLEMENTATION.

METHOD get_instance_authorizations.
ENDMETHOD.

METHOD get_global_authorizations.
ENDMETHOD.

METHOD Approve.
MODIFY ENTITIES OF zsm_i_trav_req IN LOCAL MODE
ENTITY zsm_i_trav_req
UPDATE FIELDS ( Status )
WITH VALUE #( FOR ls IN keys
( %tky = ls-%tky
Status = ‘A – APPROVED’ ) ).

READ ENTITIES OF zsm_i_trav_req IN LOCAL MODE
ENTITY zsm_i_trav_req
ALL FIELDS WITH CORRESPONDING #( keys )
RESULT DATA(lt_read).

result = VALUE #( FOR ls1 IN lt_read
( %tky = lS1-%tky
%param = ls1 ) ).
ENDMETHOD.

METHOD cancel.
MODIFY ENTITIES OF zsm_i_trav_req IN LOCAL MODE
ENTITY zsm_i_trav_req
UPDATE FIELDS ( Status )
WITH VALUE #( FOR ls IN keys
( %tky = ls-%tky
Status = ‘C – CANCELLED’ ) ).

READ ENTITIES OF zsm_i_trav_req IN LOCAL MODE
ENTITY zsm_i_trav_req
ALL FIELDS WITH CORRESPONDING #( keys )
RESULT DATA(lt_read).

result = VALUE #( FOR ls1 IN lt_read
( %tky = lS1-%tky
%param = ls1 ) ).
ENDMETHOD.

METHOD Reject.
MODIFY ENTITIES OF zsm_i_trav_req IN LOCAL MODE
ENTITY zsm_i_trav_req
UPDATE FIELDS ( Status )
WITH VALUE #( FOR ls IN keys
( %tky = ls-%tky
Status = ‘R – REJECTED’ ) ).

READ ENTITIES OF zsm_i_trav_req IN LOCAL MODE
ENTITY zsm_i_trav_req
ALL FIELDS WITH CORRESPONDING #( keys )
RESULT DATA(lt_read).

result = VALUE #( FOR ls1 IN lt_read
( %tky = lS1-%tky
%param = ls1 ) ).
ENDMETHOD.

METHOD get_instance_features.
READ ENTITIES OF zsm_i_trav_req IN LOCAL MODE
ENTITY zsm_i_trav_req
ALL FIELDS WITH CORRESPONDING #( keys )
RESULT DATA(lt_read).

result = VALUE #( FOR ls_read IN lt_read
( %key = ls_read-%key
%features-%action-Approve = COND #( WHEN ls_read-Status = ‘A – APPROVED’ OR ls_read-Status = ‘C – CANCELLED’ OR ls_read-Status = ‘R – REJECTED’
THEN if_abap_behv=>fc-o-disabled
ELSE if_abap_behv=>fc-o-enabled )

%features-%action-Reject = COND #( WHEN ls_read-Status = ‘R – REJECTED’ OR ls_read-Status = ‘C – CANCELLED’ OR ls_read-Status = ‘A – APPROVED’
THEN if_abap_behv=>fc-o-disabled
ELSE if_abap_behv=>fc-o-enabled )

%features-%action-cancel = COND #( WHEN ls_read-Status = ‘C – CANCELLED’ OR ls_read-Status = ‘R – REJECTED’ OR ls_read-Status = ‘A – APPROVED’
THEN if_abap_behv=>fc-o-disabled
ELSE if_abap_behv=>fc-o-enabled ) ) ).
ENDMETHOD.

METHOD GetDefaultsFor_Appr.

LOOP AT keys INTO DATA(ls_keys).
INSERT VALUE #( %tky = ls_keys-%tky
%param = VALUE za_approve_param( ApprovalDate = cl_abap_context_info=>get_system_date( )
Remarks = ‘Leave Remarks here’
ApprovedBy = cl_abap_context_info=>get_user_technical_name( ) ) ) INTO TABLE result.

ENDLOOP.
ENDMETHOD.

ENDCLASS.

Projection

Now comes the most important step. So that the function can be called from the frontend, we have to release it in the projection of our RAP object to the outside. In this case, it is not an action, but a function. Accordingly, the implementation looks like this:

sanjay22_0-1789972101109.png

Test

Now that we have everything prepared, we can carry out the test in application. To do this, we click on Approve and trigger the action. You can see the result.

sanjay22_0-1789972734448.png

sanjay22_9-1789971794887.png

Conclusion

Using Default Functions in RAP is a simple and effective way to prepopulate action popup fields with meaningful values, such as the current date, logged-in user, or business-specific defaults. This improves usability, reduces manual data entry, and ensures consistent data capture.

By defining a DEFAULT FUNCTION, implementing the corresponding GetDefaultsFor_* method, and exposing it in the projection behavior, developers can provide a smarter and more user-friendly Fiori experience with minimal development effort.

 

 

 

“}]] 

  Read More Technology Blog Posts by Members articles 

#abap

By ali

Leave a Reply