ERPClimb logoERPClimb
Service projections and OData
SAP Cloud Application Programming Modelbeginner 5 min read

Service projections and OData — SAP Guided Tutorial

Free sample lesson · open to everyone

Key takeaway

How CAP services expose a deliberate API over the domain model using projections, read-only and excluded elements, actions and functions — and how to read the resulting OData V4 $metadata.

In this lessonShow sections

Visual overview

Service projections and OData — learning sheetFlow: Domain model: db → Service: projections → Actions: bound / unbound → $metadata: the contract. Key ideas: SERVICES AND PATHS; PROJECTIONS; ACTIONS AND FUNCTIONS; READING THE CONTRACT. Watch out: Exposing every db entity 1:1 freezes your table design into the API and leaks internal fields.ERPClimb.com / CAP / LESSON 3/12 / LEARNING SHEETService projections and OData1 · HOW IT FLOWS1Domain modeldb2Serviceprojections3Actionsbound / unbound4$metadatathe contract2 · KEY IDEAS EXPLAINEDServices And Paths`service CatalogService @(path: '/browse') { ...}` — served at /odata/v4/browse by default (pathprefix depends on CAP version and protocol…One service per consumer type: a CatalogServicefor shoppers, an AdminService for back office.Each gets its own authorization.Projections`@readonly entity Books as projection on my.Booksexcluding { createdBy, modifiedBy };`Rename and flatten: `author.name as authorName` —consumers do not need to $expand.Actions And FunctionsUnbound: `action submitOrder(book : Books:ID,quantity : Integer) returns { stock : Integer };`Bound: `entity Orders as projection on my.Ordersactions { action cancel(reason : String); }` —called on /Orders(ID)/CatalogService.cancel.Reading The Contract`cds compile srv --to edmx` or GET /$metadatashows entity sets, navigation properties, actionsand annotations.Check that keys, types and nullability are whatconsumers expect before a UI is built on top.3 · REMEMBER · WATCH OUTRememberA service is the public API; the domain model is internal. Exposeprojections, not raw tables.`entity Books as projection on my.Books { ID, title, author.name asauthor }` flattens, renames and limits fields.`@readonly`, `@insertonly`, `excluding { ... }` and`@cds.redirection.target` control what clients can do.!Watch outExposing every db entity 1:1 freezesyour table design into the API andleaks internal fields.4 · IN A REAL PROJECTAn admin UI and a public catalog shared one service exposing Orders directly. Public users could see createdBye-mail addresses and could even PATCH stock. Splitting into a read-only CatalogService (projection withoutaudit fields) and an AdminService with @requires: 'admin' closed both gaps without touching the database…Original ERPClimb learning sheet • Verify against your SAP release and edition

The situation: a real project scenario

An admin UI and a public catalog shared one service exposing Orders directly. Public users could see createdBy e-mail addresses and could even PATCH stock. Splitting into a read-only CatalogService (projection without audit fields) and an AdminService with @requires: 'admin' closed both gaps without touching the database model.

Explanation

A CAP service is the deliberate, public API you choose to expose — the domain model underneath it stays internal, and clients should only ever see projections, not raw tables.

A projection such as entity Books as projection on my.Books { ID, title, author.name as author } can flatten associations, rename fields, and limit what is visible, which is exactly how you avoid freezing your internal table design into a contract that other teams depend on, and how you avoid leaking fields like audit columns that were never meant to be public.

Services are declared with their own path, for example service CatalogService @(path: '/browse') { ... }, served under an OData V4 endpoint whose exact prefix depends on your CAP version and protocol settings.

A good rule of thumb is one service per consumer type — a read-facing CatalogService for shoppers and a separate AdminService for back-office staff — because each service gets its own authorization, and a single service trying to serve every audience tends to either over-expose data or become unworkable to secure.

Annotations like @readonly, @insertonly, and excluding { createdBy, modifiedBy } control what operations and fields a projection allows, and when two projections expose the same underlying entity, @cds.redirection.target tells CAP which projection an association should resolve to, avoiding ambiguous navigation.

Beyond plain CRUD, services expose actions and functions. Actions change data and are called with POST — an unbound one might look like action submitOrder(book : Books:ID, quantity : Integer) returns { stock : Integer };, while a bound action lives on an entity, declared inside actions { action cancel(reason : String); } and called at a URL like /Orders(ID)/CatalogService.cancel.

Functions are the read-only counterpart, called with GET, and are the right tool for calculations or lookups that have no side effects — using a function for something that mutates data, or vice versa, breaks the HTTP semantics clients and tooling rely on.

The resulting contract is visible in $metadata, which you can inspect directly via a GET request or generate offline with cds compile srv --to edmx. It's worth reviewing before a UI gets built against it — checking keys, types, and nullability match what consumers will expect — because once consumers exist, renaming or removing an element is a breaking change; the safer path is to add new fields and, when you truly must break something, version the service.

To practice this: build a CatalogService that exposes Books read-only, excludes the audit fields, and flattens the author's name; add an unbound submitOrder action and a bound cancel action on Orders; then diff $metadata before and after to see the new action imports, and call the bound action to see its URL pattern. You should be able to point at any element in $metadata and say exactly which projection or action in srv/ produced it.

Code example

ABAP Code
using my.bookshop as my from '../db/schema'; service CatalogService @(path: '/browse') {  @readonly entity Books as projection on my.Books {    ID, title, price, currency, author.name as authorName  } excluding { createdBy, modifiedBy };   action submitOrder(book : Books:ID, quantity : Integer) returns { stock : Integer };}

Watch out

Common mistakes

  • One giant service exposing all entities to every user group.
  • Using functions for operations that change data.
  • Renaming service elements without telling consumers — silently breaking UIs and integrations.
  • Forgetting @cds.redirection.target when two projections of one entity exist, causing ambiguous associations.

Do this

Best practices

  • Design services per consumer and role; keep the domain model internal.
  • Mark read-only consumer services @readonly and exclude technical fields.
  • Review $metadata in pull requests for any service change that UIs or integrations consume.

In the interview

Interview angle

Interviewers want to hear that a CAP service is a deliberate API, not a database dump. Talk about projections per consumer, read-only exposure, actions vs functions, and how you treat $metadata as a contract you cannot change casually.

Practice this lesson

Original educational case studies for SAP consultants, not official SAP certification exam questions. Verify product edition and implementation details against the current SAP documentation.

Quick check

0 of 4 questions attempted

Question 1 of 4

Which operation type should a CAP service use for a side-effect-free calculation such as getting a shipping quote?

Practise this in an interview

Answer in your own words and get instant feedback, the way an interviewer would judge it.

1How do you design CAP services for different consumers of the same data?

2When would you use a bound action instead of an unbound action in CAP?

Was this lesson helpful?

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.