Sub topic 13
Reacting to business events
This chapter assumes you know how a CAP remote service calls a released S/4HANA OData API through a destination, as covered in the previous chapters. It also assumes you know basic OAuth 2.0 concepts. Here you will learn when to react to events instead of polling an API, and how to wire this up in a side-by-side extension.
Why events
A synchronous API call answers the question "what is the state right now?" It works well when a user opens a screen and needs current data. It works poorly when your extension needs to know that something changed. To detect changes with calls alone, you must poll, which means asking again and again on a schedule. Polling wastes calls, adds delay, and puts load on the core system.
Event-driven integration reverses this. The S/4HANA system publishes a business event, such as "business partner changed", to a message broker. Your extension subscribes and reacts when the event arrives. The core system does not know your extension exists, so you can add or remove extensions without touching the core. This fits the clean core principle.
The moving parts
SAP S/4HANA Cloud publishes released business events. You enable them through an event channel binding to a broker, such as SAP Integration Suite, advanced event mesh. Your CAP application consumes events through a messaging service.
Events are notifications, not data
Business events from S/4HANA usually carry a small payload. Typically it holds the event type and the key of the changed object, not the full record. The common pattern is therefore:
- Receive the event.
- Read the key from the payload.
- Call the released API to fetch current data.
This is also safer. Events can arrive late or out of order. Fetching the current state avoids acting on stale data.
Consuming an event in CAP
First, declare a messaging service in package.json. This example uses the broker setup you chose for your landscape. The file-based kind is only for local testing.
{
"cds": {
"requires": {
"messaging": {
"kind": "enterprise-messaging-shared"
},
"[development]": {
"messaging": {
"kind": "file-based-messaging"
}
}
}
}
}Then subscribe in the service handler. This extends the handler from the previous chapter.
const cds = require('@sap/cds')
module.exports = class PartnerReview extends cds.ApplicationService {
async init() {
const bp = await cds.connect.to('API_BUSINESS_PARTNER')
const messaging = await cds.connect.to('messaging')
this.on('READ', 'BusinessPartners', req => bp.run(req.query))
messaging.on('sap/s4/beh/businesspartner/v1/BusinessPartner/Changed/v1', async msg => {
const key = msg.data.BusinessPartner
const partner = await bp.run(
SELECT.one.from('API_BUSINESS_PARTNER.A_BusinessPartner').where({ BusinessPartner: key })
)
console.log('Partner changed:', partner?.BusinessPartnerFullName)
})
return super.init()
}
}The topic string depends on your broker and namespace setup. Check the event name in SAP Business Accelerator Hub.
Common errors
- Assuming exactly-once delivery. Brokers usually guarantee at-least-once delivery. Make your handler idempotent, meaning that processing the same event twice gives the same result.
- Assuming ordering. Do not rely on events arriving in sequence. Re-read the current state.
- Slow handlers. If a handler throws, the broker may redeliver. Handle errors deliberately, and use a dead-letter queue for events that keep failing.
- Using unreleased events. Subscribe only to released events, for the same stability reasons that apply to APIs.
Choosing between the two
| Scenario | Better fit |
|---|---|
| User opens a worklist and needs current data | Synchronous API call |
| Extension must act soon after a change | Event |
| Large one-time data load | Synchronous API call |
| Many extensions need the same change | Event |
Exercise
A business event arrives at your CAP extension twice, and it contains only the business partner key. Why should you fetch the partner from the released API, and what property must your handler have?
Show answer
The payload holds only a key, and events can be late or out of order, so fetching the current state from the released API avoids acting on stale data. The handler must be idempotent, because at-least-once delivery means the same event can be processed more than once without causing a different result.