Send Event from SAP to Pub/Sub: Enabling SAP as a Pub/Sub Publisher

Kriti Sugandha
Google Cloud - Community
5 min readNov 24, 2023

--

Event-driven architecture is a software design pattern in which components communicate with each other by broadcasting and processing events. This pattern has become increasingly popular in recent years, as it allows for more loosely coupled and scalable systems.

One way to implement event-driven architecture in SAP is to use the ABAP SDK for Google Cloud.

The ABAP SDK for Google Cloud already supports a number of Google Cloud services, including Cloud Storage, Cloud Translation, Document AI, Secret Manager and Cloud Pub/Sub.

Pub/Sub is an asynchronous and scalable messaging service that decouples services producing messages from services processing those messages. It allows services to communicate asynchronously, with latencies on the order of 100 milliseconds, and is used for streaming analytics, data integration pipelines, service integration, and task parallelization.

Use Case: SAP as a Publisher

A third-party system needs to be notified whenever a new material is created in SAP. We can use the ABAP SDK for Google Cloud and Cloud Pub/Sub to implement this.

Prerequisites

  • Install ABAP SDK for Google Cloud [Detailed steps here]
  • Create a Pub/Sub topic in the Google Cloud Console: DEMO_CPS_MATERIALS. Also attach a default subscription to the topic
  • Provide the required roles to the service account: Pub/Sub Publisher
  • Configure the client key based on your environment (GCP/non-GCP). [More about Authentication]

Implementation Steps:

Create a Function Module “ZDEMO_CPS_PUBLISH_MATERIALS” in SM37.

  • This FM will be called whenever a new material is created within SAP.
  • It will use the ABAP SDK for Google Cloud: Pub/Sub API to publish a message to the Cloud Pub/Sub topic.
  • Please note that Pub/Sub expects a base64 encoded string as the input message.
  • You can also use the Code Wizard in the ABAP SDK for Google Cloud to generate the code for this function module.
FUNCTION zdemo_cps_publish_materials.
*" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*"*"Local Interface:
*" IMPORTING
*" VALUE(EVENT) LIKE SWETYPECOU-EVENT
*" VALUE(RECTYPE) LIKE SWETYPECOU-RECTYPE
*" VALUE(OBJTYPE) LIKE SWETYPECOU-OBJTYPE
*" VALUE(OBJKEY) LIKE SWEINSTCOU-OBJKEY
*" VALUE(EXCEPTIONS_ALLOWED) LIKE SWEFLAGS-EXC_OK DEFAULT SPACE
*" EXPORTING
*" VALUE(REC_ID) LIKE SWELOG-RECID
*" TABLES
*" EVENT_CONTAINER STRUCTURE SWCONT
*" EXCEPTIONS
*" TEMP_ERROR
*" ANY_ERROR
*" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TYPES: BEGIN OF lty_attr_wf_event,
event TYPE swetypecou-event,
objtype TYPE swetypecou-objtype,
source TYPE string,
END OF lty_attr_wf_event.
DATA: lv_p_projects_id TYPE string,
lv_p_topics_id TYPE string,
ls_input TYPE /goog/cl_pubsub_v1=>ty_023,
ls_attr_wf_event TYPE lty_attr_wf_event.

TRY.
* Open HTTP Connection
DATA(lo_client) = NEW /goog/cl_pubsub_v1( iv_key_name = 'DEMO_PUBSUB' ).
* Populate relevant parameters
lv_p_projects_id = lo_client->gv_project_id.
lv_p_topics_id = 'DEMO_CPS_MATERIALS'.
ls_attr_wf_event-event = event.
ls_attr_wf_event-objtype = objtype.
ls_attr_wf_event-source = 'SAP'.
GET REFERENCE OF ls_attr_wf_event INTO DATA(lr_attr).
APPEND INITIAL LINE TO ls_input-messages ASSIGNING FIELD-SYMBOL(<ls_message>).
IF <ls_message> IS ASSIGNED .
IF objkey IS NOT INITIAL.
<ls_message>-data = cl_http_utility=>encode_base64( unencoded = CONV string( objkey ) ).
GET REFERENCE OF lr_attr INTO <ls_message>-attributes.
ENDIF.
ENDIF.
* Call API method: pubsub.projects.topics.publish
CALL METHOD lo_client->publish_topics
EXPORTING
iv_p_projects_id = lv_p_projects_id
iv_p_topics_id = lv_p_topics_id
is_input = ls_input
IMPORTING
* es_raw =
es_output = DATA(ls_output)
ev_ret_code = DATA(lv_ret_code)
ev_err_text = DATA(lv_err_text)
es_err_resp = DATA(ls_err_resp).
IF lo_client->is_success( lv_ret_code ).
MESSAGE 'Success' TYPE 'S'.
ELSE.
MESSAGE lv_err_text TYPE 'E'.
ENDIF.
* Close HTTP Connection
lo_client->close( ).
CATCH /goog/cx_sdk INTO DATA(lo_exception).
MESSAGE lo_exception->get_text( ) TYPE 'E'.
ENDTRY.
ENDFUNCTION.

We can use workflow events to set up this function module to run whenever a new material is created. To do this, use transaction code SWE2.

  • Object Category: BOR
  • Object Type: BUS1001006
  • Event: Created

Test:

  • Create a material using T-code MM01.
  • To check if the material was published in Pub/Sub:
  • On the GCP console, go to the topic, tab: Messages
  • Select the default subscription that you created earlier.
  • Click Pull to get a list of published messages.

This should display the latest message which was published from SAP.

The third-party system can subscribe to the Pub/Sub topic and receive notifications whenever a new material is created. It can then take whatever action is necessary, such as updating its own database or sending an email notification.

ABAP SDK for Google Cloud and Cloud Pub/Sub can be used to implement event-driven architectures between SAP and other systems. This can help to improve the efficiency and scalability of your business processes.

Next Steps

Embrace the dynamic ABAP SDK for Google Cloud Community, a thriving ecosystem where fellow ABAP developers leveraging Google Cloud converge to share knowledge, collaborate, and explore. Join us as we shape the future of ABAP SDK for Google Cloud and unleash a universe of possibilities.

Embark on your innovation journey with us:

Delve into the world of ABAP SDK on YouTube: SAP and Google Cloud: Innovating Together

Uncover insightful content on Medium: Follow the medium tag for ABAP SDK for Google Cloud

Happy Learning! Happy Innovating!

--

--