Example of using Events in SAP OOABAP classes

Using of event handler methods in SAP classes, implementing event handler method in OOABAP

+ -
This is the basic example of using events in SAP classes.

Learners directly coming for this lesson, please refer previous lesson Events in SAP classes and Working with class methods in SAP as we are adding one more method for the same class.

Go to SE24, provide class name as ZCL_SAPN_MATERIALS( we previously created this class see previous lesson) and click change.

Event handler methods in SAP Classes OOABAP

Steps for creating and using event in SAP classes

Define an event

Go to Events tab and add a event as below.

NO_MATERIAL-INSTANCE-PUBLIC-No material entered

Event handler methods in SAP Classes OOABAP

Define a method

Go to Methods tab and create a method as below

NO_MATERIAL_HANDLER-INSTANCE-PUBLIC-Event Handler Method.

Event handler methods in SAP Classes OOABAP

Save, double click on the method and add some code.
    WRITE:/ 'NO material entered'.

Event handler methods in SAP Classes OOABAP

Save and activate immediately.

Link event and method and convert the method into event-handler method.

Now we have to link the method to event to make the method as event handler.

Go to methods and put cursor on method NO_MATERIAL_HANDLER, click on detail view icon(see below image).

Event handler methods in SAP Classes OOABAP

A pop up will open, provide description, select Event Handler for check box, provide our class name as ZCL_SAPN_MATERISLS and event name as NO_MATERIAL (Press F4), enter.

You will see an icon(event handler icon), which means the method is event handler method.

Event handler methods in SAP Classes OOABAP

Create a triggering method which will raise the event

Event handler method is created, now we have to trigger that event, the vent can be triggered by using below syntax.
RAISE EVENT <EVENT NAME>.
We will trigger the event for the method GET_MATERIAL_DTAILS (we previously created Get material details ), double click on the method GET_MATERIAL_DTAILS and add the below code.
  METHOD GET_MATERIAL_DTAILS.
*Select material data from mara table into exporting parameter ex_mara (work area) for a material no im_matnr
    IF IM_MATNR IS INITIAL .
      RAISE EVENT NO_MATERIAL.
    ELSE.
      SELECT SINGLE * FROM MARA
        INTO EX_MARA
        WHERE MATNR = IM_MATNR.

    ENDIF.
  ENDMETHOD.

Event handler methods in SAP Classes OOABAP

Use set handler and register event handler method to a particular instance in the program

Now we have to register this event in our SE38 program, to register a event handler method we use below syntax.
SET HANDLER <INSTANCE>-><EVENT HANDLER METHOD> FOR <INSTANCE>. "here INSTANCE is object and EVENT HANDLER METHOD handler method created in se24
Go to SE38, create a program ZSAPN_CLASS_EVENT and write the below code
REPORT ZSAPN_CLASS_EVENT.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "class decleration 
DATA WA_MARA TYPE MARA. "work area to store material data

PARAMETERS P_MATNR TYPE MARA-MATNR. "material input

CREATE OBJECT LO_MATERIAL. "create object for material calsss
SET HANDLER LO_MATERIAL->NO_MATERIAL_HANDLER FOR LO_MATERIAL. "register event handler method

START-OF-SELECTION.
  CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS "call method 
    EXPORTING
      IM_MATNR = P_MATNR
    IMPORTING
      EX_MARA  = WA_MARA.

  WRITE : WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.

Now execute the program with out giving any input, the result will be

Event handler methods in SAP Classes OOABAP

Assignment for you: Create and implement a event and event handler method for material not found in the above class, if user provided the input but the data is not there in MARA table, raise an event.

Learner Questions


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

    Please Sign in to ask a question

    Was this lesson helpful to you? Yes No 77 People out of 77 think this lesson helpful


    Lesson Navigation

    ←Previous Chapter
    Next Lesson → Constructors in SAP OOABAP