EEE :Custom Business Event for Billing Document Creation with Custom Payload and Context Filters
Share

[[{“value”:”

 

Technical Specification & Implementation Guide

Custom Business Event for Billing Document Creation with Custom Payload and Context Filters


1. Solution Architecture Overview

This solution establishes a hybrid integration pattern in SAP S/4HANA: billing document creation events trigger a custom workflow receiver function module via Event Type Linkage (SWE2), which filters business transactions, validates sales order references, enriches header data using released Core Data Services (CDS), and raises an ABAP RESTful Application Programming Model (RAP) Business Event.

The event is bound via an Event Binding artifact (.evtbnd) and published to external event brokers (such as SAP Event Mesh / SAP Integration Suite, Advanced Event Mesh) using SAP Enterprise Event Enablement (/IWXBE/), utilizing context annotations for source-side topic filtering.

┌─────────────────────────────────┐
│ SD Billing Creation (e.g. VF01) │
└────────────────┬────────────────┘
                 │ Raises Event CREATED (Class CL_SD_BIL_BD_EVENT / VBRK)
                 ▼
┌─────────────────────────────────┐     SWE2 Event Type Linkage
│       SWE2 Event Linkage        ├──────────────────────────┐
└─────────────────────────────────┘                          │
                                                             ▼
                                             ┌─────────────────────────────────┐
                                             │    Receiver Function Module     │
                                             │   ZSWW_WI_CREATE_VIA_EVENT_IBF  │
                                             └───────────────┬─────────────────┘
                                                             │ Validates & reads I_BillingDocument
                                                             ▼
                                             ┌─────────────────────────────────┐
                                             │       Behavior Pool Class       │
                                             │    ZBP_I_BILLINGDOCCREATED      │
                                             │      (RAISE ENTITY EVENT)       │
                                             └───────────────┬─────────────────┘
                                                             │
                                                             ▼
                                             ┌─────────────────────────────────┐
                                             │       RAP Business Event        │
                                             │  ZI_BillingDocCreated~Created   │
                                             └───────────────┬─────────────────┘
                                                             │ Mapped via Event Binding (Z_SALES_INV)
                                                             ▼
                                             ┌─────────────────────────────────┐
                                             │   Enterprise Event Enablement   │
                                             │       (/IWXBE/OUTBOUND_CFG)     │
                                             └───────────────┬─────────────────┘
                                                             │ Source-side Filtering (/IWXBE/EVENT_FILTER)
                                                             ▼
                                             ┌─────────────────────────────────┐
                                             │   SAP Event Mesh / AEM Broker   │
                                             │    (oneerp/Invoice/Created/v1)  │
                                             └─────────────────────────────────┘

2. Step 1: Trace Billing Document Creation Event (SWELS / SWEL)

Before configuring the receiver module, verify that the event is actively triggered in the system during billing document generation. In modern SAP S/4HANA systems, this event is published by the ABAP Class CL_SD_BIL_BD_EVENT (as well as legacy BOR VBRK).

  1. Activate Event Trace (SWELS): Execute transaction SWELS and turn Event Trace to Switch On.
  2. Execute Business Transaction: Create and post a billing document using transaction VF01.
  3. Verify Event Log in SWEL: Filter by Object Category CL ABAP Class, Object Type CL_SD_BIL_BD_EVENT, and Event CREATED.
ritesh_9898_4-1794939787390.png

 

 
Figure 1: SWEL Event Trace displaying CL_SD_BIL_BD_EVENT CREATED event
  1. Deactivate Event Trace (SWELS): Turn off trace logging after validation.

3. Step 2: Data Model Creation (CDS Entities)

3.1 Root View Entity: ZI_BillingDocCreated

This CDS root view entity serves as the root business object for the event, selecting from released view I_BillingDocument.

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Billing Document Created - Root Entity'
define root view entity ZI_BillingDocCreated
  as select from I_BillingDocument
{
  // Event subject key
  key BillingDocument,

  // Payload fields
  SDDocumentCategory,
  BillingDocumentType,
  OverallBillingStatus,
  BillingIssueType,
  BillingDocumentCategory,
  PayerParty,
  SoldToParty,
  SalesOrganization,
  DistributionChannel
}

3.2 Abstract Entity: ZD_BillingDocCreated (Payload & Context Filters)

This abstract entity defines the payload structure. The @event.context annotations expose context attributes directly into the CloudEvents metadata headers for source-side filtering.

@EndUserText.label: 'Billing Document Created - Event Payload'
define abstract entity ZD_BillingDocCreated
{
  SDDocumentCategory      : vbtypl;

  @Event.context          : { attribute: 'xsapbilltype', position: 1 }
  BillingDocumentType     : fkart;

  OverallBillingStatus    : vf_status;
  BillingIssueType        : vf_todo;
  BillingDocumentCategory : fktyp;
  PayerParty              : kunrg;
  SoldToParty             : kunag;

  @Event.context          : { attribute: 'xsapsalesorg', position: 2 }
  SalesOrganization       : vkorg;

  DistributionChannel     : vtweg;
}

4. Step 3: Behavior Definition (BDEF)

Declare the custom business event within the Behavior Definition for ZI_BillingDocCreated.

managed with unmanaged save implementation in class zbp_i_billingdoccreated unique;
strict ( 2 );

define behavior for ZI_BillingDocCreated
lock master
authorization master ( instance )
{
  // Declare the custom business event with the payload parameter
  event Created parameter ZD_BillingDocCreated;
}

5. Step 4: Behavior Pool Class (ZBP_I_BILLINGDOCCREATED)

RAISE ENTITY EVENT is restricted to behavior implementation classes. A public static wrapper method delegates the call to a local handler class within the behavior pool.

5.1 Global Class Definition & Implementation

CLASS zbp_i_billingdoccreated DEFINITION
  PUBLIC
  ABSTRACT
  FINAL
  FOR BEHAVIOR OF zi_billingdoccreated.

  PUBLIC SECTION.
    " Event parameter table for external callers
    TYPES tt_created TYPE TABLE FOR EVENT zi_billingdoccreated~created.

    " Public static API to trigger the event from outside the behavior pool
    CLASS-METHODS raise_created
      IMPORTING
        it_events TYPE tt_created.

ENDCLASS.

CLASS zbp_i_billingdoccreated IMPLEMENTATION.

  METHOD raise_created.
    lcl_event_handler=>on_created( it_events ).
  ENDMETHOD.

ENDCLASS.

5.2 Local Types Definition & Implementation (Local Include)

CLASS lhc_zi_billingdoccreated DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.
    METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION
      IMPORTING keys REQUEST requested_authorizations FOR ZI_BillingDocCreated RESULT result.
ENDCLASS.

CLASS lhc_zi_billingdoccreated IMPLEMENTATION.
  METHOD get_instance_authorizations.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_event_handler DEFINITION FRIENDS zbp_i_billingdoccreated.
  PUBLIC SECTION.
    CLASS-METHODS on_created
      IMPORTING
        it_events TYPE zbp_i_billingdoccreated=>tt_created.
ENDCLASS.

CLASS lcl_event_handler IMPLEMENTATION.
  METHOD on_created.
    " Raise the RAP business event
    RAISE ENTITY EVENT ZI_BillingDocCreated~Created FROM it_events.
  ENDMETHOD.
ENDCLASS.

6. Step 5: Event Binding Configuration (.evtbnd)

In ABAP Development Tools (ADT):

  1. Create a new Event Binding named Z_SALES_INV.
  2. Set Type Namespace: oneerp, SAP Object Type: Invoice, Operation: Created.
  3. Add version 0001.0.0 bound to Entity ZI_BILLINGDOCCREATED and Entity Event CREATED.
  4. Activate the artifact.
ritesh_9898_5-1794939831798.png

 

Figure 2: Event Binding Z_SALES_INV configured in ABAP Development Tools (ADT)


7. Step 6: Workflow Receiver Function Module (ZSWW_WI_CREATE_VIA_EVENT_IBF)

This function module is called by the event linkage. It validates business rules, extracts billing header data via released CDS view I_BillingDocument, populates the payload, and dispatches the event. You can write your own business logic here for trigger RAP event..

FUNCTION zsww_wi_create_via_event_ibf.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(SENDER) TYPE  SIBFLPORB
*"     VALUE(EVENT) TYPE  SIBFEVENT
*"     VALUE(RECTYPE) TYPE  SWFERECTYP
*"     VALUE(HANDLER) TYPE  SIBFLPORB
*"     VALUE(EXCEPTIONS_ALLOWED) TYPE  SWEFLAGS-EXC_OK DEFAULT SPACE
*"     VALUE(XML_SIZE) TYPE  SWF_XMLSIZ
*"     VALUE(EVENT_CONTAINER) TYPE  SWF_XMLCNT
*"  EXPORTING
*"     VALUE(RESULT) TYPE  SWFREVRSLT
*"  EXCEPTIONS
*"      READ_FAILED
*"      CREATE_FAILED
*"----------------------------------------------------------------------

  CONSTANTS: lc_elem_evt_objkey TYPE swfdname   VALUE '_EVT_OBJKEY',
             lc_elem_tcode      TYPE swfdname   VALUE '_TCODE',
             lc_tcode_vf01      TYPE tcode      VALUE 'VF01',
             lc_typeid_vbrk     TYPE sibftypeid VALUE 'VBRK',
             lc_typeid_class    TYPE sibftypeid VALUE 'CL_SD_BIL_BD_EVENT'.

  TYPES: BEGIN OF lty_billing_header,
           sddocumentcategory      TYPE i_billingdocument-sddocumentcategory,
           billingdocumenttype     TYPE i_billingdocument-billingdocumenttype,
           overallbillingstatus    TYPE i_billingdocument-overallbillingstatus,
           billingissuetype        TYPE i_billingdocument-billingissuetype,
           billingdocumentcategory TYPE i_billingdocument-billingdocumentcategory,
           payerparty              TYPE i_billingdocument-payerparty,
           soldtoparty             TYPE i_billingdocument-soldtoparty,
           salesorganization       TYPE i_billingdocument-salesorganization,
           distributionchannel     TYPE i_billingdocument-distributionchannel,
         END OF lty_billing_header.

  DATA: lwa_billing_header TYPE lty_billing_header,
        l_evt_objkey       TYPE swo_typeid,
        l_tcode            TYPE tcode,
        l_invoice          TYPE vbeln_vf,
        l_sales_order      TYPE vbeln_va,
        lo_event_container TYPE REF TO cl_swf_cnt_container,
        lt_svo_event       TYPE zbp_i_billingdoccreated=>tt_created.

  CLEAR result.

  " 1. Reconstruct event container from XML
  TRY.
      lo_event_container = cl_swf_cnt_factory=>create_from_xml(
                             im_xml_table = event_container
                             im_xml_size  = xml_size ).
    CATCH cx_swf_cnt_container.
      CLEAR lo_event_container.
  ENDTRY.

  IF lo_event_container IS NOT BOUND.
    IF exceptions_allowed = abap_true.
      RAISE read_failed.
    ENDIF.
    RETURN.
  ENDIF.

  " 2. Extract Event Parameters via Public Interface
  TRY.
      lo_event_container->if_swf_ifs_parameter_container=>get(
        EXPORTING name  = lc_elem_evt_objkey
        IMPORTING value = l_evt_objkey ).
    CATCH cx_swf_cnt_container.
      CLEAR l_evt_objkey.
  ENDTRY.

  TRY.
      lo_event_container->if_swf_ifs_parameter_container=>get(
        EXPORTING name  = lc_elem_tcode
        IMPORTING value = l_tcode ).
    CATCH cx_swf_cnt_container.
      CLEAR l_tcode.
  ENDTRY.

  " Fallback: sender instance ID carries the key
  IF l_evt_objkey IS INITIAL AND ( sender-typeid = lc_typeid_vbrk OR sender-typeid = lc_typeid_class ).
    l_evt_objkey = sender-instid.
  ENDIF.

  " 3. Transaction Gate: Process VF01 creations (allow empty tcode for background jobs)
  IF ( l_tcode <> lc_tcode_vf01 AND l_tcode IS NOT INITIAL ) OR l_evt_objkey IS INITIAL.
    RETURN.
  ENDIF.

  l_invoice = |{ l_evt_objkey(10) ALPHA = IN }|.

  " 4. Business Validation: Verify invoice references a sales document
  SELECT SINGLE salesdocument
    FROM i_billingdocumentitem
    WHERE billingdocument = @l_invoice
      AND salesdocument  <> @space
    INTO @l_sales_order.
  IF sy-subrc <> 0.
    RETURN.
  ENDIF.

  " 5. Retrieve Invoice Header Data from Released CDS View
  SELECT SINGLE sddocumentcategory,
                billingdocumenttype,
                overallbillingstatus,
                billingissuetype,
                billingdocumentcategory,
                payerparty,
                soldtoparty,
                salesorganization,
                distributionchannel
    FROM i_billingdocument
    WHERE billingdocument = @l_invoice
    INTO CORRESPONDING FIELDS OF @lwa_billing_header.

  IF sy-subrc <> 0.
    IF exceptions_allowed = abap_true.
      RAISE read_failed.
    ENDIF.
    RETURN.
  ENDIF.

  " 6. Construct Payload and Raise RAP Business Event
  lt_svo_event = VALUE #(
    ( billingdocument = l_invoice
      %param = VALUE #( sddocumentcategory      = lwa_billing_header-sddocumentcategory
                        billingdocumenttype     = lwa_billing_header-billingdocumenttype
                        overallbillingstatus    = lwa_billing_header-overallbillingstatus
                        billingissuetype        = lwa_billing_header-billingissuetype
                        billingdocumentcategory = lwa_billing_header-billingdocumentcategory
                        payerparty              = lwa_billing_header-payerparty
                        soldtoparty             = lwa_billing_header-soldtoparty
                        salesorganization       = lwa_billing_header-salesorganization
                        distributionchannel     = lwa_billing_header-distributionchannel ) ) ).

  zbp_i_billingdoccreated=>raise_created( it_events = lt_svo_event ).


ENDFUNCTION.

8. Step 7: SWE2 Event Type Linkage Configuration

Configure the linkage entry in transaction SWE2 as shown below:

Field Configuration Value Description

Object Category CL ABAP Class ABAP Class based event linkage
Object Type CL_SD_BIL_BD_EVENT Standard Billing Document Event Class
Event CREATED Billing document creation event
Receiver Type ZAIF_EVENT Custom receiver identifier
Receiver Call Function Module Function module dispatch method
Receiver Function Module ZSWW_WI_CREATE_VIA_EVENT_IBF Custom receiver function module
Destination of Receiver NONE Local RFC destination
Event delivery Using tRFC (Default) Asynchronous transactional RFC execution
Linkage Activated Checked (X) Active linkage flag
Behavior Upon Error Feedback 0 System defaults Standard error handling
Receiver Status 0 No errors Normal operational status
 
ritesh_9898_6-1794939929483.png

 

Figure 3: Transaction SWE2 Event Type Linkages Details for CL_SD_BIL_BD_EVENT

9. Step 8: Enterprise Event Enablement (/IWXBE/)

9.1 Outbound Channel Configuration (/IWXBE/OUTBOUND_CFG)

  1. Open transaction /IWXBE/OUTBOUND_CFG.
  2. Select your active channel (connected to SAP Event Mesh or Advanced Event Mesh).
  3. Under Outbound Topic Bindings, add Z_SALES_INV. The resolved topic name is oneerp/Invoice/Created/v1.

9.2 Event Filter Configuration (/IWXBE/EVENT_FILTER)

Because the context annotations were declared on ZD_BillingDocCreated, the attributes xsapbilltype and xsapsalesorg are available as selectable filter conditions.

  1. Open transaction /IWXBE/EVENT_FILTER.
  2. Select topic oneerp/Invoice/Created/v1.
  3. Add filter condition: Property xsapbilltype equals F2 (Standard Invoice).
  4. Save the configuration. Non-matching invoices are discarded on the S/4HANA source side.
ritesh_9898_7-1794939961012.png

 

Figure 4: Transaction /IWXBE/EVENT_FILTER with context filter attributes xsapbilltype and xsapsalesorg

10. Step 9: Testing & Verification Checklist

Step Action Transaction / Tool Expected Result

1. Create Document Create and save an invoice referencing a sales order. VF01 Billing document posted successfully.
2. Check Event Trace Verify event linkage execution. SWEL Event CL_SD_BIL_BD_EVENT.CREATED shows receiver FM executed with return code 0.
3. Check Event Monitor Verify outbound broker delivery. /IWXBE/EVENT_MONITOR Outbound event oneerp/Invoice/Created/v1 is logged as Sent / Acknowledged.
4. Verify Filter Gate Post an invoice with billing document type other than F2. VF01 / /IWXBE/EVENT_MONITOR Event is processed by receiver FM but discarded by /IWXBE/ without publication.

11. Conclusion & Summary

In this guide, we explored how to design and implement an end-to-end, hybrid event-driven architecture in SAP S/4HANA for billing document creation. By leveraging standard SWE2 Event Type Linkage triggered from SD billing creation (CL_SD_BIL_BD_EVENT), we routed the transaction through a custom receiver function module (ZSWW_WI_CREATE_VIA_EVENT_IBF) to perform business validations and enrich the event payload using released Core Data Services (I_BillingDocument). We then bridged traditional workflow events into the modern ABAP RESTful Application Programming Model (RAP) via behavior pools and an Event Binding (.evtbnd) artifact. Finally, using SAP Enterprise Event Enablement (EEE), we configured efficient source-side filtering on custom CloudEvents context attributes (xsapbilltype, xsapsalesorg) before publishing to SAP Integration Suite, Advanced Event Mesh (AEM). This architectural pattern guarantees that downstream consumers receive timely, enriched, and validated business events while optimizing network throughput and SAP system resources.

“}]] 

  Read More Technology Blog Posts by Members articles 

