Lesson 4 of 5
Business events
10 min
Listen to this lesson
When a fleet customer's core return agreement is closed, three things should happen elsewhere: finance refunds any remaining deposit, the customer portal shows the agreement as closed, and the returns warehouse stops expecting cores. The old ECC solution called each of these directly from the save logic. Every new consumer meant changing the agreement code, and a failure in one consumer could block the save. RAP business events decouple the provider from its consumers.
What a RAP business event is
SAP defines a RAP business event as a data record that expresses a significant change in the state of a RAP BO entity. Interested parties subscribe and process it; the provider does not know or wait for them. The payload always includes the instance key and can carry more information through a parameter. SAP's documentation currently allows events only in the root entity of a managed or unmanaged BO; they can be added in BDEF extensions and reused in interface BDEFs.
- DeclareIn the BDEF: event agreementClosed parameter ZD_CMC_AgreementClosed (a CDS abstract entity with customer, refund amount and currency)
- RaiseRAISE ENTITY EVENT in the save phase, recommended in save_modified with additional save for managed BOs
- BindAn event binding maps the event to an event type that consumers can subscribe to
- Consume locallyAn event handler class in the same system processes it asynchronously
- Consume remotelyConfigured event channels pass it to an event broker for other systems and BTP apps
Declaring and raising
In the behavior definition, Kenji declares event agreementClosed parameter ZD_CMC_AgreementClosed;. A flat parameter must be a CDS abstract entity; a deep parameter uses an abstract behavior definition with hierarchy for nested payloads. Because the BO is managed, he adds with additional save and raises the event in the saver's save_modified method, only for agreements whose status changed to CLOSED in this transaction: RAISE ENTITY EVENT zr_cmc_coreagreement~agreementClosed FROM ... with the key and the payload values.
Raising in the save phase matters. An event raised earlier, during interaction, could announce a change that is later rejected by a validation or discarded with the draft. Raised during save, it describes something that was actually committed.
| Variant | Syntax idea | Use |
|---|---|---|
| Business event | event name [parameter Entity] | Announce a significant state change to any subscriber |
| Deep payload | event name deep parameter AbstractBDEF | Send header and items in one event |
| Derived event | managed event name on baseEvent parameter Entity | Same trigger, different payload for a different audience; raised automatically with the base event |
| Event for side effects | event name for side effects | Only to trigger a UI side effect when something changes in the background |
Event variants in SAP's behavior definition language. Check availability for your release.
Consuming events
Inside the same system, a RAP event handler class is declared FOR EVENTS OF the BO, with a local class inheriting from CL_ABAP_BEHAVIOR_EVENT_HANDLER whose methods are declared FOR ENTITY EVENT. SAP's cheat sheet notes that these handler methods are called asynchronously, and that database changes in a handler need the transactional phase handled correctly (class CL_ABAP_TX). For other systems and side-by-side apps, the event binding gives the event a type, and SAP's event infrastructure forwards it through configured channels to an event broker, such as the Event Mesh capability of SAP Integration Suite (formerly the standalone SAP Event Mesh) or SAP Integration Suite, advanced event mesh. In SAP S/4HANA Private Edition and on-premise the channel is set up in the Enterprise Event Enablement framework (transaction /IWXBE/CONFIG); the exact setup depends on your release and landscape.
What would you do?
Dan, the finance process owner, wants refunds to start whenever an agreement is closed. A developer proposes calling the refund BAPI directly from the agreement's save_modified method. What should Kenji recommend?
- A RAP business event announces a significant state change; consumers subscribe and process it independently.
- Declare it in the root's BDEF, optionally with a CDS abstract entity parameter; raise it with RAISE ENTITY EVENT at save.
- An event binding maps the event to an event type; local handlers run asynchronously.
- Design consumers to be idempotent and monitored; events do not make consumers part of the save.
Check your understanding · not graded
Lesson check
1. Where does SAP recommend raising RAP business events in a managed BO?
2. What must a flat event parameter refer to?
3. What is needed after an event is created and raised so consumers can subscribe?
4. Why should event consumers be idempotent?