ERPClimb logoERPClimb
Managed RAP
RAP / CDS / ODataintermediate 16 min read

Managed RAP Part 4: Compositions, Create-by-Association and Draft

Preview · a free account unlocks the full lesson

Key takeaway

Understand the composition tree behind a RAP business object, create items through their parent with create-by-association and %cid ref, and enable draft so users can edit requests over a longer session safely.

In this lessonShow sections

Visual overview

Managed RAP Part 4: Compositions, Create-by-Association and Draft — learning sheetIn one line: Understand the composition tree behind a RAP business object, create items through their parent with create-by-association and %cid_ref, and enable draft so users can edit requests over a longer session safely.ERPClimb.com / RAP / LESSON 4/5 / LEARNING SHEETManaged RAP Part 4: Compositions, Create-by-Associationand Draft1 · IN ONE LINEUnderstand the composition tree behind a RAP business object, create items through theirparent with create-by-association and %cid_ref, and enable draft so users can edit requestsover a longer session safely.2 · KEY IDEAS EXPLAINEDThe Composition TreeIn the root CDS view: "composition [0..*] ofZR_PurchReqItem as _Item".In the child CDS view: "association to parentZR_PurchReq as _Request on ...".Create-By-Association%cid — a temporary ID you give the new header,e.g. 'REQ1'.%cid_ref — used on the items to say "myparent is the instance with %cid REQ1".Why DraftEdits go to draft tables, not to the activetables. The user can leave and resume later, evenon another device.Validations and determinations can run whileediting, so messages appear early.How To Enable Draft — Step By Step1. Add "with draft;" after strict ( 2 )in the BDEF.3 · DO · AVOIDBest practice!Common mistakeUse composition only for data with the samelifecycle; everything else is an association.Always create children viacreate-by-association and link new instances…Plan for draft from the start: use %tky and INLOCAL MODE in every method.✕Modeling a reference (e.g. supplier, material)as a composition. Composition means ownership…✕Declaring composition in the root butforgetting "association to parent"…✕Giving the child its own "create"instead of creating it via the parent…4 · IN A REAL PROJECTA service-request app was first built without draft. Users entering long requests with 30+ items lost workwhenever their session timed out, and validations fired on every field change because each change was savedimmediately. Some users also opened the same request twice and overwrote their own changes. Enabling draft…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Items can only be created through the parent, declared with association _Item { create; }. Enabling draft requires several pieces together: with draft, draft tables, a total etag, draft actions, and "with draft" added to the relevant associations. Code should always use %tky so it works correctly for both draft and active instances, and draft tables need to be regenerated after adding any new fields.

The Purchase Request already has numbering, locking, authorization and a workflow by this point. This part looks closely at the header-item relationship created in Part 1, covers how to create items correctly, and enables draft — the feature that makes a RAP app behave like a real Fiori application.

A RAP business object isn't a single table; it's a tree of entities. The root, PurchaseRequest, owns its children, the Items. This ownership has to be declared on both sides. In the root CDS view, it's "composition [0..*] of ZR_PurchReqItem as _Item." In the child CDS view, it's "association to parent ZR_PurchReq as _Request on ...".

Composition brings several things along with it: lifecycle, meaning deleting a request automatically deletes its items; locking, via "lock dependent by _Request," so editing an item locks the whole request; authorization, via "authorization dependent by _Request," so item operations are checked against the parent; draft, meaning the whole tree is drafted and activated together; and one transaction, meaning header and items are saved in a single LUW, all or nothing.

Composition should only be modeled when the child truly belongs to the parent — a request item belongs to its request, but a supplier doesn't belong to a request, which is why that relationship is a normal association instead.

Deeper trees are allowed, such as root to item to schedule line, with every level using association to parent, and lock/authorization dependency pointing back to the root through the whole chain.

Items have no standalone "create" operation in the BDEF — they can only be created through the parent, via "association _Item { create; }," which guarantees every item always gets a valid parent key. In EML this is written as CREATE BY \_Item.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Code example
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.