What is CDS Comopsition Tree ? (Parent Child Relationship)
A composition tree consists of: Root Entity , Child Entity and Leaf Entity. refrence blog
//// Root Entity - Defines the COMPOSITION pointing to the child.
define root view entity Z_I_SalesOrder
as select from zsales_table
composition [0..*] of Z_I_SalesOrderItem as _Item
{
key order_id,
customer_id,
_Item // Exposes the composition
}
//// Child Entity - Defines the ASSOCIATION TO PARENT pointing back to the root.
define view entity Z_I_SalesOrderItem
as select from zitem_table
association to parent Z_I_SalesOrder as _SalesOrder
on $projection.order_id = _SalesOrder.order_id
{
key order_id,
key item_id,
product_id,
_SalesOrder // Exposes the parent
}
What is the difference between Composition and Association ?
A composition is a strict “part-of” relationship where a child entity cannot exist without its parent and is deleted automatically alongside it. An association is a looser “has-a” link where entities are independent
What is additional Binding ?
Additional bindings are defined in metadata extensions using the annotation @Consumption.valueHelpDefinition and can be set to work in one of three directions based on the usage property: FILTER , RESULT , FILTER_and_RESULT both. Refrence RAP blog
In this example, selecting a Connection ID automatically fills in the corresponding Flight Date, Carrier ID, and Price using the #RESULT
@Consumption.valueHelpDefinition: [
{
entity: { name: '/DMO/I_Flight', element: 'ConnectionID' },
additionalBinding: [
{ localElement: 'FlightDate', element: 'FlightDate', usage: #RESULT },
{ localElement: 'CarrierID', element: 'AirlineID', usage: #RESULT },
{ localElement: 'FlightPrice', element: 'Price', usage: #RESULT }
]
}
]
ConnectionID;
How to add validation in RAP and display error ?
// Define the validation rule in Bdef
validation validateCustomer on save { create; update; field CustomerID; }
// implement method in LHC
CLASS lhc_travel IMPLEMENTATION.
METHOD validateCustomer.
// 1. Read the entity data passed from the UI
READ ENTITIES OF zr_travel_d IN LOCAL MODE
ENTITY Travel
FIELDS ( CustomerID ) WITH CORRESPONDING #( keys )
RESULT DATA(lt_travel_data).
LOOP AT lt_travel_data INTO DATA(ls_travel).
// 2. Clear out any previous state messages for this field (Crucial for Draft apps)
APPEND VALUE #( %tky = ls_travel-%tky
%state_area = 'VAL_CUSTOMER' ) TO reported-travel.
// 3. Apply your custom validation check
IF ls_travel-CustomerID IS INITIAL OR ls_travel-CustomerID = '000000'.
// 4. Reject the save operation by filling the FAILED structure
APPEND VALUE #( %tky = ls_travel-%tky ) TO failed-travel.
// 5. Create and attach the error message using REPORTED
APPEND VALUE #(
%tky = ls_travel-%tky
%state_area = 'VAL_CUSTOMER' " Identifies this specific error state
%element-CustomerID = if_abap_behv=>mk-on " Highlights field in red on UI
%msg = new_message_with_text(
severity = if_abap_behv_message=>severity-error
text = 'Invalid or missing Customer ID.'
)
) TO reported-travel.
ENDIF.
LOOP END.
ENDMETHOD.