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
16,984
Following the 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, here I would like to show you how you can create RAP Business Events in an OP system.

With the release 2022, SAP supports the native exposure and consumption of business events for on-premise systems as well. 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 ready-to-use event consumption models using the event consumer wizard embedded in ABAP Development Tools for Eclipse. This is explained in another post later.

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. For those who read the corresponding blog post for creating RAP events in the Cloud, you can skip from this part until the connection to the SAP Event Mesh which is the main difference between the two.

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.

From release 2022 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, SAP BTP ABAP Environment 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 SAP 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 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 must 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 SAP 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. Basically, 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 behaviour 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 SAP 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 in OP' ) ) ).

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 required is a minimal RAP implementation that provides a method that raises the event that can be called from existing non-RAP .

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. In the Cloud, we need to create a communication arrangement to provide the channel. However, in on premise you can directly create the channel in the system.

Creating a channel using a service key


A channel represents a single connection to a service instance of the SAP Event Mesh. As a prerequisite, you need to prepare an SAP Event Mesh instance in your SAP business technology platform system and download the service key of this instance.

In your system, run the transaction /IWXBE/CONFIG and press the ‘📄via Service Key’ button to create a new channel.


Paste the service key of your SAP Event Mesh instance into the corresponding field. Destination name and OAuth Configuration name are optional fields. These are filled with random names if left empty.

The daemon user denotes the user under which the daemon session is running. The daemon sessions are background processes that are responsible for keeping the connection open and process the events. There is a corresponding role for the customer to use SAP_IWXBE_RT_XBE_DAEMON, which has all the required authorizations.


Once you filled out all necessary fields hit save. Your newly created channel will now show up in the channel list. Activate the channel by pressing the Activate’ button. If the channel is active, it should appear with a green box under active channels.

In case the green status isn’t shown, you might need to refresh because the background session needs time to start.


After creating a channel, you can decide which events should be listed on this channel. This explicit step of maintaining an outbound binding is necessary to publish events by an SAP S/4HANA system.

Configure outbound bindings


To send the created event to the SAP Event Mesh, you need to configure the outbound bindings for this channel. In the channel config UI, you can simply click on ‘Outbound Bindings’ to start configuring the event topics to be sent to SAP Event Mesh. Alternatively, you can run /IWXBE/OUTBOUND_CFG.

Select your channel, click on creates new topic binding and choose 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. For more information on how to create a queue, check this blog post. 



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



Now we check the SAP 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.



You can also monitor the generated event with /IWXBE/EVENT_MONITOR in your SAP S/4HANA system:



Summary


A RAP event is modelled 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 SAP Event Mesh service. You also need to create and configure your channel in your SAP S/4HANA system. More you can find 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.

 

 
14 Comments
former_member791038
Discoverer
0 Kudos
Hi Safa, thanks for useful post.

Is it explained somewhere how to consume an event using RAP BO in OP system?

KR,
Yahor
SafaBahoosh
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hello Yahor!

you're welcome, Yes it is 🙂

regards

Safa
former_member791038
Discoverer
0 Kudos
Could you please share? may be some blogposts?
SafaBahoosh
Product and Topic Expert
Product and Topic Expert
0 Kudos
I'm about to write it, I'll update you soon here.
PaV11
Contributor
0 Kudos
Hello Safa,

This is excellent news for S/4HANA on-premise or private cloud customers.

As of now, SAP recommended to use MQTT for sending events from S/4HANA to BTP Event Mesh.

In your screenshots, I see that AMQP is used. Is AMQP the recommended protocol from sending S/4HANA RAP events to Event Mesh ? Please guide.

 

Regards,

Vinod
SafaBahoosh
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Vinod,

Thanks for your interest.

The both protocols works. We recommend to use AMQP but at the end it is up to your need and your system configuration as it doesn't make a difference here...the bottleneck isn't the protocol.

MQTT generally serves high-latency, low-bandwidth networks with thousands of embedded devices better. On the other hand, AMQP works better in large, enterprise-wide applications with highly customized and involved messaging requirements.

Regards

Safa

