Skip to Content
Technical Articles
Author's profile photo Safa Golrokh Bahoosh

How to Create RAP Business Events in SAP BTP ABAP Environment

This blog is the second post from a blog series of enterprise event enablement and the possible ways to produce and consume an event in SAP BTP ABAP Environment and SAP S/4 HANA Cloud. In the first post, I gave you a rough introduction on the basics of event-based communication. Here, you will see how you can implement a RAP event.

With the release 2208, SAP supports the native exposure and consumption of business events. To consume an event, you can generate ready-to-use event consumption models using the event consumer wizard embedded in ABAP Development Tools for Eclipse. This is explained in the third post of this blog series, consuming an event using the event consumer wizard ,embedded in ABAP Development Tools for Eclipse (ADT).

For exposure, an event can be defined and raised in the behaviour definition of a RAP business object and then published via a corresponding Event Binding. Let us explore this in detail here.

Introduction

Event-driven architecture enables asynchronous communication between an event provider and an event consumer in use cases where no direct response from the event consumer is required. Previously, events could only be defined in SAP S/4HANA with the help of Business Event Enablement (releases 2008 to 2205). However, this solution is not integrated into RAP. Therefore, from release 2208 we offer a RAP integrated solution supporting all RAP qualities like extensibility, documentability, supportability and testability. The solution is offered for customers and partners in on-premise, Steampunk and SAP S/4HANA Cloud (Embedded Steampunk).

A RAP event has the following characteristics at design time:

  • An event is directly defined via RAP behaviour definition (BDEF)
  • Events can be added via extensions (BDEF additional behaviour)
  • The event exposure is represented by a corresponding RAP Event Binding that supports an event specific life cycle incl. API state. Events are part of the BO and have the same life cycle.

At runtime

  • A RAP BO implementation can register an event, which will be raised later shortly before a commit is triggered
  • Dedicated API in ABAP supports event registration
  • The events are forwarded to the central event infrastructure and later propagated asynchronously to the BTP Event Mesh.

Usually, an event with the respective message is raised after a change of the business object has been completed. For example, if a BO entity like a travel or a booking has been created, updated, or deleted. When the changed business object is saved to the database, the event is raised during the SAVE sequence after the point of no return has passed.

How to Create Business Events with RAP

First, we have to define a business event in our behaviour definition, then we will see how to map this implementation detail to the outer business event world using a channel to the Event Mesh system. We also implement a method where we raise the event.

 Prerequisite: Create a Custom RAP Business Object

As prerequisite, you need to define your BO. You can follow exercise1 of  RAP 100 to build the booking application. If you already have a BO, you can extend your own BO with the appropriate logic.

Create an Event in Behaviour Definition

An event can be defined in the behaviour definition of a RAP business object with the key word event *EventName* with or without parameters. There might be various ways to raise an entity event in your RAP business object implementation. Basicly, you can raise an event either inside your BO implementation, i.e. in  “save modified” method or outside the BO implementation by creating e.g. a static method in the global class pool of the behavior implementation class. However, it is recommended to raise events during (additional) save.

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’

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

event BookingCancelled parameter ZD_CancelingReason;

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);
}

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 is linked to SAP Event Mesh.

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 an Event

As said, an event is triggered in a save sequence. So, to raise an event, we implement the logic in ‘save modified’ method of the implementation class:

CLASS lcl_event_handler DEFINITION INHERITING FROM cl_abap_behavior_saver.

  PROTECTED SECTION.
    METHODS save_modified REDEFINITION.

ENDCLASS.

CLASS lcl_event_handler IMPLEMENTATION.

  METHOD save_modified.
    IF delete-booking IS NOT INITIAL.
      RAISE ENTITY EVENT zr_bookingdata~BookingCancelled
    FROM VALUE #( FOR <s_travel> IN delete-booking  ( TravelID = <s_travel>-TravelID  %param = VALUE #( reasoncode ='02'  description = 'cancelled by customer' ) ) ).

