Enterprise Resource Planning Blogs by Members
Gain new perspectives and knowledge about enterprise resource planning in blog posts from community members. Share your own comments and ERP insights today!
cancel
Showing results for 
Search instead for 
Did you mean: 
ajith_urimajalu
Participant

Introduction to SAP Business Events


Enterprises running SAP often need to send changes in SAP business data to external systems. For example, when a material description is changed in SAP, an external system may want the updated description to change a label. In SAP terminology, these are called “Business Events”.


Over the years, SAP has changed the way how Business Events are raised and how these events can be consumed.




  1. One option that has been around for a very long time is Business Transaction Events (BTE) where events can be be consumed using function modules. Here’s an example of BTE in S/4HANA.

  2. Another option that is still relevant in S/4HANA is Business Workflow Events, which allows configuring of a function module (using SWE_TEMPLATE_REC_FB as a template) or a class (that implements interface BI_EVENT_HANDLER_STATIC to consume the business event.

  3. Most recently, SAP introduced RAP Business Events as the primary eventing mechanism for ABAP RESTful Application Programming Model (RAP). Majority of RAP based applications on both S/4HANA Cloud and S/4HANA On-Prem support RAP Business Events.


Google Cloud Pub/Sub as an event messaging platform


Pub/Sub works as a messaging middleware for traditional service integration or a simple communication medium for modern microservices. ABAP SDK for Google Cloud provides client library classes that allow SAP applications to natively call over 55 Google APIs including Pub/Sub.


While an external system can use pull subscription to read messages published to a Pub/Sub topic, a push subscription can deliver events to serverless webhooks on Cloud Functions, App Engine, Cloud Run, or Application Integration. This versatility makes Pub/Sub an ideal choice for sending SAP Business Events to external systems.


In this blog post, I will show how you can send a RAP Business Event to a Pub/Sub topic.



Anatomy of RAP Business Events


Let’s first understand how RAP Business Events work and how these events can be handled by RAP event handler class.




Using demo CDS entity demo_rap_event_m (available in newer S/4HANA systems) as an example:




  • RAP Business Event created is defined in the Behavior definition demo_rap_event_m.

  • The event is raised in the local implementation (CCIMP) of behavior implementation class bp_demo_rap_event_m when a new entry is created.


Handling RAP Business Events


Business Events can be handled in a RAP Event handler class. For example, class cl_demo_rap_event_handler handles events of entity demo_rap_event_m



CLASS cl_demo_rap_event_handler 
DEFINITION PUBLIC ABSTRACT FINAL
FOR EVENTS OF demo_rap_event_m.
...
ENDCLASS.

The implementation of the event handler methods is done in CCIMP include by defining a local class that inherits from CL_ABAP_BEHAVIOR_EVENT_HANDLER.



CLASS lhe_event DEFINITION INHERITING FROM cl_abap_behavior_event_handler.    
PRIVATE SECTION.
METHODS on_created FOR ENTITY EVENT created FOR demo_rap_event_m~created.
....
ENDCLASS.

You can run class cl_demo_rap_events to see the event handler in action.


Key Notes / Observations:




  1. Event handlers are executed using bgRFC, which requires setup of default destination BGPF and a supervisor destination (note https://me.sap.com/notes/0003099088)

  2. You can define multiple event handler classes for the same CDS entity.


Send message to Pub/Sub using RAP Event Handler


Now that we know how to create handlers for RAP events, let’s create one to send the event information to a Pub/Sub topic using ABAP SDK.




If you are new to ABAP SDK for Google Cloud, please use public documentation links to set up ABAP SDK and Pub/Sub resources. We will use the following values to publish messages to Pub/Sub topic:




  1. Client Key ‘PUBSUB’

  2. Pub/Sub topic ‘SAP_DEMO_RAP_EVENT’.


Lets create an event handler class zcl_event_handler_pubsub for the CDS entity demo_rap_event_m



CLASS zcl_event_handler_pubsub 
DEFINITION PUBLIC ABSTRACT FINAL
FOR EVENTS OF demo_rap_event_m.
...
ENDCLASS.

In the local types (CCIMP include) of zcl_event_handler_pubsub, add the below logic to publish the message to Pub/Sub topic using ABAP SDK for Google Cloud.



CLASS lhe_event DEFINITION INHERITING FROM cl_abap_behavior_event_handler.

PRIVATE SECTION.

METHODS on_created FOR ENTITY EVENT
created FOR DEMO_RAP_EVENT_M~created.

ENDCLASS.

CLASS lhe_event IMPLEMENTATION.

METHOD on_created.

TRY.
DATA(lo_pubsub) = NEW /goog/cl_pubsub_v1( iv_key_name = 'PUBSUB' ).

DATA(ls_input) = VALUE /goog/cl_pubsub_v1=>ty_023( ).

LOOP at created ASSIGNING FIELD-SYMBOL(<ls_created>).

APPEND VALUE #(
data = cl_web_http_utility=>encode_base64(
/ui2/cl_json=>serialize( data = <ls_created> ) ) )
to ls_input-messages.
ENDLOOP.

lo_pubsub->publish_topics(
EXPORTING
iv_p_projects_id = CONV string( lo_pubsub->gv_project_id )
iv_p_topics_id = 'SAP_DEMO_RAP_EVENT'
is_input = ls_input
IMPORTING
ev_ret_code = DATA(lv_ret_code)
ev_err_text = DATA(lv_err_text)
es_err_resp = DATA(ls_err_resp) ).

CATCH /goog/cx_sdk INTO data(lo_sdk_error).
ENDTRY.

"Add error handling logic

ENDMETHOD.

ENDCLASS.

By running class CL_DEMO_RAP_EVENTS, our event handler class zcl_event_handler_pubsub is triggered and the message is published to the Pub/Sub topic




These messages can now be consumed by external systems using pull / push subscriptions.



Applying the approach to standard RAP Events


As SAP is adopting RAP for all newer applications built on S/4HANA On-Prem and Public Cloud, the above approach can be used to stream SAP business events to external applications.



Finding Standard RAP Events


You can find standard RAP Events by looking at the behavior definitions in ADT. You can search behavior definitions using the Property Filter type:bdef in the ADT search bar.




List of Behavior Definitions in S/4HANA On-Prem System

When you open the behavior definition, you can find the events which can be handled using the event handler.





Extending behavior definitions to implement your own events


What if a standard behavior does not have an event? Behavior Extensions to the rescue!


As long as the standard behavior definition is marked as extensible, you can :




  1. Create a Behavior Extension for the Behavior and implement your own events.

  2. Inherit system class cl_abap_behavior_saver and write logic to raise the event within CCIMP include of RAP behavior implementation class

  3. Handle the event using the same approach as above


Conclusion and Next Steps


As you can see, by using RAP Business Events along with ABAP SDK for Google Cloud & Pub/Sub, you can build modern, scalable event-driven integration between SAP and external systems.


Happy to answer any questions. Happy Learning and Innovating !
7 Comments
stefan_se
Discoverer
0 Kudos
Hello,

I tried to follow the example for a local RAP event consumption, but when creating the Event Handler class, I am always getting the following syntax error:

"The use of Behavior Definition DEMO_RAP_EVENT_M is not permitted."

It seems that it means the root entity is not released for Cloud Development. System is an S4 2023 Cloud Private Edition.

Best regards,
Stefan
stefan_se
Discoverer
I found it myself, I am now able to consume events of R_PurchaseOrderTP (in my example on a changed Purchase Order).

I switched the language version of the package, but I need to switch still afterwards the language version of my class manually back to Standard ABAP.

I checked further, there are 181 Behaviour Definitions showing up in the "USE_FOR_CLOUD_DEVELOPMENT", but I did not see any that have already events.

So the approach 3) local event consumption looks promising, but still is not considered "Clean Core" due to lack of released objects that support events.

