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.

Implementation and configuration of an event in RAP only takes a few minutes and is relatively easy and straight forward. furthermore, an event which is implemented in RAP can be called from everywhere, also from legacy coding. To avoid the recurrence, I would skip the introduction part here. For more information and getting know the event enablement and the RAP events, I suggest you look on those blogs with the provided links above.

 Create a RAP Business Event

As a prerequisite, you need to define your RAP BO. If you already have a BO, you can extend it with the appropriate behavior. An event can be either raised inside the BO implementation, one way is during (additional) save as it is shown in the previous blog post, or outside the BO implementation by creating for example with a static method in the global class pool of the behavior implementation class. The latter provide you the possibility to raise this RAP event from everywhere, which is the aim of this blog post.

Create an Event in Behaviour Definition

Let’s create an event which is raised when a booking flight is cancelled. For this, go to the behaviour definition and add a new event to the business object. The event name is ‘BookingCancelled’.

In addition, we want to provide a cancellation reason and description. So, we define an additional parameter as an abstract entity, where we put those definitions.

@EndUserText.label: 'booking cancelation reason'
  define abstract entity ZD_CancelingReason 
{
  ReasonCode : abap.char(2);  
  Description : abap.char(64);
}

This is the code snippet you need to add to the behaviour definition:

event BookingCancelled parameter ZD_CancelingReason;

so the behaviour definition looks like this:

managed implementation in class ZCL_BOOKINGDATA unique; 
strict(2);
define behavior for ZR_BOOKINGDATA alias BOOKING
persistent table ztbooking_data
lock master
authorization master( instance )
{
  create;
  update;
  delete;

  event BookingCancelled  parameter ZD_BOOKINGCANCELATION;

  mapping for ZTBOOKING_DATA
  {
    TravelID = travel_id;
    AgencyID = AGENCY_ID;
    CustomerID = CUSTOMER_ID;
    BeginDate = BEGIN_DATE;
    EndDate = END_DATE;
    BookingFee = BOOKING_FEE;
    TotalPrice = TOTAL_PRICE;
    CurrencyCode = CURRENCY_CODE;
    Description = DESCRIPTION;
    OverallStatus = OVERALL_STATUS;
    CreatedBy = CREATED_BY;
    CreatedAt = CREATED_AT;
    LastChangedBy = LAST_CHANGED_BY;
    LastChangedAt = LAST_CHANGED_AT;
    LocalLastChangedAt = LOCAL_LAST_CHANGED_AT;
 }
}

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

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

If you want the event to be raised out-side the BO implementation you need a wrapper:

Define and implement local class in behavior class pool 

Create and the implement e.g. e local class where the statement raising the event is wrapped

CLASS lcl_event_handler DEFINITION FRIENDS ZCL_BOOKINGDATA.

 PUBLIC SECTION.
CLASS-METHODS on_booking_deleted
      IMPORTING deleted_events TYPE ZCL_BOOKINGDATA=> events_cancelled.
ENDCLASS.

CLASS lcl_event_handler IMPLEMENTATION.

METHOD on_booking_deleted.
    RAISE ENTITY EVENT ZR_BOOKINGDATA~BookingCancelled
      FROM deleted_events.
 ENDMETHOD.
ENDCLASS.

Create static method in global class pool

Create e.g. 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_deleted
      IMPORTING deleted_events TYPE events_cancelled.

protected section.
private section.
ENDCLASS.


CLASS ZCL_BOOKINGDATA IMPLEMENTATION.
  METHOD raise_deleted.
    lcl_event_handler=>on_booking_deleted( deleted_events ).
  ENDMETHOD.

ENDCLASS.

Raise a RAP Event in your legacy code

Now you can raise the event in your legacy code. Here as an example, I wrote a simple report to call the method. I want to inform that a booking with a certain travel ID is cancelled :

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 event is modelled as a part of the behaviour definition (BDEF) and linked to the payload. The key metadata information is derived from the RAP object. Additionally, a RAP binding is mapping the implementation in RAP to the SAP Object Type (SOT), event operation and version information that is used to create the topic which can be consumed in SAP Event Mesh service. 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 rise this event from everywhere and this could save you time and effort.

 

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x