Send SAP RAP Business Events to external systems using Google Cloud Pub/Sub and ABAP SDK

Ajith Urimajalu
Google Cloud - Community
6 min readDec 29, 2023

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 and ABAP SDK 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.

Here’s a blog post that shows how you can configure a function module to send a Business Workflow Event to a Pub/Sub topic.

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.

Ready to start your innovation journey with ABAP SDK for Google Cloud? Here are some key resources:

Join our community to collaborate with us:

Subscribe and watch videos on our YouTube channel to learn about innovations for SAP on Google Cloud

Happy Learning and Innovating !

--

--