Introduction
In SAP RESTful Application Programming Model (RAP), determinations are a powerful mechanism that automatically derive or calculate field values based on changes to other fields within a business object. They play a central role in enforcing business logic, keeping data consistent, and reducing manual input errors.
While determinations are commonly implemented using direct field references within the same entity, a more advanced and often necessary pattern involves leveraging associations — navigational links between related business object nodes or external CDS entities. This approach becomes essential when the value you need to derive depends not on the current entity alone, but on data that lives in an associated entity.
This blog walks through the concept and practical implementation of determinations that use associations in SAP RAP.
I have implemented a scenario where the non-editable Total Amount field in the header entity is automatically determined based on the Price and Quantity fields maintained in the associated item entity.
Below is the Header entity, which includes the Total Amount field
Below is the item entity, which includes quantity and price field
In the behavior definition, the Total Amount field is configured as non-editable using feature control, ensuring that it cannot be explicitly modified by the user.
Determination has been defined in the behavior definition of the Item entity.
The determination logic is implemented in the local handler class of the Item entity.
METHOD calculate_total_price.
DATA lt_header_update TYPE TABLE FOR UPDATE zi_nk_header.
*Step 1 : Read changed items
READ ENTITIES OF zi_nk_header IN LOCAL MODE
ENTITY zi_nk_item
FIELDS ( soid quantity price currency )
WITH CORRESPONDING #( keys )
RESULT DATA(lt_items)
FAILED DATA(lf).
DATA lt_header_keys TYPE TABLE FOR UPDATE zi_nk_header.
LOOP AT lt_items ASSIGNING FIELD-SYMBOL(<item>).
*Step 2 :Navigate to parent headers using back-association
READ ENTITIES OF zi_nk_header IN LOCAL MODE
ENTITY zi_nk_item BY \_header
FIELDS ( soid totalamount currency )
WITH VALUE #( ( %tky = <item>-%tky ) )
RESULT DATA(lt_header_result)
FAILED DATA(lf_h).
LOOP AT lt_header_result ASSIGNING FIELD-SYMBOL(<hdr>).
*Step 3 : Read all items under those headers
READ ENTITIES OF zi_nk_header IN LOCAL MODE
ENTITY zi_nk_header BY \_salesitem
FIELDS ( quantity price currency )
WITH VALUE #( ( %tky = <hdr>-%tky ) )
RESULT DATA(lt_all_items)
FAILED DATA(lf2).
*Step 4 : Calculate the total amount
*Here i have used decfloat34 to avoid the loss of decimal values and incorrect totals
DATA(lv_total) = REDUCE decfloat34(
INIT sum = CONV decfloat34( '0' )
FOR <it> IN lt_all_items
NEXT sum = sum + ( <it>-quantity * <it>-price )
).
APPEND VALUE #(
%tky = <hdr>-%tky
totalamount = lv_total
currency = <item>-currency
%control = VALUE #( totalamount = if_abap_behv=>mk-on )
) TO lt_header_update.
ENDLOOP.
ENDLOOP.
*Step 5 : Update the header entity
MODIFY ENTITIES OF zi_nk_header IN LOCAL MODE
ENTITY zi_nk_header
UPDATE FIELDS ( totalamount currency )
WITH lt_header_update
FAILED DATA(lf3)
REPORTED DATA(lr).
ENDMETHOD.
The Total Amount and Currency fields are rendered as read-only, preventing any direct user modification.
In the Item entity, users provide input for Quantity and Price, based on which the Total Amount is determined and automatically updated in the associated Header entity.
Output:-
Conclusion:-
This approach ensures data consistency, reduces redundancy, and keeps the business logic modular and maintainable. It is especially useful in scenarios like calculating header totals based on item data, where changes in one entity automatically reflect in another.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 14 | |
| 13 | |
| 12 | |
| 11 | |
| 10 | |
| 9 | |
| 8 | |
| 8 | |
| 7 | |
| 6 |