How the Order Approval Portal exposes its model (CR2): service definitions and projections, auto-exposure and redirection, protocols and endpoints (OData V4, REST, OData V2 adapter, GraphQL), everything generic providers serve without code, generic input validation, and how to call the services locally.
Part 1 – Why services: one model, many APIs
Requirements on this page
CR-ID | Requirement | Where on the page |
|---|---|---|
CR2 | Reps and managers get different APIs on the same data | Part 1 (idea), Part 2 (projections), Part 3 (protocols and endpoints) |
CR2 | No code for standard reads and writes | Part 4 (generic providers) |
CR3 (declarative part) | Reject bad input, e.g. a rejection without a reason | Part 5 (annotations); coded checks on 04 · Custom Logic in Node.js |
The domain model (02 · CDS Modeling) says what exists. A service says who may see and do what, through which API. Reps need to create requests and add notes for their region; managers need a slimmer view of open requests plus Approve / Reject. Instead of one big API with if-statements, CAP encourages one service per use case / persona, each a facade of projections on the same entities – the same idea as RAP projection views and service definitions on top of the interface layer.

|
- The service name decides the path: lower case, trailing
Serviceremoved →/odata/v4/rep,/odata/v4/manager. - Authorization (
@requires,@restrict) is explained once on 07 · Authorization & Security; it appears here only to show where it sits.
Part 2 – Service definitions and projections
Entities in a service are usually projections (views) on domain entities. A projection can rename, hide, add calculated columns, filter rows and change annotations – without copying data.
Construct | Example | Effect |
|---|---|---|
Plain projection | entity ApprovalRequests as projection on my.ApprovalRequests; | All elements, all rows; writable |
Select columns / alias | as projection on my.ApprovalRequests { ID, salesOrder, status } | Only these elements in the API |
excluding | { * } excluding { createdBy, modifiedBy } | All but the listed elements |
Path flattening | { *, customer.name as customerName } | Read-only column from an association |
Row filter | as select from my.ApprovalRequests where status = 'N' | View with a WHERE – read-only unless CAP can resolve it for writes |
| @readonly entity RequestTypes as projection on …; | Only READ / only CREATE accepted (generic 405 otherwise) |
Actions and functions |
| Custom operations; implemented on 04 · Custom Logic |
Events | event OrderApproved : { salesOrder : String(10); } | Declared payload for messaging (10 · Messaging) |
2.1 Auto-exposure and redirection
- Auto-exposure: when an exposed entity has a composition (notes, decisions), CAP exposes the children automatically. Targets annotated
@cds.autoexpose(code lists from@sap/cds/commonare) are added read-only when an exposed entity points to them. - Auto-redirection: associations in a projection are redirected to the projection of the target in the same service –
RepService.ApprovalRequests.notespoints toRepService.Notes, not to the domain entity. - If there are two projections on the same target in one service, CAP cannot choose: redirect explicitly with
redirected toor mark one with@cds.redirection.target.
|
Common mistakes: exposing the domain entity directly with every technical field; one "god service" for all personas (authorization becomes a maze); forgetting that a projection with a WHERE clause may no longer be writable.
Part 3 – Protocols and endpoints: OData V4, REST and more
A service is protocol-neutral. Protocol adapters serve it: OData V4 is the default, REST is built in, GraphQL comes as the open-source plugin @cap-js/graphql. Annotations choose protocol and path.
|
Protocol | Default path | When to use |
|---|---|---|
OData V4 | /odata/v4/<service> | Fiori elements, SAP Build Apps, anything that wants |
REST | /rest/<service> | Simple JSON clients, webhooks, other teams' microservices |
OData V2 | via the community adapter | Only for old UI5 apps or tools that need V2 |
GraphQL |
| Clients that prefer GraphQL; Node.js only, plugin still 0.x |
Internal | @protocol: 'none' | Services only called from other services |
3.1 API documentation for free
|
Interview tip: "Why OData V4 and not plain REST?" – Fiori elements and many SAP tools need OData metadata and annotations; querying ($filter, $expand, $search, paging) comes standardised, and CAP serves REST in parallel if another consumer needs it.
Part 4 – Generic providers: served out of the box
As soon as an entity is in a service, CAP's generic handlers answer CRUD requests on it – no code. They translate OData queries into CQL and SQL, apply managed fields, defaults and validation, and run everything in one transaction. Custom handlers only add to or replace this behaviour (04 · Custom Logic in Node.js).

Feature | What CAP does | How you influence it |
|---|---|---|
CRUD | READ, CREATE, UPDATE (PATCH / PUT), DELETE on every exposed entity |
|
Deep read / write |
| Model compositions (02 · CDS Modeling) |
Auto-generated keys | UUID keys filled on CREATE | aspect |
Managed data | createdAt / By, modifiedAt / By | aspect |
Search |
|
|
Paging | Server-side limit, |
|
Sorting | Implicit sorting by key for stable paging | $orderby |
Optimistic locking | ETag in responses, | annotate ApprovalRequests with { modifiedAt @odata.etag } |
Input validation | @mandatory, @readonly, @assert … (Part 5) | Annotations |
Localized data | Texts in the user's language |
|
Draft | Edit / save / discard choreography for Fiori |
|
4.1 Paging limits in practice
|
Common mistakes: writing a READ handler that re-implements filtering and paging (you lose $filter, $search, paging and authorization filters); expecting more than the maximum page size in one response; no ETag on entities edited by several people.
Part 5 – Generic input validation
Input checks are generic features too. Declare them as annotations on the service entities (or on the domain entities, from where they are inherited by every projection). CAP checks them on every CREATE and UPDATE, rejects the request with a message and rolls the transaction back. Only rules that need logic go into code.

