0% found this document useful (0 votes)
172 views

03.events in Object Oriented ABAP

1. Events allow methods in one class to trigger methods in another class without instantiating the other class. 2. To create an event, define the event and link it to a method to make that method an event handler method. 3. To trigger the event, call the RAISE EVENT syntax within a method. 3. To register an event handler method, use the SET HANDLER syntax in a program, linking the event handler method to an instance of the class.

Uploaded by

Shashank Yerra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views

03.events in Object Oriented ABAP

1. Events allow methods in one class to trigger methods in another class without instantiating the other class. 2. To create an event, define the event and link it to a method to make that method an event handler method. 3. To trigger the event, call the RAISE EVENT syntax within a method. 3. To register an event handler method, use the SET HANDLER syntax in a program, linking the event handler method to an instance of the class.

Uploaded by

Shashank Yerra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

# Content

1 What are Events in SAP classes ?

2 Example of using Events in SAP classes

What are Events in SAP classes ?


Last Updated: November 7th 2013 by Ashok Kumar Reddy

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.

Follow the below steps to add a event

 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.

Use below syntax to register a event handler method for a instance

SET HANDLER <INSTANCE> <EVT-HDLER-METHOD> for <INSTANCE>. "here instance is the


object created by using create object

In the next we will be learning it with working example.

Learner Questions

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


Example of using Events in SAP
classes
Last Updated: November 8th 2013 by Ashok Kumar Reddy

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.

Steps for creating and using event in SAP classes

 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.

NO_MATERIAL-INSTANCE-PUBLIC-No material entered

Define a method
Go to Methods tab and create a method as below
NO_MATERIAL_HANDLER-INSTANCE-PUBLIC-Event Handler Method.

Save, double click on the method and add some code.


WRITE:/ 'NO material entered'.
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).

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.

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.

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

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 ..!!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy