ERPClimb logoERPClimb

Code examples · ABAP RAP · Intermediate

RAP parent and child: a sales order with items (composition)

The problem

A sales order has a header (customer) and any number of items (product and quantity).

How we solve it

  1. 1Two tablesheader + item
  2. 2Two viewscomposition
  3. 3Behaviorlock dependent
  4. 4Deep createEML

The result

A header with two items is created in one request and saved in one transaction.

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

1. The requirement

A sales order has a header (customer) and any number of items (product and quantity). Items cannot exist without their order, are locked together with it, and must be created, saved and deleted as one unit.

2. How we design it

Two tables and two CDS views. The header view is the root and declares a composition to the items. The item view declares an association to its parent. In the behavior definition the header is the lock and authorization master and the item depends on it. The framework then keeps header and items in one transaction. We test it with one EML statement that creates both levels at once.

Why we decided it this way

  1. 1Composition, not a plain associationA composition says the items belong to the header: deleting the order deletes the items, and the lock is shared.
  2. 2Item key is its own UUID, with the header UUID as a normal fieldManaged numbering fills the item key. The header UUID is passed down by the framework.
  3. 3Lock and authorization dependent on the headerOne lock and one authorization rule for the whole order, defined once at the root.

Before you start

  • ADT and a package with a transport or $TMP. The tables below are new.

3. The code, step by step

Step 1 of 4: Create the two tables

Header and item tables with the fields the framework needs.

CDS · Dictionary tables · ZSO_HEAD and ZSO_ITEM
// Header table
@EndUserText.label : 'Sales order header'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zso_head {
  key client         : abap.clnt not null;
  key so_uuid        : sysuuid_x16 not null;
  customer           : abap.char(20);
  created_by         : abp_creation_user;
  created_at         : abp_creation_tstmpl;
  last_changed_by    : abp_lastchange_user;
  last_changed_at    : abp_lastchange_tstmpl;
  local_last_changed : abp_locinst_lastchange_tstmpl;
}
// Item table
@EndUserText.label : 'Sales order item'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zso_item {
  key client         : abap.clnt not null;
  key item_uuid      : sysuuid_x16 not null;
  so_uuid            : sysuuid_x16;
  product            : abap.char(20);
  quantity           : abap.int4;
  local_last_changed : abp_locinst_lastchange_tstmpl;
}
Part by part (plain text)
  1. Header table. The UUID is the key. The administrative fields (created, changed) are filled by the framework because the CDS view marks them with @Semantics annotations. LOCAL_LAST_CHANGED is used for the ETag.
  2. Item table. The item has its own UUID key and a field for the header UUID, which links it to its order.

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

A header with two items is created in one request and saved in one transaction. Deleting the header removes the items too.

What you should see

EML run: mapped header 1 and mapped items 2. After COMMIT ENTITIES both items exist in table ZSO_ITEM under the new header. Delete the header: its items are gone.

Try it and check the result

  • Activate tables, views and the behavior definition. Create the behavior class with the quick fix (Ctrl+1) on zbp_i_sohead; it generates the empty get_global_authorizations method. Activate it, then run ZCL_SO_DEEP_CREATE with F9: 'mapped header 1, mapped items 2'.
  • Open the table ZSO_ITEM in Data Preview: two rows with the same SO_UUID as the new header in ZSO_HEAD.
  • Delete the header with a second EML MODIFY (DELETE FROM VALUE #( ( %key-SoUuid = ... ) )) and COMMIT ENTITIES: the items disappear with it.

Common mistakes

  • Forgetting 'association _Items { create; }' in the header behavior makes the CREATE BY \_Items statement a syntax error in the calling class.
  • The item's own behavior must not repeat 'lock master'. A dependent entity follows its header.
  • The ON condition of the parent association must compare the header UUID with the field that stores it in the item, or the view will not activate or the items will not be found.
  • To show header and items in a Fiori app, the projection views and projection behavior also need the composition. This example stops at the business object.

Keep learning