Annotation | Checks | Example |
|---|---|---|
@mandatory | Value present and not empty | salesOrder @mandatory |
@readonly | Client values silently ignored on CREATE / UPDATE | status @readonly |
@assert.range | Numbers and dates within [min, max]; open intervals | netAmount @assert.range: [(0), _] |
@assert.format | Regular expression for strings | salesOrder @assert.format: '^[0-9]{1,10}$' |
@assert.target | Target of a managed to-one association exists | type @assert.target |
@assert: (case … end) | Any condition in CDS expression language, also across fields and associations; pushed down to the database | see below |
|
@assert: (case … end)constraints (capire, Declarative Constraints) are collected into one validation query that runs on the database after the INSERT/UPDATE; if a branch returns a message, the request is rejected and the transaction rolled back.- Messages can be plain text or keys from
_i18n/messages.properties(ori18n.properties); custom texts for the classic annotations via@mandatory.message,@assert.range.message,@assert.format.message. - For Fiori, CAP adds the
targetso the message appears at the field. During draft editing the checks produce messages but keep the draft (06 · Fiori). @assert.integrity/ database constraints protect referential integrity against programming errors – not meant for end-user messages.
The coded part of CR3 – "set status and deadline automatically", checks that need other services – is on CAP › 04 · Custom Logic in Node.js.
Part 6 – Trying a service: index page, .http files, curl
Three ways to call RepService locally (after cds watch):
|
|
miais a mock user you define with roleRep(07 · Authorization); mock users need basic authentication with an empty password locally.- The index page at
http://localhost:4004lists every service and entity with links to data,$metadataand the Fiori preview. - Automated tests with
cds.testare on 12 · Testing; calling one service from another in code (cds.connect.to) on 04 · Custom Logic.
Part 7 – Interview questions
Q1. Why define several services on one domain model?
Each service is a facade for one persona or use case: it exposes only the needed entities and fields, carries its own authorization, actions and annotations, and can evolve independently. The domain model stays free of UI and access concerns.
Q2. What does "as projection on" give you compared with copying the entity?
A view, not a copy: you can select, rename, exclude and flatten elements, filter rows and add annotations, while data stays in one table. Associations are auto-redirected to projections in the same service, and simple projections stay writable.
Q3. What are generic providers?
CAP's built-in handlers that serve CRUD, deep reads and writes, search, paging, sorting, ETags, managed fields, defaults, validation, localized data and draft for every exposed entity without code. Custom handlers add logic before, on or after them.
Q4. How do you serve a service as REST instead of OData, or at another path?
@protocol: 'rest' (or the shortcut @rest) – served at /rest/<name>; @protocol: ['odata', 'rest'] serves both; @path: 'approvals' changes the path; @protocol: 'none' makes it internal.
Q5. How does CAP page results and how do you change the limits?
Implicit server-side paging with @odata.nextLink; the maximum page size is 1000 by default. Change it with cds.query.limit.default / .max in the configuration or @cds.query.limit on a service or entity.
Q6. How does optimistic locking work in CAP?
Annotate an element (usually modifiedAt) with @odata.etag. Responses carry the ETag; clients send If-Match; if the row changed meanwhile, CAP answers HTTP 412 Precondition Failed.
Q7. Which validations can you declare instead of coding?
@mandatory, @readonly, @assert.range, @assert.format, @assert.target, and in newer versions @assert with case expressions for cross-field and cross-entity rules pushed down to the database. Messages can be i18n keys; Fiori shows them at the field.
Q8. What is auto-redirection and when do you need @cds.redirection.target?
Associations in service projections are redirected to the projection of their target in the same service. If two projections exist for the same target, CAP cannot choose – mark one with @cds.redirection.target or use redirected to explicitly.
Q9. Scenario: a rep reports that the list stops after 1000 requests. What happened?
The client ignores @odata.nextLink and only reads the first page; CAP's default maximum page size is 1000. Fix the client to follow the next link (Fiori does), or raise / lower the limit deliberately with @cds.query.limit – never return unbounded result sets.
Q10. Scenario: managers must see open requests with the customer name, but must never change a request directly – only through Approve / Reject. How do you model ManagerService?
A projection on ApprovalRequests with customer.name as customerName and without technical fields, plus bound actions approve() and reject(reason). Then restrict it to @restrict: [{ grant: ['READ', 'approve', 'reject'], to: 'Manager' }] (07 · Authorization): generic UPDATE and DELETE are not granted, so the status changes only through the actions (04 · Custom Logic). Plain @readonly is not enough – it equals grant: 'READ' and would block the actions too.
🏠 Start Here · ◀ 02 · CDS Modeling · 04 · Node.js Logic ▶ · Sections: CORE ABAP | OOPS ABAP | ABAP on HANA | Clean Core | RAP | ABAP Points | SAP Fiori | SAPUI5 | Mobile Development (MDK) | BTP Foundations | BTP Development | CAP | SAP Build | ABAP on BTP | Integration Suite | SAP Business AI | SAP BASIS | Security | SAP SDLC
Functional: FICO ABAP | TRM ABAP | VIM | SAP BIS | SD ABAP | MM ABAP | PP ABAP | QM ABAP | PM ABAP | PS ABAP | EWM | TM ABAP | EHS ABAP | HR ABAP | BW | SAP MDG | SAP Analytics Cloud | SAP Ariba | SAP CX | SAP IBP