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
- Step 1: Define an event .
- Step 2: Define a method.
- Step 3: Link event and method and convert the method into event-handler method.
- Step 4: Create a triggering method which will raise the event.
- Step 5: 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 methodGo 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 eventEvent 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 programNow 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 se24Go 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.