You are on page 1of 4

# Content

1 Local class with event and event handler method in SAP ABAP

Local class with event and event


handler method in SAP ABAP
Last Updated: November 11th 2013 by Ashok Kumar Reddy

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.

EVENTS : NO_MATERIAL. "event

Define a method and makt it as Event handler method

METHODS : EVENT_HANDLER FOR EVENT NO_MATERIAL OF CL_EVENTS. "event handler method

Create one more method for this example.


METHODS : GET_MATERIAL_DETAILS
IMPORTING IM_MATNR TYPE MARA-MATNR
EXPORTING EX_MARA TYPE MARA.

Create a triggering point


Create a triggering poing with keyword RAISE.
CLASS CL_EVENTS 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. "triggering point
ENDIF.
ENDMETHOD.
METHOD EVENT_HANDLER.
WRITE :/ 'No material found'.
ENDMETHOD.
ENDCLASS. "CL_EVENTS

Register event using SET HANDLER and use it in our program


Define class, create object and register the event with th eobject using SET HANDLER keyword.
DATA LO_EVENT TYPE REF TO CL_EVENTS.
DATA : WA_MARA TYPE MARA.
CREATE OBJECT LO_EVENT.
SET HANDLER LO_EVENT->EVENT_HANDLER FOR LO_EVENT.
CALL METHOD LO_EVENT->GET_MATERIAL_DETAILS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.

Final Program code will be

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

No Questions by learners, be first one to ask ..!!

You might also like