The event payload data will include both the key fields of the entity and the fields of the parameter (abstract entity). In case no parameter is specified, the payload only consists of the entity key fields. Here, an event with key field travel ID and given parameter values will be raised as we delete an entry. 

It is also possible to raise the RAP-based event from existing code outside a RAP implementation. So, It is not required to have a managed RAP entity. All that is requiered is a minimal RAP implementation that provides a method that raises the event that can be called from existing non-RAP code.

Send the Event to SAP Event Mesh

After an event is raised, it is delivered to the Event Mesh to be consumed later. The connection between the system and the SAP Event Mesh is achieved through a so-called channel. To this aim, we need to create a communication arrangement in our SAP BTP ABAP environment system using the event mesh instance’s service key and the sap_com_0092 scenario which is built for an event enablement.

Add the Event to an Outbound Channel

By creating a communication arrangement, a channel will be stablished between the BTP system and the event mesh system. Now we need to add the event to the outbound channel. This is done by the outbound configuration. After selecting your channel in ‘Enterprise Event Enablement’ application, click on ‘create’ and find the topic which is generated during the event binding creation.

In the SAP Event Mesh system, create a queue for the selected instance and subscribe it to your topic. You can find the topic by opening the Event Metadata and copy the channel from there.

Run the booking application, select the entry with the chosen ID and delete it. An event will be raised then.

Now we check the Event Mesh service to see if this event has arrived. The event payload data will include both the key fields of the entity and the fields of the parameter. You see the type which has been defined in the event binding. The field ‘data’ of the event payload contains the key of the business object and the event parameter structure.

Summary

A RAP event is modeled as a part of the behaviour definition (BDEF) and linked to the payload. The key 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 Event Mesh service. You can also follow this tutorial to try it yourself.

From release 2022, RAP BO can also be created in S/4HANA On Premise as it is explained in here. Besides, an event which is implemented in RAP can be called from everywhere, also from legacy coding. For more information please refer to this post.

To consume an event, you can generate the event consumption model from the event meta data using an event consumer wizard embedded in ADT. If you just hang tight, I’ll explain this in the next post!