Best regards,
Stefan
ajith_urimajalu
Participant
0 Kudos
Searching for a behavior with events can be challenging. Hopefully, SAP provides an easy way to search them.

 

I found released behavior R_SalesOrderTP with events in an S/4 system. See if you can use it for your proof of concept.
stefan_se
Discoverer
0 Kudos
Thank you for the hint.

Even on Sales Order on S4 2023 I am getting:
"The use of Behavior Definition R_SALESORDERTP is not permitted."

It has a C0 contract (extend), but not C1 (use system-internally).

Best regards,
Stefan
Victor_Alvarez
Participant
0 Kudos

This is one of the main problems that we are also facing. What is the sense in permitting the use of local events if we cannot consume it since the use is not permitted:

Victor_Alvarez_0-1707990995257.png

 

@Andre_Fischer  any thoughts?

METHODS consume_custom_event FOR ENTITY EVENT d_journalentrycreated FOR Journalentry~created.

 

 

BlackmanCC
Explorer
0 Kudos

@Andre_Fischer What can I do if there is no RAP object and no RAP event. Can I also send classic EVENTS (e.g. for BOR  objects) over the Event channel of "S/4 Enterprise Events"?

 

Andre_Fischer
Product and Topic Expert
Product and Topic Expert
0 Kudos

@stefan_se the R_...TP CDS views or behavior definitions are never C1-released. Instead you have to use the transactional interface views e.g. I_SalesOrderTP.

Since using the comment section for solving technical problems is a tedious task I suggest to post questions in the Q&A section instead.

Labels in this area