RAP Business Events
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
When an expense claim is created, other parts of the company often have something to do: a mail to the manager, an entry in a reporting system, a check in another application. The claim itself should not know about any of them, and it should not wait for them. A RAP business event solves this: the business object announces "a claim was created" once, after the data is safely saved, and anyone who has signed up for that announcement reacts in their own time. This page shows how to define such an event in the behavior definition, how to raise it in the right place of the save, what an event binding is, how a derived event gives an existing event a different payload, and how an event is consumed, in the same system with an event handler class or in another system through SAP Event Mesh. It is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608) and applies to RAP business objects in ABAP Cloud (SAP BTP ABAP environment, SAP S/4HANA Cloud Public Edition) and in SAP S/4HANA on premise. The listings need a RAP business object that exists in an SAP system, so each one requires an SAP system; not run here.
Before you read
-
RAP Overview: what a RAP business object is and where the behavior definition sits.
-
RAP Behavior Definitions: the claim
ZI_ExpClaimand its behavior definition, which this page extends with events. -
Managed, Unmanaged and Unmanaged Save:
with additional save, the saver class and the save sequence, the place where events are raised. -
Entity Manipulation Language (EML): the interaction phase, the save sequence and
COMMIT ENTITIES, which end in the late save phase. -
Classes and Objects: the event handler is a class that inherits from a framework class.
Key terms
-
RAP business event: a record that says a significant change happened to an entity of a RAP business object, so that interested parties can react.
-
Event payload: the information that travels with an event, always the key of the instance and, if the event has a parameter, more fields.
-
Event binding: the development object that maps an event of a business object to a namespace, a business object name and an operation, so that systems outside can subscribe to it.
-
Derived event: an event defined on top of an existing event that has a different payload and needs no code of its own.
-
Event handler class: the class that consumes events of a business object in the same system.
-
Additional save: the addition
with additional save, which keeps the managed save and lets your own code run in the saver class as well. -
SAP Event Mesh: the SAP messaging service that carries events between systems.
Events: telling others without waiting for them
An event-driven design separates two roles. The event provider is the system or business object where something happens. The event consumer is the code that wants to know. The RAP documentation describes the style as publisher and subscriber: the provider triggers the message, the messaging service tells the consumers, and the consumers do the follow-up work. The provider does not know who the consumers are, and there is no answer going back. That is why it is called asynchronous: the provider continues without waiting.
For RAP the documentation names two ways of using an event, which differ only in where the consumer lives.
| Consumption | What happens |
|---|---|
Local |
The consumer is in the same system as the provider. An event handler class receives the event. No service binding and no connection to an event infrastructure is needed. |
Remote |
The consumer is in another system. The event travels through SAP Event Mesh, and it reaches the consumer through queues the consumer has subscribed to. |
An event is the right tool for "something happened, react if you care". It is the wrong tool when the caller needs a result back, for example "is this claim allowed?". That question belongs in a validation or an action of the behavior definition, described on the RAP Behavior Definitions page.
The life of an event
The documentation puts the whole life cycle in one sentence: an event is defined in the behavior definition with the keyword
event, raised in the behavior implementation in the late save phase, and described to the outside by an event binding.
The important rule is the timing. An event is raised after the point of no return of the save sequence, when the change is
final. If it were raised earlier, a consumer could be told about a claim that the save then rejects.
Defining an event in the behavior definition
An event is one line in the behavior definition: the keyword event, a name, and optionally a parameter. The syntax forms in
the Keyword Documentation are
event EventName [[deep] parameter ParamName];
managed event DerivedEventName on RefEvent parameter ViewEntityName;
The first form defines a RAP business event, the second a derived event (a later section). Events can be defined in managed
and in unmanaged business objects. Without a parameter the event carries only the key of the instance it is about. A
parameter adds more information, the payload. In the flat form the parameter must be a CDS abstract entity, a CDS entity
that only describes a structure and reads no table, so the payload has one component per field of that entity. The deep
form takes an abstract behavior definition with a hierarchy instead, so that the payload can contain child entities.
For the running example the claim ZI_ExpClaim gets two events. ClaimCreated has a parameter that tells the consumer which
employee the claim belongs to, and ClaimDeleted has none. The behavior definition of the claim page gets the addition
with additional save, because the documentation recommends raising events in the additional save of a managed object, and
two event lines.
" requires an SAP system; not run here
@EndUserText.label: 'Payload of the event ClaimCreated'
define abstract entity ZA_ExpClaimCreated
{
EmployeeId : abap.numc(8);
}
" requires an SAP system; not run here: the behavior definition of ZI_ExpClaim extended with events
managed with additional save with full data
implementation in class zbp_i_expclaim unique;
strict ( 2 );
define behavior for ZI_ExpClaim alias Claim
persistent table zexp_claim
lock master
authorization master ( global )
{
create;
update;
delete;
association _Items { create; }
event ClaimCreated parameter ZA_ExpClaimCreated;
event ClaimDeleted;
" fields, determinations, validations, actions and mapping as in the behavior definitions page
}
Reading it: event ClaimCreated parameter ZA_ExpClaimCreated; announces an event whose payload is the key ClaimId plus
the field EmployeeId of the abstract entity. event ClaimDeleted; announces an event with only the key. with
additional save with full data in the header makes the framework call the saver method with the full instance data.
The event lines are the design of the event; nothing is sent until the behavior pool raises it. The employee number type
abap.numc(8) matches the column employee_id of ZEXP_CLAIM from
Database Tables and Structures.
Raising an event with RAISE ENTITY EVENT
The statement that raises an event is RAISE ENTITY EVENT ent~evt FROM tab. ent is the entity of the business object and
evt the event defined in the behavior definition. tab is an internal table typed with the derived type for the event;
each row says for which instance the event is raised. A row always contains the key of the root entity (the component
group %key), and, if the event has a parameter, the component group %param as well.
The statement is allowed only in the saver methods of the behavior pool during the late save phase. Anywhere else the
runtime error BEHAVIOR_ILLEGAL_STATEMENT occurs. For a managed business object the documentation recommends the method
save_modified of the additional save, which is why the behavior definition above has with additional save. The method
receives the changes in deep structures named create, update and delete, whose internal tables carry the name of the
entity; this page’s claim has the alias Claim, so the tables are create-claim and delete-claim. Each internal table is
read with an iteration expression (FOR ... IN ..., see
Constructor Expressions), so that one event is raised for every
instance, not just the first.
" requires an SAP system; not run here: local classes in the Local Types of zbp_i_expclaim
CLASS lsc_zi_expclaim DEFINITION INHERITING FROM cl_abap_behavior_saver.
PROTECTED SECTION.
METHODS save_modified REDEFINITION.
ENDCLASS.
CLASS lsc_zi_expclaim IMPLEMENTATION.
METHOD save_modified.
IF create-claim IS NOT INITIAL.
RAISE ENTITY EVENT zi_expclaim~ClaimCreated
FROM VALUE #( FOR <cr> IN create-claim (
%key = VALUE #( ClaimId = <cr>-ClaimId )
%param = VALUE #( EmployeeId = <cr>-EmployeeId ) ) ).
ENDIF.
IF delete-claim IS NOT INITIAL.
RAISE ENTITY EVENT zi_expclaim~ClaimDeleted
FROM VALUE #( FOR <del> IN delete-claim (
%key = VALUE #( ClaimId = <del>-ClaimId ) ) ).
ENDIF.
ENDMETHOD.
ENDCLASS.
The listing follows the shape of the sample in the Keyword Documentation: an IF per kind of change, then
RAISE ENTITY EVENT with an iteration expression that builds one table row per changed instance. The first event passes the
%param group because it has a parameter, the second passes only the key. The class lsc_zi_expclaim redefines
save_modified as the saver class of the managed and unmanaged
page does. If you must raise an event outside the behavior pool, the documentation suggests a local class in the
behavior pool’s global class with a static method that wraps the statement.
Source: Keyword Documentation: RAISE ENTITY EVENT and Keyword Documentation: RAP - event. This listing requires an SAP system; not run here.
Event bindings and releasing events
Defining and raising the event is the runtime part. For a consumer in another system the event also needs a design-time
description, the event binding. The RAP documentation says that the event binding maps the event defined in the business
object to a namespace, a business object name and a business object operation such as modify. The Keyword Documentation
says the same from the other side: once an event has been created and raised, an event binding must be defined to map it to
an event type. An event binding is created as a development object in the ABAP development tools; the creation steps are in
the ADT user guide, not on the RAP pages used for this page.
Two more terms appear around events. An event can be reused in an interface behavior definition with the keyword use event;
the Keyword Documentation says that reusing events in projection behavior definitions is not possible. Before an event can be
consumed it has to be exposed in a RAP business object interface in this way, except when it is added through an extension.
Second, an event can be released for a release contract. The documentation recommends releasing local events for contract C1
(use system-internally), which needs the interface to be released, and remote events by releasing the event binding for
contract C2 (use as remote API). The contracts are explained on the
ABAP Language Versions and Released APIs page.
Derived events: another payload without more code
Sometimes an event exists, but a consumer needs different information. A derived event is defined with reference to an already implemented event and redefines the payload. When the referenced event is raised, the derived event is raised too, and nothing has to be implemented in the behavior pool. The payload is given by a CDS view entity, which has to follow the rules of the documentation: it has no associations, it is flat, and its key fields match the key fields of the entity the referenced event belongs to.
" requires an SAP system; not run here
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Payload of the derived event'
define view entity ZP_ExpClaimContact
as select from ZI_ExpClaim
{
key ClaimId,
EmployeeId
}
" requires an SAP system; not run here: in the behavior definition, next to the event it refers to
managed event ClaimCreatedForHr on ClaimCreated parameter ZP_ExpClaimContact;
The view entity selects only the key and one more field, and the second statement defines ClaimCreatedForHr on
ClaimCreated: whenever ClaimCreated is raised, ClaimCreatedForHr is raised with the data of the view entity. The
documentation adds two cautions: the view entity only selects data, so if it selects nothing no event is raised (important
for events raised after a deletion), and the framework reads the view entity with privileged access, which means that no
authorization checks are made on the payload. Derived events also work in behavior definition extensions, with extra
prerequisites about extensibility and released interfaces.
Source: RAP: Derived Business Events and Keyword Documentation: RAP - managed event. These listings require an SAP system; not run here.
Consuming events
Local consumption with an event handler class
For a consumer in the same system you write an event handler class. It is set up like a behavior pool, a global class that
holds no logic itself plus a local class in the Local Types. The global class uses FOR EVENTS OF followed by the name of the
behavior definition whose events it handles. The local class inherits from cl_abap_behavior_event_handler and has one
method for each event. The event must be exposed in a RAP business object interface that is released for C1.
" requires an SAP system; not run here: the global class
CLASS zcl_expclaim_events DEFINITION
PUBLIC
ABSTRACT
FINAL
FOR EVENTS OF zi_expclaim.
ENDCLASS.
CLASS zcl_expclaim_events IMPLEMENTATION.
ENDCLASS.
" requires an SAP system; not run here: the Local Types of the same class
CLASS lcl_claim_event_handler DEFINITION INHERITING FROM cl_abap_behavior_event_handler.
PRIVATE SECTION.
METHODS on_created FOR ENTITY EVENT created_claims FOR zi_expclaim~ClaimCreated.
ENDCLASS.
CLASS lcl_claim_event_handler IMPLEMENTATION.
METHOD on_created.
LOOP AT created_claims INTO DATA(created_claim).
" created_claim-%key-ClaimId and created_claim-%param-EmployeeId hold the payload
ENDLOOP.
ENDMETHOD.
ENDCLASS.
The method declaration names an importing table (created_claims) and the event, and the table holds one row per instance
for which the event was raised. In the Keyword Documentation the same pattern reads the key and parameter fields of each row
inside a LOOP. Two rules from the documentation: within one handler class an event can be handled by only one method (several
handler classes can handle the same event), and the local event may fail for temporary reasons such as a lock on a draft
instance. The framework then retries up to three times when the handler raises an exception that inherits from
CX_RAP_EVENT_HDLR_ERROR_RETRY, with a delay you specify.
Source: RAP: Business Event Consumption, Keyword Documentation: CLASS, FOR EVENTS OF, RAP Event Handler Class, Keyword Documentation: CL_ABAP_BEHAVIOR_EVENT_HANDLER and Keyword Documentation: Local Consumption of RAP Business Events (Database Table Modification). These listings require an SAP system; not run here.
Remote consumption through SAP Event Mesh
Events raised in another system are consumed from SAP Event Mesh by subscribing to the matching messaging queues. The documentation names as prerequisite a configured communication arrangement for inbound and outbound communication of the product. An event consumption model can consume the remote events that a business object publishes, and the event to be consumed must be exposed in a C2 released event version. Setting up a queue and a consumption model is work in the SAP system and in the SAP Event Mesh service, so this page does not show it; the RAP page on business event consumption links to the steps.
Source: RAP: Business Event Consumption
What older books show, and what has changed
-
Books written before RAP describe business events of the classic world, such as change pointers, workflow events and the business object repository, which are raised and received by different mechanisms. A RAP business event is defined in the behavior definition and raised with
RAISE ENTITY EVENT; it is not the classic event of a class (RAISE EVENT, covered on the Events page), which only works inside one program. -
RAP books and blog posts from the first years show behavior definitions without events or release contracts. Events, local consumption through
CL_ABAP_BEHAVIOR_EVENT_HANDLER, and derived events were added to ABAP over several releases; the Keyword Documentation records the first of them in its ABAP release news for 7.92. If a book does not mention them, use the current documentation instead.
Summary
-
A RAP business event tells other code that something happened to an entity, once the change is saved; the provider does not wait for or know the consumers.
-
You define the event with
event Name [parameter Abstract]in the behavior definition and raise it withRAISE ENTITY EVENT ent~evt FROM tabin a saver method of the late save phase, for a managed object insave_modifiedof an additional save. -
The payload always has the key of the instance, plus the parameter fields if the event has a parameter (a CDS abstract entity).
-
An event binding describes the event for outside systems; events are released for C1 (local use) or C2 (remote use).
-
A derived event,
managed event Name on RefEvent parameter ViewEntity, gives an existing event another payload with no extra code. -
Local consumers use an event handler class (
FOR EVENTS OF,cl_abap_behavior_event_handler); remote consumers subscribe through SAP Event Mesh.
References
-
ABAP RESTful Application Programming Model: Business Events (example)
-
ABAP RESTful Application Programming Model: About Business Events in RAP
-
ABAP RESTful Application Programming Model: Develop Business Events
-
ABAP RESTful Application Programming Model: Derived Business Events
-
ABAP RESTful Application Programming Model: Business Event Consumption
-
ABAP Keyword Documentation: CLASS, FOR EVENTS OF, RAP Event Handler Class
-
ABAP Keyword Documentation: Local Consumption of RAP Business Events (Database Table Modification)