In the realm of SAP development, the ABAP RESTful Application Programming Model (RAP) has emerged as a powerful framework for building efficient and scalable Fiori Elements applications. The developer can focus on custom business logic as the RAP framework takes care of common/ standard features. Non-standard operations can be an action or a function in a business object. In most cases, the actions/operations require some parameters to execute the business logic.
The primary purpose of defaulting action parameters is to enhance user efficiency by pre-filling form fields with relevant and context-aware values. This reduces manual data entry, minimizes errors, and ensures data consistency across the application. Implementing complex business logic within these defaulting actions adds a layer of intelligence to the application, enabling it to adapt to varying scenarios and user-specific requirements.
This blog post describes the different ways of defaulting action parameters in a Fiori Elements application built on the RAP framework. Earlier blog post can be referred to enable the parameters for an action.
@EndUserText.label: 'Abstract entity for Supplier'
@Metadata.allowExtensions: true
define root abstract entity zrk_a_supplier
{
@UI.defaultValue: 'S000000003'
ToBesupplier : zrk_sup_no;
}
Syntax:
@UI.defaultValue : #( 'ELEMENT_OF_REFERENCED_ENTITY: <field from entity>')
Example:
@EndUserText.label: 'Abstract entity for Supplier'
@Metadata.allowExtensions: true
define root abstract entity zrk_a_supplier
{
@UI.defaultValue : #( 'ELEMENT_OF_REFERENCED_ENTITY: Supplier')
ToBesupplier : zrk_sup_no;
}
The business case is to convert a purchase requisition into a purchase contract. There could be different cases.
The action definition needs to be enriched with a function in the base behavior definition and the function needs to be consumed in the projection layer. Below are the detailed steps.
action ( features : instance, precheck ) Convert_Into_PC
parameter ZRK_A_ActionParam_PR_To_PC result [1] $self
{ default function GetDefaultsForConvert_Into_PC; }
METHODS convert_into_pc FOR MODIFY
IMPORTING keys FOR ACTION _prhead~convert_into_pc RESULT result .
METHODS GetDefaultsForConvert_Into_PC FOR READ
IMPORTING keys FOR FUNCTION _PRHead~GetDefaultsForConvert_Into_PC RESULT result.
METHOD GetDefaultsForConvert_Into_PC.
" Read the requisition header
READ ENTITIES OF zrk_i_pur_req_h IN LOCAL MODE
ENTITY _PRHead
ALL FIELDS WITH CORRESPONDING #( keys )
RESULT DATA(lt_pur_req).
CHECK lt_pur_req IS NOT INITIAL.
" Read the requisition item
READ ENTITIES OF zrk_i_pur_req_h IN LOCAL MODE
ENTITY _PRHead BY \_PRItem
ALL FIELDS WITH CORRESPONDING #( keys )
RESULT DATA(lt_pur_req_item).
CHECK lt_pur_req_item IS NOT INITIAL.
LOOP AT lt_pur_req ASSIGNING FIELD-SYMBOL(<fs_pur_req>).
APPEND INITIAL LINE TO result ASSIGNING FIELD-SYMBOL(<fs_result>).
" If it is create operation, then %cid needs to be used instead of %tky
<fs_result>-%tky = <fs_pur_req>-%tky.
<fs_result>-%param-description = |Created from { <fs_pur_req>-ObjectId }|.
<fs_result>-%param-buyer = COND #( WHEN <fs_pur_req>-Buyer IS NOT INITIAL
THEN <fs_pur_req>-Buyer
ELSE sy-uname ).
" Default the company code from the user attributes in the Org structure
<fs_result>-%param-Company_code = zrk_cl_mng_pur_con=>determine_company_code( ).
" Calculate the validity dates
<fs_result>-%param-valid_from = cl_abap_context_info=>get_system_date( ).
<fs_result>-%param-valid_to = cl_abap_context_info=>get_system_date( ) + 365.
" Take the first supplier from the requisition item
LOOP AT lt_pur_req_item ASSIGNING FIELD-SYMBOL(<fs_item>)
WHERE Supplier IS NOT INITIAL.
<fs_result>-%param-supplier = <fs_item>-Supplier.
EXIT.
ENDLOOP.
" If the supplier is not assigned to any item in PR, then determine from source of supply
IF <fs_result>-%param-supplier IS INITIAL.
<fs_result>-%param-supplier =
zrk_cl_mng_pur_con=>determine_supplier_material( iv_material = VALUE #( lt_pur_req_item[ 1 ]-PartNo ) ).
ENDIF.
ENDLOOP.
ENDMETHOD.
use function GetDefaultsForConvert_Into_PC ;
<Annotations Target="SAP__self.Convert_Into_PC(SAP__self.PRHeadType)">
<Annotation Term="SAP__core.OperationAvailable" Path="_it/__OperationControl/Convert_Into_PC"/>
<Annotation Term="SAP__common.DefaultValuesFunction" String="com.sap.gateway.srvd.zrk_ui_pur_req.v0001.GetDefaultsForConvert_Into_PC"/>
</Annotations>
ABAP RAP Defaulting action parameters with complex business logic empower Fiori Elements applications to be not just user-friendly but also intelligent and adaptable to the dynamic nature of business processes. The ability to customize defaulting actions according to specific business needs ensures a more seamless and efficient user experience.
https://help.sap.com/docs/abap-cloud/abap-rap/operation-defaulting
https://community.sap.com/topics/abap/rap
For more similar content, Follow me on community or LinkedIn