ERPClimb logoERPClimb
CAP
BTP & Integrationadvanced 5 min read

BTP CAP Part 6: Service Handlers — Before, On, After and Transactions

Preview · a free account unlocks the full lesson

Key takeaway

Add business logic the CAP way: event handlers in before, on and after phases, validations with req. error/req. reject, enriching results, calling other services and remote APIs, transaction handling with cds. tx, and avoiding the anti-pattern of replacing generic handlers.

In this lessonShow sections

Visual overview

HANDLERSThe CAP request lifecycleWhere to put which logic1Request arrivesAuthorized by annotationsRequest2before handlersValidate, default, rejectbefore?Input valid?req.reject / req.errorNo → 400 with messageYes3on handlerGeneric provider or custom actiononafter handlersEnrich results; commitafterTip: don't replace generic READ — enrich in after handlers to keep paging andfilters.

Explanation

Part 5 modeled the domain; this part covers the logic layer — before, on and after handlers, and how CAP manages transactions. A before handler validates or modifies the incoming request; an on handler implements, or replaces, the actual operation; an after handler enriches or adjusts the result before it goes back to the caller.

Validation errors should be raised with req.error or req.reject, which also let you set the correct HTTP status code rather than a generic failure. Every request runs inside its own transaction automatically, while cds.tx gives explicit control over transactions for background work that isn't tied to a single incoming request.

Remote services are consumed by connecting to them with cds.connect.to and then querying them with CQL, the same query language used for local entities.

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.