BTP CAP Part 6: Service Handlers — Before, On, After and Transactions
Preview · a free account unlocks the full lesson
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 sectionsHide sections
Visual overview
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.
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
Interview questions on this topic
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.