Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
SafaBahoosh
Product and Topic Expert
Product and Topic Expert
4,256
In the blog series of enterprise event enablement I present to you the possible ways to produce and consume an event in SAP BTP ABAP Environment and SAP S/4 HANA Cloud, also I show you how you can create RAP Business Events in an OP2022 system.

Creation and execution of business events within RAP BOs  have been covered in my previous blog posts. An additional scenario is using business events from non-RAP applications. The particularity here will be that no corresponding RAP BOs are available for these applications.

For such scenario the RAP BO infrastructure can be reused to define business events. For that minimal viable RAP BOs are used as container.

Create a RAP Business Event in minimal RAP BO


First, A managed RAP BO with unmanaged save is needed for the purpose. You will need a CDS view entity (to expose the primary key of the non-RAP applicationo), the behavior definition and the related behavior implementation.

Remark: To avoid the generation of unnecessary ABAP artefacts, do not use the ADT generation wizard to create the minimal viable RAP BO.

An event can be raised inside the BO implementation, for example during (additional) save as it is shown in the previous blog post. In the following scenario, the event will not be raised within the BO behavior implementation logic, but outside in a static method defined in the global class pool of the behavior implementation class. This provide you the possibility to raise this RAP event from anywhere, which is the aim of this blog post.

Create CDS View Entity


Create a root CDS view entity with a stable key. In most cases, a basic view representing your root entity is already available.
@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Mimimal Event Demo Example'
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
serviceQuality: #X,
sizeCategory: #S,
dataClass: #MIXED
}
define root view entity ZR_BOOKINGDATA
as select from ztdatabooking
{
key travel_id as TravelID
}

Create an Event in Behaviour Definition


Now create a minimal RAP BO as shown below and define the event ‘BookingCancelled’ which is raised when a flight booking is cancelled. For this, go to the behavior definition and add a new event to the business object.
managed with unmanaged save implementation in class ZCL_BOOKINGDATA unique; 
strict(2);
with privileged mode disabling NoCheckWhenPrivileged;

define own authorization context { }
define authorization context NoCheckWhenPrivileged { }

define behavior for ZR_BOOKINGDATA alias BOOKING
lock master
authorization master( global )

{
event BookingCancelled parameter ZD_CANCELINGREASON;
}

We activate this BO and with this, we model a new event called ‘BookingCancelled’.

This is the code snippet to add the event to the behaviour definition:
event BookingCancelled parameter ZD_CancelingReason;

 

The event has the parameter ‘ZD_CancelingReason’ to provide a cancellation reason and description. So, For that, we need to define a CDS abstract entity.
@EndUserText.label: 'booking cancelation reason'
define abstract entity ZD_CancelingReason
{
ReasonCode : abap.char(2);
Description : abap.char(64);
}

 

Creation of an Event Binding for Our New Business Event


Now we create the event binding for our newly created business event. This event binding is needed to map the RAP business event to the external event infrastructure. Additionally, the event binding ensures that this event can be linked to SAP Event Mesh later.

To create a new event binding in your package, right click on Business Service > New > Event Binding. Provide a Namespace, the business object name, and the business event topic (business object operation). You can freely choose these names to specify your event.

Next step is to map this event binding to your RAP business event. Click on ‘Add’. Under event items select the version (in future we might have several versions) and pick the business object name and the event name defined in your behaviour definition as it is shown in this following screenshot:


Click Add so the event will be created. Now activate the Event Binding. As you can see in the screenshot, the type (aka topic) is a concatenation of the three attributes (name space, business object, business object operation) followed by the version of the event. So, the wildcard * points to the corresponding event version. This is relevant for addressing the events to the Event Mesh.

Raise a RAP Event


You need a wrapper to raise the event outside the BO behavior implementation.

Define and implement local class in behavior class pool 


Create and implement a local class where the statement raising the event is wrapped
CLASS lcl_event_handler DEFINITION FRIENDS ZCL_BOOKINGDATA.

PUBLIC SECTION.
CLASS-METHODS on_booking_canceled
IMPORTING booking_events TYPE ZCL_BOOKINGDATA=>events_cancelled.
ENDCLASS.

CLASS lcl_event_handler IMPLEMENTATION.

METHOD on_booking_canceled.
RAISE ENTITY EVENT ZR_BOOKINGDATA~BookingCancelled
FROM booking_events.
ENDMETHOD.
ENDCLASS.

Create static method in global class pool


Create a static method in the global class pool of the behavior implementation class.
class ZCL_BOOKINGDATA definition
public
abstract
final
for behavior of ZR_BOOKINGDATA .

PUBLIC SECTION.
TYPES events_cancelled TYPE TABLE FOR EVENT ZR_BOOKINGDATA~BookingCancelled.
CLASS-METHODS raise_cancelled
IMPORTING booking_events TYPE events_cancelled.

protected section.
private section.
ENDCLASS.


CLASS ZCL_BOOKINGDATA IMPLEMENTATION.
METHOD raise_cancelled.
lcl_event_handler=>on_booking_canceled(booking_events ).
ENDMETHOD.

ENDCLASS.

Raise a RAP Event in non-RAP applications


Now you can raise the event in your non-RAP applications. Here as an example, I wrote a simple report to call the method:
REPORT zpm_flight.

ZCL_BOOKINGDATA=>raise_deleted( deleted_events =
VALUE #( ( TravelID = '006' %param = VALUE #( reasoncode ='02' description = 'Cancelled by Customer' ) ) ) ).

Commit work.
cl_demo_output=>display('A RAP event is raised' ).

Send the Event to SAP Event Mesh


After an event is raised, it must be delivered to the SAP Event Mesh to be consumed later. The connection between the system and the SAP Event Mesh is achieved through a so-called channel. The detailed step of creating the channel and SAP Event Mesh configuration are already explained here in the previous blog. After the connection is built, we run the report:


The event will be sent to the SAP EventMesh. We can check in the queue to find the event and consume it:

Summary


A RAP business event is modelled as a part of the minimal RAP BO behavior definition (BDEF) and linked to the payload. The metadata information of the key is derived from the RAP BO. Additionally, an Event Binding enables you to access the event itself outside of an ABAP system on the SAP BTP event mesh. You also need to create and configure your channel in your SAP S/4HANA system. More you can find here .

As you already saw, creating a RAP event is a simple work and won’t take much time but with a big advantage that you can raise this event from everywhere and this could save you time and effort.

 
3 Comments