Assigned Tags

      35 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Wolfgang Röckelein
      Wolfgang Röckelein

      Hi Safa Golrokh Bahoosh ,

      thank you for helpful blog.

      One question: are you sure your raise entity event statement is correct? The usage of %param contrasts the docu at https://help.sap.com/docs/BTP/923180ddb98240829d935862025004d6/e23776500eb8463f957134e682b76fba.html/#procedure .

      Regards,

      Wolfgang

      Author's profile photo Safa Golrokh Bahoosh
      Safa Golrokh Bahoosh
      Blog Post Author

      Hello Wolfgan,

      yes as far as i know it is correct. you can raise an event either inside your BO implementation, i.e. in  'save modified' method as it is explaind here or outside the BO implementation by creating e.g. a static method in the global class pool of the behavior implementation class. however, the statement which raise the event stays the same, and it is " RAISE ENTITY EVENT " .

      Hopfully I understand your question correctly and could answer it.

      Regards

      Safa

      Author's profile photo Wolfgang Röckelein
      Wolfgang Röckelein

      Hi Safa,

      thank you for your quick answer. My problem is not the position of the RAISE ENTITY EVENT (and the cited documentation uses BTW the same position) but the way the parameter values are filled.

      The cited documentation does NOT use %param and also the use of TravelID in your statement without TravelID being part of the abstract entity is strange.

      Regards,

      Wolfgang

      BTW providing code only via screenshots is a bit problematic, could you provide your example eg in an abapgit repo? This would make this discussion more easily.

      Author's profile photo Safa Golrokh Bahoosh
      Safa Golrokh Bahoosh
      Blog Post Author

      Hello Wolfgang,

      now i get your point! actually the value you give is not just the fields in the abstract entity. The abstract entity is where you define your parameters and it is not a must. You can use this entity to provide more input to your event message data for further usage.

      The event payload data will include both the key fields of the entity and the fields of the parameter (abs. entity). In case no parameter is specified, the payload only consists of the entity key fields.

      Here i said,  raise an event with key field, the Travel Id = 6 and give these values for the parameter as input in the event message.  You can surely modify it in a way that you get the related ID for each deleted item. i want to keep it simple here.

      The aim is not to show how you can build a RAP application. For this, i linked the exersie you can use to build this application, and there is not much coding needed to raise an event, all you need is defining your abstract entity and implement the save modified method in the implementation class. If it helps, i will add the code snippet here. 

      However, there would be a toturial in regard to this which comes out soon, I will put the link here then and you can try it by yourself 🙂

      Regards

      Safa

      Author's profile photo Wolfgang Röckelein
      Wolfgang Röckelein

      Hi Safa,

      ok with traveIID.

      However you use %param syntax while the cited docu does not use it. What is correct? Or both?

      Still source code in screen shots makes it very difficult to follow your example.

      Regards,

      Wolfgang

      Author's profile photo Safa Golrokh Bahoosh
      Safa Golrokh Bahoosh
      Blog Post Author

      Hi,

      Sure it is fine too. There are different ways to pass values to the parameters.

      The step bystep implementation guide is mainly thought for toturials not blog posts. My suggesstion is, follow the exercise1 of  RAP 100 and build the application, I provided the code snippet which you need to add there for your ease. 

      Regards

      Safa

       

      Author's profile photo Yahor Novik
      Yahor Novik

      Hi Safa,

      Thank you for helpful blog!

      Do you know is it possible to create communication arrangement for App using Trial account?

      KR, Yahor

      Author's profile photo Safa Golrokh Bahoosh
      Safa Golrokh Bahoosh
      Blog Post Author

      Hello Yahor,

      sorry for the late reply. well to build a communication arrangement you need the maintain communication arrangement application and an admin roles. please check if you have access to it. in case you can not create the communication arrangment, as you need the event mesh instance service key, you might create a ticket asking for integration with event mesh service...

      best regards

      Safa

      Author's profile photo Jayanta Choudhuri
      Jayanta Choudhuri

      https://blogs.sap.com/2021/09/23/regular-expressions-regex-in-modern-abap/

      See last Comment

      Small correction  . is meta character \. is full-stop

      Sorry for interruption

      Author's profile photo Saurabh Joshi
      Saurabh Joshi

      Good Blog !!

      Author's profile photo Ly-Na Phu
      Ly-Na Phu

      Safa Golrokh Bahoosh

      is it possible to send the event to an HTTP endpoint directly, ala Webhook, instead of sending it to the SAP Event Mesh? We can write our own code using the HTTP client library and make the HTTP callout to an endpoint. But how can our custom code get the event fired on the business data?

      Thanks
      Ly-Na

      Author's profile photo Safa Golrokh Bahoosh
      Safa Golrokh Bahoosh
      Blog Post Author

      Hello,

      yes! it is possible, you need to define a HTTP handler class and use the webbooks as you mentioned, however this is also must be done through SAP event mesh. you can create a webhook for your event mesh instance. I don't want to go to the detail, but the custom code get the data through the URL of your HTTP service you provide for the webhook as you create it and also destination definition& proxy setting in your handler method.

      regards

      Safa

      Author's profile photo Ly-Na Phu
      Ly-Na Phu

      Hi Safa Golrokh Bahoosh

      Thanks for your reply.

      We know that in Event Mesh, we can use webhook. But that was not my question. I think you misunderstood my question, or I have not precisely described my question. Sorry for that.

      We don't want to use the webhook in Event Mesh but local in s/4hana. We would like to write an abap custom code in s/4hana to consume the event from the business event enablement component. Then we want to push the event with our custom code to the external client and not to the SAP Event Mesh.

      Thanks,
      Ly-Na

      Author's profile photo Safa Golrokh Bahoosh
      Safa Golrokh Bahoosh
      Blog Post Author

      Hello Ly-Na

      Thank for explanation and sorry for the confusion.

      As far as I know, S/4 only connects to SAP Event Mesh (using AMQP protocol). I can check with other colleagues to see if any 3rd party brokers are planned to be supported (by I would not expect that to be the case).

      Best regards

      Safa

      Author's profile photo Ly-Na Phu
      Ly-Na Phu

      Hi  Safa Golrokh Bahoosh

      Any news you could get from your colleagues?

      Thanks,
      Ly-Na

      Author's profile photo Daniel Gerecht
      Daniel Gerecht

      Hi Ly-Na,

      sorry for the late reply. But local event handlers, without Event Mesh usage, are planned for the upcoming release:

       

      |SAP BTP, ABAP environment 2308| |SAP S/4 HANA Cloud 2308| |SAP S/4HANA 2023| |SAP S/4HANA Cloud, private edition 2023|

       

      Best regards,
      Daniel

      Author's profile photo Rammel Balagtas
      Rammel Balagtas

      Is it also released for the embedded steampunk (public cloud edition) ? S/4HANA ABAP cloud environment 2308?

      Author's profile photo Daniel Gerecht
      Daniel Gerecht

      yes.

      Author's profile photo Rammel Balagtas
      Rammel Balagtas

      I saw from the earlier SAP TechEd demo session that he can already created the local event handler. Is it now available? Could you help to point me to any tutorial or SAP documentation around it? Basically our requirement was to handle the standard business events (e.g. Sales Order changed, Business partner changed, etc) similar to how BTE previously works before in classical extensibility.

      Most of the tutorials point me to the usage of SAP event mesh but what I really wanted to do is handle the standard business event locally (from the S/4hana abap cloud environment).

      I roughly saw his code but as the session was very fast, I didn't get to see how it was used or implement. Hoping that you can guide as with the documentation around it. Thank you so much!

      Author's profile photo Daniel Gerecht
      Daniel Gerecht

      I hope this helps: Business Event Consumption | SAP Help Portal

      Author's profile photo Lamia FARHAT
      Lamia FARHAT

      Hi Safa Golrokh Bahoosh

       

      Is it possible to implement the same mechanism in S4 instead of SAP ABAP in BTP ?

       

      Regards

      Lamia

      Author's profile photo Safa Golrokh Bahoosh
      Safa Golrokh Bahoosh
      Blog Post Author

      Hello Lamia,

      yes! I'm about to write the Blog 🙂 but in short, its almost the same with a slight change to send the event out( creating the channel directly in S/4 and define the outbound binding, instead of defining communication arrangement as we do in the cloud) . I update this post accordingly and provide the link as soon as I finish.

      regards

      Author's profile photo Lamia FARHAT
      Lamia FARHAT

      Hi Safa Golrokh Bahoosh

       

      Thanks a lot for your reply but I'm a little bit concerned seems that we cannot send 'custom' events from S4 without the Add on for event enablement ..

       

      Thanks a lot

       

      Lamia

      Author's profile photo Safa Golrokh Bahoosh
      Safa Golrokh Bahoosh
      Blog Post Author

      That's right! This means that if you have created the events with the add-on, you must also send them with the add-on.

      Author's profile photo Lamia FARHAT
      Lamia FARHAT

      No in fact I have not created the events with the add on and I don't have the add-on installed .

      I'm searching for a solution without this add-on .

      Author's profile photo Safa Golrokh Bahoosh
      Safa Golrokh Bahoosh
      Blog Post Author

      In this case, either follow the link I sent you in the other post or wait for the blog to come 😉

      Author's profile photo Safa Golrokh Bahoosh
      Safa Golrokh Bahoosh
      Blog Post Author

      Hello Lima,

      as promised, the blog on Creating RAP Business Events in S/4HANA On Premise 2022 is published. hope it helps you solving the issue.

      regards

      Safa

      Author's profile photo Lamia FARHAT
      Lamia FARHAT

      Hello Safa

      Thank you so much for getting back to me .

      Bad chance I'm in 2021 🙂

      Kind Regards

      Lamia

      Author's profile photo Safa Golrokh Bahoosh
      Safa Golrokh Bahoosh
      Blog Post Author

      Hello Lamia,

      you're welcome, then it's time to upgrade 😉

      Best regards

      Safa

      Author's profile photo Baris Buyuktanir
      Baris Buyuktanir

      Dear Safa,
      Thanks for the blog entry for publishing the event from S/4HANA.

      https://blogs.sap.com/2023/01/25/how-to-create-rap-business-events-in-s-4hana-on-premise-2022/

       

      in this blog you've published the event to Event Mesh via service key.

      My question is:

      Is this possible to publish it via the same mechanism to an event broker-other whan EventMesh- via the same mechanism. (Assume that it is using the MQTT protocol)

      If yes what would be in the manual configuration in creating new channel ( I assume destination, OAuth Configuration, Client Profile should be manually created..) if you don't have a service key(which is gathered through EventMesh, which we don't have).

      To wrap up, my question is what could be the manual configuration if we want to publish this event to an EventBroker handling MQTT, or even to an HTTP Endpoint(even better).

      Thank you,

      Baris

      Author's profile photo Daniel Gerecht
      Daniel Gerecht

      Hi Baris,

      currently only integration with SAP Event Mesh (default plan) is supported. Futher integrations are planned, e.g., we plan to support the integration SAP Advanced Event Mesh in S/4HANA Private 2023.

      Unfortunately the MQTT protocol does not completely specify the interaction with an event broker. In fact, we even use the AMQP protocol for integration with Event Mesh in the current S/4 releases.

      Best regards,
      Daniel

      Author's profile photo Matt Dion
      Matt Dion

      Hi Safa,

      First, thank you very much for this blog. It was quite helpful during our initial event mesh setup last year.

      However, I think something has changed since you wrote it. Events that we previously created are no longer being sent to the event mesh. The only thing that I've found is the in the event binding. Here you provided the details:

      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.

      I think the issue might be that we are no longer able to freely choose the names. I am seeing the message now that: the business object name does not exist:

      Do you know if something has changed in the meantime to this setup?

      Thanks in advance.

      Author's profile photo Safa Golrokh Bahoosh
      Safa Golrokh Bahoosh
      Blog Post Author

      Hello Matt,

      Thanks for your interest and happy to hear that was helpful 🙂

      actually, there is no change in regards to that. the point is, you can create an Event Binding with standard SAP Business Objects(Ex: Events defined in BAPI’s) or you can freely choose your own name in case of custom RAP events(warning occurs ). so you get a warning while you create an event binding with a new business object you've freely chosen. however, this warning is not a showstopper and you can continue buildings your event binding and you well find the type later on to add it to your outbound binding and ....

      Thanks & best regards

      Safa

       

      Author's profile photo Matt Dion
      Matt Dion

      Hi Safa,

      Thanks for the confirmation! In the meantime, we discovered that the event mesh's service key needed to be reapplied to the communication arrangement for some reason (even though the service key had not changed). This fixed the communication issue.

      It is concerning considering it appears that the connection can be broken for no apparent reason and without any alert, but this is of course beyond the scope of this blog. So again, thanks!

      Best regards,

      Matt

      Author's profile photo Safa Golrokh Bahoosh
      Safa Golrokh Bahoosh
      Blog Post Author

      Hi Matt,

      some time it could be the case that the communication is broken, you can always check if the connection is successful. if there is any issues, you will get a pop up. usually this can be solved by reactivating...but I never faced an issue that I need to renter the same service key...anyway, happy to hear that the problem is solved 🙂

      regards

      Safa