Events in OOPS ABAP
- Using Events in Object Oriented Programing, Method of one class can call method of another class.
- For events we will have to raise the event and then we will handle the event.
Steps For Event Handler :-
- Triggering Method :- A method which will raise the event OR a method which will be raising the event will be called as triggering method.
- Event Handler Method :- A method which will handle the event.
- Register the event handler method using SET Handler statement.
Syntax :-
- SET HANDLER lo_object1→ EVENT_HANDLER for lo_object2.
- where lo_object1 = Class Object in which event handler method is defined.
- EVENT_HANDLER = event handler method name.
- lo_object2 = Class Object in which triggering method is defined.
let’s create a program to understand it.
Requirement :-
- We will create a executable program where we will pass a input parameter for user.
- And if user do not pass any input, We will give a message on the Selection screen to pass the input to the parameter.
- And we will achieve this requirement using events.
Solution :-
-
Step 1 :- Go to ABAP Editor ( SE38 ) and create a executable program.
-
Step 2 :- Now we need to create few fields from VBAK as a local variable in which we will get the details on the basis of Sales document number provided by user as a input.
-
Step 3 :- Create a class with one method and one event.
-
Step 4 :- Now we need to implement the method and raise the event of the above class.
-
Step 5 :- Now we will create a class which will handle the event of the Class1 which we have created above.
-
Step 6 :- In Start of selection create the object of both classes.
-
Step 7 :- Handle the event.
-
Step 8 :- call method display and use the write statement to display the output.
Code :-
*&---------------------------------------------------------------------*
*& Report ZAR_OOPS_EVENTS
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zar_oops_events.
DATA : perdat TYPE erdat,
perzet TYPE erzet,
pernam TYPE ernam,
pvbtyp TYPE vbtypl.
PARAMETERS : pvbeln TYPE vbeln_va.
CLASS class1 DEFINITION.
PUBLIC SECTION.
METHODS display IMPORTING pvbeln TYPE vbeln_va
EXPORTING perdat TYPE erdat
perzet TYPE erzet
pernam TYPE ernam
pvbtyp TYPE vbtypl.
EVENTS : no_input.
ENDCLASS.
CLASS CLASS1 IMPLEMENTATION.
METHOD DISPLAY.
if pvbeln is INITIAL.
raise event no_input.
else.
Select single erdat erzet ernam vbtyp
from vbak into ( perdat, perzet, pernam, pvbtyp ).
endif.
ENDMETHOD.
ENDCLASS.
CLASS CLASS2 DEFINITION.
PUBLIC SECTION.
METHODS MESSAGE FOR EVENT no_input of class1.
ENDCLASS.
class class2 IMPLEMENTATION.
method message.
WRITE : 'Please enter Sales Document Number'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(lo_obj1) = new CLASS1( ).
DATA(lo_obj2) = new CLASS2( ).
SET HANDLER lo_obj2->message for lo_obj1.
lo_obj1->display( EXPORTING pvbeln = pvbeln
IMPORTING perdat = perdat
perzet = perzet
pernam = pernam
pvbtyp = pvbtyp ).
write :/ perdat, perzet, pernam, pvbtyp.
*&-------------------------------------------------------------------
*&End Of Program
*&------------------------------------------------------------------
Execute the program :-
-
Press F8 without giving any input.
-
Now go back and enter some value of Sales Document number.
-
Press F8.
Good
ReplyDelete