RAP Events

Estimated read time 10 min read

SAP RAP (ABAP RESTful Application Programming Model) supports events to handle business logic at different points in an application’s lifecycle. These events help in executing specific actions automatically or on-demand.  

Types of Events in RAP Implicit Events (Triggered Automatically) 

These are predefined events triggered by the RAP framework during different phases of the transaction.  

      Before-Save Event  

Occurs before the transaction is committed.  Used for validations, automatic field updates, or derivations Example: Auto-filling a timestamp before saving a record. 

       After-Save Event  

Occurs after the transaction is committed.  Used for notifications, logging, or triggering workflows Example: Sending an email after a purchase order is approved. 

 

     Explicit Events (Triggered Manually) 

These are custom events defined in the RAP behavior definition and triggered explicitly in the business logic.  

Definition: Declared in the behavior definition file (. behavior).  Triggering: Raised in an ABAP class using RAISE EVENT Consumption: Handled event handlers inside RAP behavior implementation classes. 

 Defining & Using RAP Events – Example 

Step 1: Define an Event in Behavior Definition 

Create an explicit event inside the RAP behavior definition (.behavior).  

 Step 2: Trigger the Event in the Behavior Implementation Class 

Raise the event inside an implementation class.  

Step 3: Handle the Event in Event Handler 

Define an event handler to execute logic when the event is raised.  

Use Cases of RAP Events 

Event Type 

Use Case Example 

Before-Save 

Validate input data before saving a sales order. 

After-Save 

Trigger a workflow after a new employee record is created. 

Explicit Event 

Notify external systems when an invoice is finalized. 

                                         

Scenario. 

Scenario: Automatic Order Confirmation Based on Stock Availability.   

Business Logic 

– When a Sales Order (SO) is created, a BEFORE SAVE event checks stock levels.   

– If stock is available, the SO is automatically confirmed (`CONFIRMED`) .   

– If stock is not available, the SO remains pending (`PENDING`) until replenishment.   

 Sales Table. 

 

@EndUserText.label : ‘sales’
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table znp_dt_sales {

key client : abap.clnt not null;
key salesorderid : abap.numc(10) not null;
itemid : abap.numc(5);
materialid : abap.char(18);
quantity : abap.dec(13,3);
currencycode : abap.cuky;
totalamount : abap.dec(15,2);
status : abap.char(10);
created_by : abp_creation_user;
created_at : abp_creation_tstmpl;
last_changed_by : abp_locinst_lastchange_user;
last_changed_at : abp_locinst_lastchange_tstmpl;

}

 

Stocks table.

 

@EndUserText.label : ‘stocks’
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table znp_dt_stock {

key client : abap.clnt not null;
key materialid : abap.char(18) not null;
plant : abap.char(4) not null;
stock : abap.dec(13,3) not null;
uom : abap.unit(3) not null;
last_updated_by : abap.sstring(12) not null;
last_updated_on : abap.dats not null;

}

 

CDS  View Entity.

 

@AccessControl.authorizationCheck: #CHECK
@Metadata.allowExtensions: true
@EndUserText.label: ‘###GENERATED Core Data Service Entity’
define root view entity ZR_NP_DT_SALES
as select from ZNP_DT_SALES
{
key salesorderid as Salesorderid,
itemid as Itemid,
materialid as Materialid,
quantity as Quantity,
@Consumption.valueHelpDefinition: [ {
entity.name: ‘I_CurrencyStdVH’,
entity.element: ‘Currency’,
useForValidation: true
} ]
currencycode as Currencycode,
totalamount as Totalamount,
status as Status,
@Semantics.user.createdBy: true
created_by as CreatedBy,
@Semantics.systemDateTime.createdAt: true
created_at as CreatedAt,
@Semantics.user.localInstanceLastChangedBy: true
last_changed_by as LastChangedBy,
@Semantics.systemDateTime.localInstanceLastChangedAt: true
last_changed_at as LastChangedAt

}

 

Abstract entity For Define the parameters of event.

 

@EndUserText.label: ‘abstract ntity’
define abstract entity znp_s_eventsc

{
Message : abap.char( 50 );
}

 

Behavior Defintion.

 

managed implementation in class ZBP_R_NP_DT_SALES unique;
strict ( 2 );
with draft;
define behavior for ZR_NP_DT_SALES alias ZrNpDtSales
persistent table znp_dt_sales
draft table znp_dt_sales_d
etag master LastChangedAt
early numbering
lock master total etag LastChangedAt
authorization master ( global )
with additional save