#abap

EEE :Custom Business Event for Billing Document Creation with Custom Payload and Context Filters
Share

[[{“value”:”

 

Technical Specification & Implementation Guide

Custom Business Event for Billing Document Creation with Custom Payload and Context Filters


1. Solution Architecture Overview

This solution establishes a hybrid integration pattern in SAP S/4HANA: billing document creation events trigger a custom workflow receiver function module via Event Type Linkage (SWE2), which filters business transactions, validates sales order references, enriches header data using released Core Data Services (CDS), and raises an ABAP RESTful Application Programming Model (RAP) Business Event.

The event is bound via an Event Binding artifact (.evtbnd) and published to external event brokers (such as SAP Event Mesh / SAP Integration Suite, Advanced Event Mesh) using SAP Enterprise Event Enablement (/IWXBE/), utilizing context annotations for source-side topic filtering.

┌─────────────────────────────────┐
│ SD Billing Creation (e.g. VF01) │
└────────────────┬────────────────┘
                 │ Raises Event CREATED (Class CL_SD_BIL_BD_EVENT / VBRK)
                 ▼
┌─────────────────────────────────┐     SWE2 Event Type Linkage
│       SWE2 Event Linkage        ├──────────────────────────┐
└─────────────────────────────────┘                          │
                                                             ▼
                                             ┌─────────────────────────────────┐
                                             │    Receiver Function Module     │
                                             │   ZSWW_WI_CREATE_VIA_EVENT_IBF  │
                                             └───────────────┬─────────────────┘
                                                             │ Validates & reads I_BillingDocument
                                                             ▼
                                             ┌─────────────────────────────────┐
                                             │       Behavior Pool Class       │
                                             │    ZBP_I_BILLINGDOCCREATED      │
                                             │      (RAISE ENTITY EVENT)       │
                                             └───────────────┬─────────────────┘
                                                             │
                                                             ▼
                                             ┌─────────────────────────────────┐
                                             │       RAP Business Event        │
                                             │  ZI_BillingDocCreated~Created   │
                                             └───────────────┬─────────────────┘
                                                             │ Mapped via Event Binding (Z_SALES_INV)
                                                             ▼
                                             ┌─────────────────────────────────┐
                                             │   Enterprise Event Enablement   │
                                             │       (/IWXBE/OUTBOUND_CFG)     │
                                             └───────────────┬─────────────────┘
                                                             │ Source-side Filtering (/IWXBE/EVENT_FILTER)
                                                             ▼
                                             ┌─────────────────────────────────┐
                                             │   SAP Event Mesh / AEM Broker   │
                                             │    (oneerp/Invoice/Created/v1)  │
                                             └─────────────────────────────────┘

2. Step 1: Trace Billing Document Creation Event (SWELS / SWEL)

Before configuring the receiver module, verify that the event is actively triggered in the system during billing document generation. In modern SAP S/4HANA systems, this event is published by the ABAP Class CL_SD_BIL_BD_EVENT (as well as legacy BOR VBRK).

  1. Activate Event Trace (SWELS): Execute transaction SWELS and turn Event Trace to Switch On.
  2. Execute Business Transaction: Create and post a billing document using transaction VF01.
  3. Verify Event Log in SWEL: Filter by Object Category CL ABAP Class, Object Type CL_SD_BIL_BD_EVENT, and Event CREATED.
ritesh_9898_4-1794939787390.png

 

 
Figure 1: SWEL Event Trace displaying CL_SD_BIL_BD_EVENT CREATED event
  1. Deactivate Event Trace (SWELS): Turn off trace logging after validation.

3. Step 2: Data Model Creation (CDS Entities)

3.1 Root View Entity: ZI_BillingDocCreated

This CDS root view entity serves as the root business object for the event, selecting from released view I_BillingDocument.

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Billing Document Created - Root Entity'
define root view entity ZI_BillingDocCreated
  as select from I_BillingDocument
{
  // Event subject key
  key BillingDocument,

  // Payload fields
  SDDocumentCategory,
  BillingDocumentType,
  OverallBillingStatus,
  BillingIssueType,
  BillingDocumentCategory,
  PayerParty,
  SoldToParty,
  SalesOrganization,
  DistributionChannel
}

3.2 Abstract Entity: ZD_BillingDocCreated (Payload & Context Filters)

This abstract entity defines the payload structure. The @event.context annotations expose context attributes directly into the CloudEvents metadata headers for source-side filtering.

@EndUserText.label: 'Billing Document Created - Event Payload'
define abstract entity ZD_BillingDocCreated
{
  SDDocumentCategory      : vbtypl;

  @Event.context          : { attribute: 'xsapbilltype', position: 1 }
  BillingDocumentType     : fkart;

  OverallBillingStatus    : vf_status;
  BillingIssueType        : vf_todo;
  BillingDocumentCategory : fktyp;
  PayerParty              : kunrg;
  SoldToParty             : kunag;

  @Event.context          : { attribute: 'xsapsalesorg', position: 2 }
  SalesOrganization       : vkorg;

  DistributionChannel     : vtweg;
}

4. Step 3: Behavior Definition (BDEF)

Declare the custom business event within the Behavior Definition for ZI_BillingDocCreated.

managed with unmanaged save implementation in class zbp_i_billingdoccreated unique;
strict ( 2 );

define behavior for ZI_BillingDocCreated
lock master
authorization master ( instance )
{
  // Declare the custom business event with the payload parameter
  event Created parameter ZD_BillingDocCreated;
}

5. Step 4: Behavior Pool Class (ZBP_I_BILLINGDOCCREATED)

RAISE ENTITY EVENT is restricted to behavior implementation classes. A public static wrapper method delegates the call to a local handler class within the behavior pool.

5.1 Global Class Definition & Implementation

CLASS zbp_i_billingdoccreated DEFINITION
  PUBLIC
  ABSTRACT
  FINAL
  FOR BEHAVIOR OF zi_billingdoccreated.

  PUBLIC SECTION.
    " Event parameter table for external callers
    TYPES tt_created TYPE TABLE FOR EVENT zi_billingdoccreated~created.

    " Public static API to trigger the event from outside the behavior pool
    CLASS-METHODS raise_created
      IMPORTING
        it_events TYPE tt_created.

ENDCLASS.

CLASS zbp_i_billingdoccreated IMPLEMENTATION.

  METHOD raise_created.
    lcl_event_handler=>on_created( it_events ).
  ENDMETHOD.

ENDCLASS.

5.2 Local Types Definition & Implementation (Local Include)

CLASS lhc_zi_billingdoccreated DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.
    METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION
      IMPORTING keys REQUEST requested_authorizations FOR ZI_BillingDocCreated RESULT result.
ENDCLASS.

CLASS lhc_zi_billingdoccreated IMPLEMENTATION.
  METHOD get_instance_authorizations.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_event_handler DEFINITION FRIENDS zbp_i_billingdoccreated.
  PUBLIC SECTION.
    CLASS-METHODS on_created
      IMPORTING
        it_events TYPE zbp_i_billingdoccreated=>tt_created.
ENDCLASS.

CLASS lcl_event_handler IMPLEMENTATION.
  METHOD on_created.
    " Raise the RAP business event
    RAISE ENTITY EVENT ZI_BillingDocCreated~Created FROM it_events.
  ENDMETHOD.
ENDCLASS.

6. Step 5: Event Binding Configuration (.evtbnd)

In ABAP Development Tools (ADT):

  1. Create a new Event Binding named Z_SALES_INV.
  2. Set Type Namespace: oneerp, SAP Object Type: Invoice, Operation: Created.
  3. Add version 0001.0.0 bound to Entity ZI_BILLINGDOCCREATED and Entity Event CREATED.
  4. Activate the artifact.
ritesh_9898_5-1794939831798.png

 

Figure 2: Event Binding Z_SALES_INV configured in ABAP Development Tools (ADT)


7. Step 6: Workflow Receiver Function Module (ZSWW_WI_CREATE_VIA_EVENT_IBF)

This function module is called by the event linkage. It validates business rules, extracts billing header data via released CDS view I_BillingDocument, populates the payload, and dispatches the event. You can write your own business logic here for trigger RAP event..

FUNCTION zsww_wi_create_via_event_ibf.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(SENDER) TYPE  SIBFLPORB
*"     VALUE(EVENT) TYPE  SIBFEVENT
*"     VALUE(RECTYPE) TYPE  SWFERECTYP
*"     VALUE(HANDLER) TYPE  SIBFLPORB
*"     VALUE(EXCEPTIONS_ALLOWED) TYPE  SWEFLAGS-EXC_OK DEFAULT SPACE
*"     VALUE(XML_SIZE) TYPE  SWF_XMLSIZ
*"     VALUE(EVENT_CONTAINER) TYPE  SWF_XMLCNT
*"  EXPORTING
*"     VALUE(RESULT) TYPE  SWFREVRSLT
*"  EXCEPTIONS
*"      READ_FAILED
*"      CREATE_FAILED
*"----------------------------------------------------------------------

  CONSTANTS: lc_elem_evt_objkey TYPE swfdname   VALUE '_EVT_OBJKEY',
             lc_elem_tcode      TYPE swfdname   VALUE '_TCODE',
             lc_tcode_vf01      TYPE tcode      VALUE 'VF01',
             lc_typeid_vbrk     TYPE sibftypeid VALUE 'VBRK',
             lc_typeid_class    TYPE sibftypeid VALUE 'CL_SD_BIL_BD_EVENT'.

  TYPES: BEGIN OF lty_billing_header,
           sddocumentcategory      TYPE i_billingdocument-sddocumentcategory,
           billingdocumenttype     TYPE i_billingdocument-billingdocumenttype,
           overallbillingstatus    TYPE i_billingdocument-overallbillingstatus,
           billingissuetype        TYPE i_billingdocument-billingissuetype,
           billingdocumentcategory TYPE i_billingdocument-billingdocumentcategory,
           payerparty              TYPE i_billingdocument-payerparty,
           soldtoparty             TYPE i_billingdocument-soldtoparty,
           salesorganization       TYPE i_billingdocument-salesorganization,
           distributionchannel     TYPE i_billingdocument-distributionchannel,
         END OF lty_billing_header.

  DATA: lwa_billing_header TYPE lty_billing_header,
        l_evt_objkey       TYPE swo_typeid,
        l_tcode            TYPE tcode,
        l_invoice          TYPE vbeln_vf,
        l_sales_order      TYPE vbeln_va,
        lo_event_container TYPE REF TO cl_swf_cnt_container,
        lt_svo_event       TYPE zbp_i_billingdoccreated=>tt_created.

  CLEAR result.

  " 1. Reconstruct event container from XML
  TRY.
      lo_event_container = cl_swf_cnt_factory=>create_from_xml(
                             im_xml_table = event_container
                             im_xml_size  = xml_size ).
    CATCH cx_swf_cnt_container.
      CLEAR lo_event_container.
  ENDTRY.

  IF lo_event_container IS NOT BOUND.
    IF exceptions_allowed = abap_true.
      RAISE read_failed.
    ENDIF.
    RETURN.
  ENDIF.

  " 2. Extract Event Parameters via Public Interface
  TRY.
      lo_event_container->if_swf_ifs_parameter_container=>get(
        EXPORTING name  = lc_elem_evt_objkey
        IMPORTING value = l_evt_objkey ).
    CATCH cx_swf_cnt_container.
      CLEAR l_evt_objkey.
  ENDTRY.

  TRY.
      lo_event_container->if_swf_ifs_parameter_container=>get(
        EXPORTING name  = lc_elem_tcode
        IMPORTING value = l_tcode ).
    CATCH cx_swf_cnt_container.
      CLEAR l_tcode.
  ENDTRY.

  " Fallback: sender instance ID carries the key
  IF l_evt_objkey IS INITIAL AND ( sender-typeid = lc_typeid_vbrk OR sender-typeid = lc_typeid_class ).
    l_evt_objkey = sender-instid.
  ENDIF.

  " 3. Transaction Gate: Process VF01 creations (allow empty tcode for background jobs)
  IF ( l_tcode <> lc_tcode_vf01 AND l_tcode IS NOT INITIAL ) OR l_evt_objkey IS INITIAL.
    RETURN.
  ENDIF.

  l_invoice = |{ l_evt_objkey(10) ALPHA = IN }|.

  " 4. Business Validation: Verify invoice references a sales document
  SELECT SINGLE salesdocument
    FROM i_billingdocumentitem
    WHERE billingdocument = @l_invoice
      AND salesdocument  <> @space
    INTO @l_sales_order.
  IF sy-subrc <> 0.
    RETURN.
  ENDIF.

  " 5. Retrieve Invoice Header Data from Released CDS View
  SELECT SINGLE sddocumentcategory,
                billingdocumenttype,
                overallbillingstatus,
                billingissuetype,
                billingdocumentcategory,
                payerparty,
                soldtoparty,
                salesorganization,
                distributionchannel
    FROM i_billingdocument
    WHERE billingdocument = @l_invoice
    INTO CORRESPONDING FIELDS OF @lwa_billing_header.

  IF sy-subrc <> 0.
    IF exceptions_allowed = abap_true.
      RAISE read_failed.
    ENDIF.
    RETURN.
  ENDIF.

  " 6. Construct Payload and Raise RAP Business Event
  lt_svo_event = VALUE #(
    ( billingdocument = l_invoice
      %param = VALUE #( sddocumentcategory      = lwa_billing_header-sddocumentcategory
                        billingdocumenttype     = lwa_billing_header-billingdocumenttype
                        overallbillingstatus    = lwa_billing_header-overallbillingstatus
                        billingissuetype        = lwa_billing_header-billingissuetype
                        billingdocumentcategory = lwa_billing_header-billingdocumentcategory
                        payerparty              = lwa_billing_header-payerparty
                        soldtoparty             = lwa_billing_header-soldtoparty
                        salesorganization       = lwa_billing_header-salesorganization
                        distributionchannel     = lwa_billing_header-distributionchannel ) ) ).

  zbp_i_billingdoccreated=>raise_created( it_events = lt_svo_event ).


ENDFUNCTION.

8. Step 7: SWE2 Event Type Linkage Configuration

Configure the linkage entry in transaction SWE2 as shown below:

Field Configuration Value Description

Object Category CL ABAP Class ABAP Class based event linkage
Object Type CL_SD_BIL_BD_EVENT Standard Billing Document Event Class
Event CREATED Billing document creation event
Receiver Type ZAIF_EVENT Custom receiver identifier
Receiver Call Function Module Function module dispatch method
Receiver Function Module ZSWW_WI_CREATE_VIA_EVENT_IBF Custom receiver function module
Destination of Receiver NONE Local RFC destination
Event delivery Using tRFC (Default) Asynchronous transactional RFC execution
Linkage Activated Checked (X) Active linkage flag
Behavior Upon Error Feedback 0 System defaults Standard error handling
Receiver Status 0 No errors Normal operational status
 
ritesh_9898_6-1794939929483.png

 

Figure 3: Transaction SWE2 Event Type Linkages Details for CL_SD_BIL_BD_EVENT

9. Step 8: Enterprise Event Enablement (/IWXBE/)

9.1 Outbound Channel Configuration (/IWXBE/OUTBOUND_CFG)

  1. Open transaction /IWXBE/OUTBOUND_CFG.
  2. Select your active channel (connected to SAP Event Mesh or Advanced Event Mesh).
  3. Under Outbound Topic Bindings, add Z_SALES_INV. The resolved topic name is oneerp/Invoice/Created/v1.

9.2 Event Filter Configuration (/IWXBE/EVENT_FILTER)

Because the context annotations were declared on ZD_BillingDocCreated, the attributes xsapbilltype and xsapsalesorg are available as selectable filter conditions.

  1. Open transaction /IWXBE/EVENT_FILTER.
  2. Select topic oneerp/Invoice/Created/v1.
  3. Add filter condition: Property xsapbilltype equals F2 (Standard Invoice).
  4. Save the configuration. Non-matching invoices are discarded on the S/4HANA source side.
ritesh_9898_7-1794939961012.png

 

Figure 4: Transaction /IWXBE/EVENT_FILTER with context filter attributes xsapbilltype and xsapsalesorg

10. Step 9: Testing & Verification Checklist

Step Action Transaction / Tool Expected Result

1. Create Document Create and save an invoice referencing a sales order. VF01 Billing document posted successfully.
2. Check Event Trace Verify event linkage execution. SWEL Event CL_SD_BIL_BD_EVENT.CREATED shows receiver FM executed with return code 0.
3. Check Event Monitor Verify outbound broker delivery. /IWXBE/EVENT_MONITOR Outbound event oneerp/Invoice/Created/v1 is logged as Sent / Acknowledged.
4. Verify Filter Gate Post an invoice with billing document type other than F2. VF01 / /IWXBE/EVENT_MONITOR Event is processed by receiver FM but discarded by /IWXBE/ without publication.

11. Conclusion & Summary

In this guide, we explored how to design and implement an end-to-end, hybrid event-driven architecture in SAP S/4HANA for billing document creation. By leveraging standard SWE2 Event Type Linkage triggered from SD billing creation (CL_SD_BIL_BD_EVENT), we routed the transaction through a custom receiver function module (ZSWW_WI_CREATE_VIA_EVENT_IBF) to perform business validations and enrich the event payload using released Core Data Services (I_BillingDocument). We then bridged traditional workflow events into the modern ABAP RESTful Application Programming Model (RAP) via behavior pools and an Event Binding (.evtbnd) artifact. Finally, using SAP Enterprise Event Enablement (EEE), we configured efficient source-side filtering on custom CloudEvents context attributes (xsapbilltype, xsapsalesorg) before publishing to SAP Integration Suite, Advanced Event Mesh (AEM). This architectural pattern guarantees that downstream consumers receive timely, enriched, and validated business events while optimizing network throughput and SAP system resources.

“}]] 

  Read More Technology Blog Posts by Members articles 

#abap

By ali

Leave a Reply