ERPClimb logoERPClimb

Code examples · SAP BTP · Beginner

SAP BTP CAP: an order with items (composition) and a deep insert

The problem

An order must be created together with its items in one call.

How we solve it

  1. 1Data modelcomposition
  2. 2Serviceprojection
  3. 3Checkbefore CREATE
  4. 4Try itdeep insert

The result

One POST creates an order and its items.

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

1. The requirement

An order must be created together with its items in one call. Items belong to their order, so deleting an order must remove its items too. An item with a quantity of zero or less must be refused.

2. How we design it

In the data model, the order has a composition of items. A composition tells CAP the items are part of the order, so CAP exposes them, accepts them inside one POST (a deep insert), reads them with $expand and deletes them with the order. A small handler adds the business rule that CAP does not know.

Why we decided it this way

  1. 1Composition instead of a plain associationItems are owned by the order: they are created, read and deleted with it.
  2. 2Let CAP expose the itemsA composition target is exposed automatically, so the service stays small.
  3. 3Check items in a before handlerThe rule applies to every item in the request, in one place.

Before you start

  • Node.js 20+ and @sap/cds-dk installed.
  • An empty project: cds init orders-demo, then open it in your editor.

3. The code, step by step

Step 1 of 4: Model orders and items

Two entities, one composition.

CDS · db/schema.cds
// Orders with a composition of items
using { cuid, managed } from '@sap/cds/common';
namespace demo.orders;

entity Orders : cuid, managed {
  customer : String(80);
  items    : Composition of many OrderItems on items.parent = $self;
}

entity OrderItems : cuid {
  parent   : Association to Orders;
  product  : String(60);
  quantity : Integer;
}
Part by part (plain text)
  1. Orders with a composition of items. cuid adds a generated UUID key and managed adds created and changed fields. 'Composition of many OrderItems on items.parent = $self' says the items belong to the order and are linked through their parent field. The item has the matching 'parent' association.

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

One POST creates an order and its items. A GET with $expand returns both. A bad quantity is refused with an error.

What you should see

POST with two items: 201 and the new order with its items. GET /orders/Orders?$expand=items returns the order and both items. POST with an item quantity of 0: 400 with the message 'Quantity must be positive'. DELETE of the order removes its items.

Try it and check the result

  • Run cds watch. CAP deploys the model to an in-memory SQLite database and prints the service URL.
  • Send the three requests in order. The first returns 201, the second shows both items inside the order, the third returns 400.
  • Send DELETE http://localhost:4004/orders/Orders(<id>) with the id from the first answer, then read the items again: they are gone with the order.

Common mistakes

  • A plain Association to the items does not give you deep insert or cascading delete. Use Composition for parts that belong to the parent.
  • Check items one by one. A request can hold many items and one bad item must fail the whole request.
  • With the default in-memory database the data is gone after a restart. Add a persistent database before real use.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: SAP Cloud Application Programming Model with Node.js, local development (cds watch) with SQLite. Check on your @sap/cds version.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.