Professional Documents
Culture Documents
1 Local class with event and event handler method in SAP ABAP
Using events in SAP Local classes, handling events in local classes in SAP
ABAP programming
+ -
The below example explains of using events with local classes using SAP ABAP programming.
Define a Class
Define a local class CL_EVENTS and follow the below steps.
Step1:Define an event .
Step2:Define a method and meke it as event handler.
Step4:Create a triggering method which will raise the event.
Step5:Use set handler and register event handler method to a particular instance in the
program.
Define an event inside the local class definition under public section.
REPORT ZSAPN_LOCAL_CLASS_EVENTS.
CLASS CL_EVENTS DEFINITION DEFERRED.
DATA LO_EVENT TYPE REF TO CL_EVENTS. "declare class
DATA : WA_MARA TYPE MARA. "declare work area
PARAMETERS P_MATNR TYPE MARA-MATNR. "Material no input
CLASS CL_EVENTS DEFINITION. "class definition
PUBLIC SECTION.
EVENTS : NO_MATERIAL. "event
METHODS : GET_MATERIAL_DETAILS
IMPORTING IM_MATNR TYPE MARA-MATNR
EXPORTING EX_MARA TYPE MARA.
METHODS : EVENT_HANDLER FOR EVENT NO_MATERIAL OF CL_EVENTS. "event handler method
ENDCLASS.
START-OF-SELECTION.
CREATE OBJECT LO_EVENT. "create object
SET HANDLER LO_EVENT->EVENT_HANDLER FOR LO_EVENT. "register event handler method
for the object
CALL METHOD LO_EVENT->GET_MATERIAL_DETAILS "call method to get material details
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
CLASS CL_EVENTS IMPLEMENTATION. "class implementation
METHOD GET_MATERIAL_DETAILS.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.
IF SY-SUBRC NE 0.
RAISE EVENT NO_MATERIAL. "trigger the event
ENDIF.
ENDMETHOD.
METHOD EVENT_HANDLER.
WRITE :/ 'No material found'. "event handler method implementation
ENDMETHOD.
ENDCLASS. "CL_EVENTS
Now test the program with invalid material number (material that is not available in MARA ), it will
raise the event.
Learner Questions