{

field ( readonly )
CreatedBy,
CreatedAt,
LastChangedBy,
Salesorderid,
Status,
LastChangedAt;

action confirm_order parameter znp_s_eventsc;
event C_order parameter znp_s_eventsc;
create;
update;
delete;

draft action Activate optimized;
draft action Discard;
draft action Edit;
draft action Resume;
draft determine action Prepare;

mapping for znp_dt_sales
{
Salesorderid = salesorderid;
Itemid = itemid;
Materialid = materialid;
Quantity = quantity;
Currencycode = currencycode;
Totalamount = totalamount;
Status = status;
CreatedBy = created_by;
CreatedAt = created_at;
LastChangedBy = last_changed_by;
LastChangedAt = last_changed_at;
}
}

 

 

In the class save_modified method raise the event if stock is not available,it will

give message to user materials is out of stock.

 

 

IF create-zrnpdtsales IS NOT INITIAL .

DATA ls_tab TYPE znp_dt_sales.
DATA(ls_c) = VALUE #( lt_c[ 1 ] OPTIONAL ) .
*

READ ENTITIES OF zr_np_dt_sales IN LOCAL MODE
ENTITY ZrNpDtSales
ALL FIELDS WITH VALUE #( ( %key = ls_c-%key ) )
RESULT DATA(lt_sales).

DATA(ls_sales) = lt_sales[ 1 ].

SELECT SINGLE FROM znp_dt_stock FIELDS stock
WHERE materialid = _sales-Materialid
INTO (lv_stock).

IF ls_sales-Quantity > lv_stock.

RAISE ENTITY EVENT zr_np_dt_sales~C_order
FROM VALUE #( ( %key = ls_u-%key
%param = VALUE #( Message =
‘Sales order is Pending Due to low stock’ ) ) ).
endif.

 

 

In  the event handler It will give the message.

 

CLASS lcl_local_events DEFINITION INHERITING FROM
cl_abap_behavior_event_handler .
PRIVATE SECTION.
METHODS confirm_order
FOR ENTITY EVENT it_parameters FOR ZrNpDtSales~C_order.

Endclass.

Method confirm_order.
data(ls_param) = value #( it_paramter[ 1 ] optional ).
APPEND VALUE #( %tky = ls_sales-%tky
%msg = me->new_message_with_text(
severity = if_abap_behv_message=>severity-error
text = ls_param-message
) ) TO reported-zrnpdtsales.

 

while creating order if the if the quantity is more than the stocks it will give message

that stocks is not available, status is set to pending.

 

 

 

 

 

 

 

 

 

 

 

​ SAP RAP (ABAP RESTful Application Programming Model) supports events to handle business logic at different points in an application’s lifecycle. These events help in executing specific actions automatically or on-demand.  Types of Events in RAP Implicit Events (Triggered Automatically) These are predefined events triggered by the RAP framework during different phases of the transaction.        Before-Save Event  Occurs before the transaction is committed.  Used for validations, automatic field updates, or derivations.  Example: Auto-filling a timestamp before saving a record.        After-Save Event  Occurs after the transaction is committed.  Used for notifications, logging, or triggering workflows.  Example: Sending an email after a purchase order is approved.       Explicit Events (Triggered Manually) These are custom events defined in the RAP behavior definition and triggered explicitly in the business logic.  Definition: Declared in the behavior definition file (. behavior).  Triggering: Raised in an ABAP class using RAISE EVENT.  Consumption: Handled event handlers inside RAP behavior implementation classes.  Defining & Using RAP Events – Example Step 1: Define an Event in Behavior Definition Create an explicit event inside the RAP behavior definition (.behavior).   Step 2: Trigger the Event in the Behavior Implementation Class Raise the event inside an implementation class.  Step 3: Handle the Event in Event Handler Define an event handler to execute logic when the event is raised.  Use Cases of RAP Events Event Type Use Case Example Before-Save Validate input data before saving a sales order. After-Save Trigger a workflow after a new employee record is created. Explicit Event Notify external systems when an invoice is finalized.                                          Scenario. Scenario: Automatic Order Confirmation Based on Stock Availability.   Business Logic – When a Sales Order (SO) is created, a BEFORE SAVE event checks stock levels.   – If stock is available, the SO is automatically confirmed (`CONFIRMED`) .   – If stock is not available, the SO remains pending (`PENDING`) until replenishment.    Sales Table.  @EndUserText.label : ‘sales’
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table znp_dt_sales {

key client : abap.clnt not null;
key salesorderid : abap.numc(10) not null;
itemid : abap.numc(5);
materialid : abap.char(18);
quantity : abap.dec(13,3);
currencycode : abap.cuky;
totalamount : abap.dec(15,2);
status : abap.char(10);
created_by : abp_creation_user;
created_at : abp_creation_tstmpl;
last_changed_by : abp_locinst_lastchange_user;
last_changed_at : abp_locinst_lastchange_tstmpl;

} Stocks table. @EndUserText.label : ‘stocks’
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table znp_dt_stock {

key client : abap.clnt not null;
key materialid : abap.char(18) not null;
plant : abap.char(4) not null;
stock : abap.dec(13,3) not null;
uom : abap.unit(3) not null;
last_updated_by : abap.sstring(12) not null;
last_updated_on : abap.dats not null;

} CDS  View Entity. @AccessControl.authorizationCheck: #CHECK
@Metadata.allowExtensions: true
@EndUserText.label: ‘###GENERATED Core Data Service Entity’
define root view entity ZR_NP_DT_SALES
as select from ZNP_DT_SALES
{
key salesorderid as Salesorderid,
itemid as Itemid,
materialid as Materialid,
quantity as Quantity,
@Consumption.valueHelpDefinition: [ {
entity.name: ‘I_CurrencyStdVH’,
entity.element: ‘Currency’,
useForValidation: true
} ]
currencycode as Currencycode,
totalamount as Totalamount,
status as Status,
@Semantics.user.createdBy: true
created_by as CreatedBy,
@Semantics.systemDateTime.createdAt: true
created_at as CreatedAt,
@Semantics.user.localInstanceLastChangedBy: true
last_changed_by as LastChangedBy,
@Semantics.systemDateTime.localInstanceLastChangedAt: true
last_changed_at as LastChangedAt

} Abstract entity For Define the parameters of event. @EndUserText.label: ‘abstract ntity’
define abstract entity znp_s_eventsc

