03.events in Object Oriented ABAP
03.events in Object Oriented ABAP
Working with events and event handler methods in SAP classes in SAP ABAP
programming model
+ -
What are events in SAP Classes?
Event is a mechanism by which method of one class can raise method of another class, without the
hazard of instantiating that class.
Define an event .
Define a method.
Link event and method and convert the method into event-handler method.
Create a triggering method which will raise the event.
Use set handler and register event handler method to a particular instance in the program.
Learner Questions
+ -
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.
Step1:Define an event .
Step2:Define a method.
Step3:Link event and method and convert the method into event-handler method.
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
Go to Events tab and add a event as below.
Define a method
Go to Methods tab and create a method as below
NO_MATERIAL_HANDLER-INSTANCE-PUBLIC-Event Handler Method.
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).
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.
ENDIF.
ENDMETHOD.
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
START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS "call method
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
Now execute the program with out giving any input, the result will be
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 ..!!