Lars_T
Newcomer
0 Kudos
Hi Safa,

Thank you for this and your other Blogs about RAP events!

As of my understanding of the blog, it's possible with existing, extensible BOs to extend SAP standard with RAP events.

"... If you already have a BO, you can extend your own BO with the appropriate logic."

I'm currently trying to setup RAP events for Process Orders, but cannot find an appropriate BO for Process Orders, making me think it's not possible to use RAP events for Process Orders.

Is there a SAP-recommended way to use RAP events with Process Orders?

Regards
Lars

 
TillHeinen
Participant
0 Kudos
Hi Safa,

thanks for the How-To. It helped already a lot!

I want create an event when the ISU Contract (SAP table EVER) is changed. To then trigger Event Mesh etc. But for this SAP standard table, I can also not find an API in the API HUB.

Therefore I was trying to create a RAP Business Object from the table EVER (not a Z_ copy, but the SAP Standard table)

When trying to do this by the ADT function 'Generate ABAP Repository Object', I always get this:

Error Message:

Add "local last changed" admin field ( ABP_LOCINST_LASTCHANGE_TSTMPL,ABP_LOCINST_LASTCHANGE_UTCL ) and "last changed" admin fields (ABP_LASTCHANGE_TSTMPL, ABP_LASTCHANGE_UTCL ) to the reference table.'

 

After generating the BO, I want to proceed with the Event Behaviour etc and to this to the outbound bindings.

 

Can you help me on that case? How is this covered in RAP Business Events.

Best wishes

Till
TillHeinen
Participant
0 Kudos

Dear safa_bahoosh 

you or you colleagues might have a look an the question posted:

Trigger RAP BO Business Event in SAP S/4HANA On Premise 2022 from GUI and FIORI UI (Object EVER) | S...

 

Thanks in advance 🙂

Best wishes

Till

TillHeinen
Participant
Hi,

I go a reply and want to share it here:

I asked in the forum: Create RAP Business Events from standard table in an On-Premise system | SAP Community


Hello Till,

For ADT function 'Generate ABAP Repository Object', You will have to append these two admin fields always so that RAP generator can able to generate all the RAP Objects automatically.
local_last_changed type ABP_LOCINST_LASTCHANGE_TSTMPL
last_changed type ABP_LASTCHANGE_TSTMPL

Append above fields with mentioned type in your db table.

Thanks a lot.

Best Regards,

Muthu

Frank1
Participant
0 Kudos
Informative blog on RAP event integration in S/4 On-Premise and private cloud: 
It's great to learn that RAP events can now be consumed within S/4 and even in SAP ABAP legacy code.
Additionally, if you wish to consume the event outside the S/4 system,
you must subscribe to the SAP BTP Event Mesh service.

How many S/4 On-Premise and private cloud customers are interested in using SAP BTP?
Are there only a few or a significant number of S/4 On-Premise and private cloud customers?
If they opt for BTP, what business cases would they typically use it for?
I assume that most customers would leverage BTP for integration purposes,
such as connecting SAP to SAP systems or SAP to non-SAP systems, is that correct?
We are on version 2023 of S4/HANA OP and we were wondering if sending dataevents towards EM is possible. We have only worked with standard topics in outbound bindings yet. They are only sending the ID (notification), not the whole dataset.
johnabraham1122
Newcomer
0 Kudos

To create RAP (Restful ABAP Programming) business events in SAP S/4HANA On-Premise 2022:

  1. Set up your RAP environment and ensure the necessary services are active.
  2. Define the event model in your RAP business object.
  3. Use CDS views to capture and expose the relevant business data.
  4. Implement the event handling logic in ABAP, then trigger and publish the events.

Make sure to test thoroughly in your development environment before deployment.

JaySchwendemann
Active Contributor
0 Kudos

Hi all,

is there a way to raise an Business Event for such things that normally would create an IDoc (say MATMAS)? Is this already build in? Are change pointers supported? Or would we need the minimal viable CDS stuff to make an RAP BE happen?

(Assuming an S/4HANA 2023 FPS1 on-prem system - but please also answer if that would onyl be supported on FPS2 or a future release)