In this post, you will learn about events in ABAP classes.

Event is an action or occurrence of something that is recognized by the application. Handling events means response by the application to the action or that occurrence.

As an example, when ALV is displayed and user clicks on something, event is triggered by the user’s action and the event handler simply responds to the user action i.e. handles the event of the user action.

Event Definition

Similar to methods, events can be instance or static. We will see the instance ones. For static CLASS-EVENTS key word is used. Events can have only exporting parameters.

EVENTS something_happened EXPORTING VALUE(about_event) TYPE string.

Event Handler 

Event handler can be in the same class of in a different class. These are nothing but methods. In the event handler, we specify the event name which we are going to handle and the class name where it is defined.

METHODS handle_something FOR EVENT something_happened OF zjp_abap 
                         IMPORTING about_event.

Trigger Event

The event can be triggered from a method in the same class. The details about the events or parameters that we need to pass to handler method as passed using EXPORTING.

RAISE EVENT something_happened EXPORTING about_event = 'Something'.

Note that the which event handler needs to be triggered for the event raised is determined at the runtime. Hence, we need to specify this information at runtime by registering the event handlers.

Register Event Handler

This is pretty much self explanatory.

SET HANDLER lo_abap_new->handle_something FOR lo_abap_new.

Now, you should be able to understand the events and event handlers and how the control will flow from the statement RAISE EVENT to the EVENT HANDLER method.

Going back to the ALV example, the events are already defined in the the ALV class CL_GUI_ALV_GRID and are also triggered from appropriate actions. All we need to do is define event handler methods in the class and register these event handlers.

For example

"Method definition 
METHODS double_click FOR EVENT double_click OF cl_gui_alv_grid 
                     IMPORTING e_row 
                               e_column 
                               es_row_no.

Handler is set as below. Here o_events is object of a class where the method is defined. o_grid is object of the class which is used to display the ALV.

SET HANDLER o_events->double_click FOR o_grid.

When a double click event occurs, the method double click is triggered automatically.

For more posts on Object Oriented ABAP, please visit the page OOABAP.


If you like the content, please subscribe…

Join 4,024 other subscribers

Discovering ABAP YouTube Channel