Skip to Content

How to Create RAP Business Events in an On-Premise system

Requires Customer/Partner License
Create and exposure of business events in an On-Premise system
You will learn
  • How to create Business Events with RAP in an On-Premise system
  • How to set up a channel to connect to SAP Event Mesh
Created by
mervey45
March 21, 2023
Contributors
mervey45
niloofar-flothkoetter

Prerequisites

  • You need to have access to an On-Premise 2022 system with authority to create and maintain a channel.
  • You need to prepare an event mesh instance in your SAP business technology platform system and download the service key of this instance. For more information see the Create Instance of SAP Event Mesh

Always replace #### with your initials or group number.

The ABAP RESTful Application Programming Model (RAP) now supports the native consumption and exposure of business events from release 2022. For exposure, an event can be defined and raised in a RAP business object or in the behavior extension and then published via Event Bindings.

  • Step 1

    To produce and raise an event you need first to define your RAP Business Object which produce the event. For this, you will create a booking application. The event will be sent whenever a booking flight is cancelled.

    1. Open ADT and open your system.

    2. If you do not have an ABAP Package create a new one. Please be sure if your package name is started with Z like

      • Name: ZEVENT_BOOKING_####
      • Description: define a RAP event
      new
    3. Right-click on your package and create a new database table

      • Name: ZBOOKING_####
      • Description: DB for booking
      database
      database
    4. Replace the default code with the code snippet provided below and replace all placeholder #### with your group ID using the Replace All function Ctrl+F.

      ZBOOKING_####
      Copy
      @EndUserText.label : 'DB for booking'
      @AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
      @AbapCatalog.tableCategory : #TRANSPARENT
      @AbapCatalog.deliveryClass : #A
      @AbapCatalog.dataMaintenance : #RESTRICTED
      define table zbooking_#### {
      key client            : abap.clnt not null;
      key travel_id         : /dmo/travel_id not null;
      agency_id             : /dmo/agency_id;
      customer_id           : /dmo/customer_id;
      begin_date            : /dmo/begin_date;
      end_date              : /dmo/end_date;
      @Semantics.amount.currencyCode : 'zbooking_####.currency_code'
      booking_fee           : /dmo/booking_fee;
      @Semantics.amount.currencyCode : 'zbooking_####.currency_code'
      total_price           : /dmo/total_price;
      currency_code         : /dmo/currency_code;
      description           : /dmo/description;
      overall_status        : /dmo/overall_status;
      attachment            : /dmo/attachment;
      mime_type             : /dmo/mime_type;
      file_name             : /dmo/filename;
      created_by            : abp_creation_user;
      created_at            : abp_creation_tstmpl;
      last_changed_by       : abp_locinst_lastchange_user;
      last_changed_at       : abp_locinst_lastchange_tstmpl;
      local_last_changed_at : abp_lastchange_tstmpl;
      }  
      
      
    5. Save and activate your table.

      database
  • Step 2
    1. Right-click your package and select New > ABAP Class from the context menu.

      cds
    2. Maintain the required information and click Next

      • Name: ZCL_BOOKING_GEN_DATA_####
      • Description: Generate demo data
      demo
    3. Select a transport request and click Finish to create the class.

    4. Copy the code below to your CDS data model and replace #### with your number

      ```ZCL_BOOKING_GEN_DATA_####
      
      CLASS zcl_booking_gen_data_#### DEFINITION
      PUBLIC
      FINAL
      CREATE PUBLIC .
      
      PUBLIC SECTION.
      
          INTERFACES if_oo_adt_classrun.
      PROTECTED SECTION.
      PRIVATE SECTION.
      ENDCLASS.
      
      CLASS zcl_booking_gen_data_#### IMPLEMENTATION.
      
      METHOD if_oo_adt_classrun~main.
          DATA:
          group_id   TYPE string VALUE '####',
          attachment TYPE /dmo/attachment,
          file_name  TYPE /dmo/filename,
          mime_type  TYPE /dmo/mime_type.
      
      *   clear data
          DELETE FROM zbooking_####.
      
          "insert travel demo data
          INSERT zbooking_0000  FROM (
              SELECT
              FROM /dmo/travel AS travel
              FIELDS
                  travel~travel_id        AS travel_id,
                  travel~agency_id        AS agency_id,
                  travel~customer_id      AS customer_id,
                  travel~begin_date       AS begin_date,
                  travel~end_date         AS end_date,
                  travel~booking_fee      AS booking_fee,
                  travel~total_price      AS total_price,
                  travel~currency_code    AS currency_code,
                  travel~description      AS description,
                  CASE travel~status    "[N(New) | P(Planned) | B(Booked) | X(Cancelled)]
                  WHEN 'N' THEN 'O'
                  WHEN 'P' THEN 'O'
                  WHEN 'B' THEN 'A'
                  ELSE 'X'
                  END                     AS overall_status,
                  @attachment             AS attachment,
                  @mime_type              AS mime_type,
                  @file_name              AS file_name,
                  travel~createdby        AS created_by,
                  travel~createdat        AS created_at,
                  travel~lastchangedby    AS last_changed_by,
                  travel~lastchangedat    AS last_changed_at,
                  travel~lastchangedat    AS local_last_changed_at
                  ORDER BY travel_id UP TO 10 ROWS
          ).
          COMMIT WORK.
          out->write( |Demo data generated for table zbooking_{ group_id }. | ).
      ENDMETHOD.
      ENDCLASS.
      
      ```
      
    5. Save and activate your data model.

    6. Run your console application. For that, select your ABAP class zcl_booking_gen_data_####, select the run button > Run As > ABAP Application (Console) F9 or press F9. A message will be displayed.

      class
      class
    7. Open your database table ZBOOKING_#### and press F8 to start the data preview and display the filled database entries, i.e. travel data.

      data
  • Step 3

    Create your OData v4 based UI services with the built-in ADT generator.
    The generated business service will be transactional, draft-enabled, and enriched with UI semantics for the generation of the Fiori elements app.

    1. Right-click your database table ZBOOKING_#### and select Generate ABAP Repository Objects from the context menu.

      app
    2. Maintain the required information and click Next:

      • Description: Booking App ###
      • Generator: ABAP RESTful Application Programming Model: UI Service
      app
    3. Maintain the required information on the Configure Generator dialog to provide the name of your data model and generate them.

      RAP Layer Artefacts Artefact Name
      Business Object
      Data Model Data Definition Name: ZEVENT_R_BOOKINGTP_####
      Alias Name: BOOKING
      Behavior Implementation Class: ZEVENT_BP_BOOKINGTP_####
      Draft Table Name: ZEVENT_BOOK0000
      Service Projection (BO Projection) Name: ZEVENT_C_BOOKINGTP_0000
      Business Services
      Service Definition Name: ZEVENT_UI_BOOKING_0000
      Service Binding Name: ZEVENT_UI_BOOKING_04_0000
      Binding Type: OData V4 - UI
      app
    4. Verify the maintained entries and press Next to confirm. The needed artefacts will be generated.

      app
    5. Select a transport request and click Finish to confirm the dialog.

      app
    6. Go to the Project Explorer, select your package, refresh it by pressing F5, and check all generated ABAP repository objects.

      app
  • Step 4
    1. Open your service binding ZEVENT_UI_BOOKING_04_#### and click Publish.

      publish
    2. Double-click on the entity BOOKING in the Entity Set and Association section to open the Fiori elements App Preview.

      publish
    3. Click the GO Button to load the data and check your result.

      load
      load
  • Step 5
    1. You need to create an event which is raised when a booking flight is cancelled. For this, open ZEVENT_BP_BOOKINGTP_#### and add a new event to the business object. The event name is BookingCancelled.

        event BookingCancelled parameter ZEVENT_D_BOOKING_CANCEL_####;
      
      

      Your behavior definition will be looking like this:

      parameter
    2. In addition,provide a cancellation reason and description. This can be done via a parameter define as a data definition (abstract entity) called ZEVENT_D_BOOKING_CANCEL_####.

      parameter
    3. Copy the code below in this definition. Do not forget to save and activate.

      ZEVENT_D_BOOKING_CANCEL_####
      Copy
       @EndUserText.label: 'booking cancelation reason'
       define abstract entity ZEVENT_D_BOOKING_CANCEL_####
       {
          REasonCode : abap.char(2);  
          Description : abap.char(64);
       }
      
      
      parameter
  • Step 6

    Now create the event binding for your newly created business event. This event binding is needed to map an event to a RAP entity event.

    1. Right-click on your package and create an event binding

      • Name: ZEVENT_CANCEL_FLIGHT_####
      • Description: cancel flight event
      binding
      binding
    2. Here fill all fields out, to get errors gone. You can freely choose these names to specify your event with some considerations explained below

      • Namespace: zevent#### (No camel case and no space)
      • Business Object: booking (No Space)
      • Business Object Operation: Delete
      error
    3. Click Add to add items.

      • Root Entity Name: ZEVENT_R_BOOKINGTP_#### (your behavior definition)
      • Entity Event Name: BOOKINGCANCELLED (Event name in your behavior definition)
      item
      item
    4. Save and activate your event binding.

    5. As you can see at the screenshot, Type (aka topic) is a concatenation of the three attributes (name space, business object, business object operation) and ends with the version of the event. The wildcard * points to the corresponding event e.g. created. This is relevant for addressing the events to the Event Mesh. Copy this address for later use zevent####.booking.Delete.v*.

      type
  • Step 7
    1. Open your behavior definition ZEVENT_R_BOOKINGTP_#### and add with additional save like following:

      add save
    2. Now open the implementation class ZEVENT_BP_BOOKINGTP_#### and navigate to Local Type.

      local type
    3. Add the lcl_event_handler class and save modified method in there:

          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 zevent_r_bookingtp_####~BookingCancelled
           FROM VALUE #( FOR <s_travel> IN delete-booking  ( TravelID = <s_travel>-TravelID  %param = VALUE #( reasoncode ='02'  description = 'cancelled by customer' ) ) ).
              ENDIF.
          ENDMETHOD.
          ENDCLASS.
      
      
      class
    4. Save and activate your class.

  • Step 8

    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 an on premise system you can directly create the channel in the system.

    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.

    1. Open SAP Logon and login in your on premise system and run the transaction /n/IWXBE/CONFIG

      logon
    2. Press via Service Key to create a new channel.

      key
    3. Enter a channel name and description and copy the service key of your SAP Event Mesh instance into the corresponding field and click Save.

      key
    4. 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.

      active
      active
  • Step 9

    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.

    1. Run /n/IWXBE/OUTBOUND_CFG and select your channel.

      channel
    2. Click Create new topic binding icon and choose the topic which is generated during the event binding creation, zevent####.booking.Delete.v*.

      topic
      topic
      topic
    3. Check your topic is listed.

      topic
  • Step 10

    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.

    The structure of the topic should be like: your event mesh instance namespace/ce/the event type you are generated and are subscribed to: zevent####/booking/delete/*.

    The Topic will look like: PmEvnt/PmEvnt.sap/Demo/ce/zevent####/booking/Delete/*

  • Step 11

    After you created a queue for the selected instance in the SAP Event Mesh system and subscribe it to your topic,

    1. Back to ADT and open service binding ZEVENT_UI_BOOKING_04_####, choose BOOKING and click Preview.

      preview
    2. Click Go to load all bookings. Select an entry with the chosen ID and delete it. An event will be raised then.

      preview
      delete
    3. Back to your queue, here you can see that a message is in your queue.

      message
    4. Click Test and choose your queue. Click Consume message.

      consume
    5. Check the Message Data

      data
    6. To monitor the generated event on SAP Logon, open SAP Logon and run the transaction /n/IWXBE/EVENT_MONITOR. Choose your channel and click Execute

      monitor
      monitor

    When you get a message in the queue in this example?

  • Step 12
Back to top