{
Message : abap.char( 50 );
} Behavior Defintion. managed implementation in class ZBP_R_NP_DT_SALES unique;
strict ( 2 );
with draft;
define behavior for ZR_NP_DT_SALES alias ZrNpDtSales
persistent table znp_dt_sales
draft table znp_dt_sales_d
etag master LastChangedAt
early numbering
lock master total etag LastChangedAt
authorization master ( global )
with additional save

{

field ( readonly )
CreatedBy,
CreatedAt,
LastChangedBy,
Salesorderid,
Status,
LastChangedAt;

action confirm_order parameter znp_s_eventsc;
event C_order parameter znp_s_eventsc;
create;
update;
delete;

draft action Activate optimized;
draft action Discard;
draft action Edit;
draft action Resume;
draft determine action Prepare;

mapping for znp_dt_sales
{
Salesorderid = salesorderid;
Itemid = itemid;
Materialid = materialid;
Quantity = quantity;
Currencycode = currencycode;
Totalamount = totalamount;
Status = status;
CreatedBy = created_by;
CreatedAt = created_at;
LastChangedBy = last_changed_by;
LastChangedAt = last_changed_at;
}
}  In the class save_modified method raise the event if stock is not available,it willgive message to user materials is out of stock.  IF create-zrnpdtsales IS NOT INITIAL .

DATA ls_tab TYPE znp_dt_sales.
DATA(ls_c) = VALUE #( lt_c[ 1 ] OPTIONAL ) .
*

READ ENTITIES OF zr_np_dt_sales IN LOCAL MODE
ENTITY ZrNpDtSales
ALL FIELDS WITH VALUE #( ( %key = ls_c-%key ) )
RESULT DATA(lt_sales).

DATA(ls_sales) = lt_sales[ 1 ].

SELECT SINGLE FROM znp_dt_stock FIELDS stock
WHERE materialid = _sales-Materialid
INTO (lv_stock).

IF ls_sales-Quantity > lv_stock.

RAISE ENTITY EVENT zr_np_dt_sales~C_order
FROM VALUE #( ( %key = ls_u-%key
%param = VALUE #( Message =
‘Sales order is Pending Due to low stock’ ) ) ).
endif.  In  the event handler It will give the message. CLASS lcl_local_events DEFINITION INHERITING FROM
cl_abap_behavior_event_handler .
PRIVATE SECTION.
METHODS confirm_order
FOR ENTITY EVENT it_parameters FOR ZrNpDtSales~C_order.

Endclass.

Method confirm_order.
data(ls_param) = value #( it_paramter[ 1 ] optional ).
APPEND VALUE #( %tky = ls_sales-%tky
%msg = me->new_message_with_text(
severity = if_abap_behv_message=>severity-error
text = ls_param-message
) ) TO reported-zrnpdtsales. while creating order if the if the quantity is more than the stocks it will give messagethat stocks is not available, status is set to pending.             Read More Technology Blogs by Members articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author