$npx -y skills add likweitan/abap-skills --skill rap-business-eventsHelp with RAP business events and enterprise eventing including event definitions in behavior definitions, raising events from RAP handler methods, event bindings, SAP Event Mesh integration, event consumption, and event-driven architecture patterns in ABAP Cloud. Use when users
| 1 | # RAP Business Events & Enterprise Eventing |
| 2 | |
| 3 | Guide for implementing event-driven patterns using RAP business events and SAP Event Mesh in ABAP Cloud. |
| 4 | |
| 5 | ## Workflow |
| 6 | |
| 7 | 1. **Determine the user's goal**: |
| 8 | - Defining business events in a RAP BO |
| 9 | - Raising events from RAP handler methods |
| 10 | - Binding events for consumption |
| 11 | - Consuming events from external systems |
| 12 | - Integrating with SAP Event Mesh |
| 13 | - Understanding event-driven architecture in ABAP |
| 14 | |
| 15 | 2. **Identify the scenario**: |
| 16 | - Local event (within the same ABAP system) |
| 17 | - Enterprise event (cross-system via Event Mesh) |
| 18 | - Event producer vs. event consumer |
| 19 | |
| 20 | 3. **Guide implementation** following RAP eventing patterns |
| 21 | |
| 22 | ## Business Events Overview |
| 23 | |
| 24 | | Concept | Description | |
| 25 | | --------------------- | ----------------------------------------------------------------- | |
| 26 | | **Business Event** | Declared in BDEF; raised when something significant happens | |
| 27 | | **Event Definition** | Formal declaration with parameters in the behavior definition | |
| 28 | | **Event Raising** | Triggered in handler/saver methods via `RAISE ENTITY EVENT` | |
| 29 | | **Event Binding** | Maps RAP event to an enterprise event topic for external delivery | |
| 30 | | **Event Consumption** | External systems subscribe and react to published events | |
| 31 | |
| 32 | ## Defining Business Events |
| 33 | |
| 34 | ### In the Behavior Definition (BDL) |
| 35 | |
| 36 | ``` |
| 37 | managed implementation in class zbp_r_travel unique; |
| 38 | strict ( 2 ); |
| 39 | |
| 40 | define behavior for ZR_Travel alias Travel |
| 41 | persistent table ztravel_tab |
| 42 | lock master |
| 43 | authorization master ( instance ) |
| 44 | etag master LocalLastChangedAt |
| 45 | { |
| 46 | create; |
| 47 | update; |
| 48 | delete; |
| 49 | |
| 50 | "Define business events |
| 51 | event travel_created parameter ZD_TravelCreatedEvt; |
| 52 | event travel_accepted; |
| 53 | event travel_rejected; |
| 54 | } |
| 55 | ``` |
| 56 | |
| 57 | ### Event Parameter Structure |
| 58 | |
| 59 | Define a CDS abstract entity for the event payload: |
| 60 | |
| 61 | ```cds |
| 62 | @EndUserText.label: 'Travel Created Event' |
| 63 | define abstract entity ZD_TravelCreatedEvt |
| 64 | { |
| 65 | travel_id : /dmo/travel_id; |
| 66 | agency_id : /dmo/agency_id; |
| 67 | customer_id : /dmo/customer_id; |
| 68 | description : /dmo/description; |
| 69 | total_price : /dmo/total_price; |
| 70 | currency : /dmo/currency_code; |
| 71 | } |
| 72 | ``` |
| 73 | |
| 74 | Events without the `parameter` addition have no payload. |
| 75 | |
| 76 | ## Raising Business Events |
| 77 | |
| 78 | ### In Handler Methods |
| 79 | |
| 80 | ```abap |
| 81 | METHOD on_travel_accept. |
| 82 | "Read travel data |
| 83 | READ ENTITIES OF zr_travel IN LOCAL MODE |
| 84 | ENTITY Travel |
| 85 | ALL FIELDS |
| 86 | WITH CORRESPONDING #( keys ) |
| 87 | RESULT DATA(lt_travels). |
| 88 | |
| 89 | "Update status |
| 90 | MODIFY ENTITIES OF zr_travel IN LOCAL MODE |
| 91 | ENTITY Travel |
| 92 | UPDATE FIELDS ( status ) |
| 93 | WITH VALUE #( FOR travel IN lt_travels |
| 94 | ( %tky = travel-%tky |
| 95 | status = 'A' ) ) |
| 96 | REPORTED DATA(lt_reported). |
| 97 | |
| 98 | "Raise event for each accepted travel |
| 99 | RAISE ENTITY EVENT zr_travel~travel_accepted |
| 100 | FROM VALUE #( FOR travel IN lt_travels |
| 101 | ( %key = travel-%key ) ). |
| 102 | ENDMETHOD. |
| 103 | ``` |
| 104 | |
| 105 | ### With Event Parameters |
| 106 | |
| 107 | ```abap |
| 108 | METHOD on_travel_create. |
| 109 | "After successful creation |
| 110 | RAISE ENTITY EVENT zr_travel~travel_created |
| 111 | FROM VALUE #( FOR travel IN lt_created_travels |
| 112 | ( %key = travel-%key |
| 113 | %param = VALUE #( |
| 114 | travel_id = travel-travel_id |
| 115 | agency_id = travel-agency_id |
| 116 | customer_id = travel-customer_id |
| 117 | description = travel-description |
| 118 | total_price = travel-total_price |
| 119 | currency = travel-currency_code ) ) ). |
| 120 | ENDMETHOD. |
| 121 | ``` |
| 122 | |
| 123 | ### In Saver Methods (Additional Save) |
| 124 | |
| 125 | ```abap |
| 126 | METHOD save_modified. |
| 127 | "Raise events in the save phase for committed data |
| 128 | IF create-travel IS NOT INITIAL. |
| 129 | RAISE ENTITY EVENT zr_travel~travel_created |
| 130 | FROM VALUE #( FOR travel IN create-travel |
| 131 | ( %key = travel-%key |
| 132 | %param = VALUE #( |
| 133 | travel_id = travel-travel_id ) ) ). |
| 134 | ENDIF. |
| 135 | ENDMETHOD. |
| 136 | ``` |
| 137 | |
| 138 | ## Event Processing Flow |
| 139 | |
| 140 | ``` |
| 141 | 1. User action triggers RAP operation |
| 142 | 2. Handler method executes business logic |
| 143 | 3. RAISE ENTITY EVENT queues the event |
| 144 | 4. RAP framework commits the transaction |
| 145 | 5. After successful COMMIT: |
| 146 | a. Local event handlers are called |
| 147 | b. Enterprise events are published to Event Mesh |
| 148 | ``` |
| 149 | |
| 150 | ## Enterprise Event Enablement |
| 151 | |
| 152 | ### Event Binding |
| 153 | |
| 154 | To publish RAP events externally, create an event bi |