ABAP Objects Events – Raising and Handling

By | September 17, 2013 | ABAP Objects, OO Concepts | 35,431 | 3

Events are good way to communicate between objects. They are very useful when the object doesn’t care about the handler of the objects.

What are Events

Events are the message raised by an object. Whenever any condition is met, object can raise an event. E.g. Timer has expired, Cut-off Limit has reached, User Interaction happened etc. This event than can be caught by the receiver. The receiver than implements the logic to handle the event. E.g. When Timer has expired, render the page again; When cut-off limit is reached, tell user to stop etc.

The objects designer doesn’t need to know who would handle the event or not. But it gives an opportunity for any receiver object to catch and process. Its kind of taking a roll call at few intervals in the discussion if anyone of the line has any question. If anyone has the question, they can ask at that time. The receiver object generally doesn’t care how the event was raised. For receiver, it only matters the fact that event was raised and its now trapped in its event handler.

Defining the Events

To define the event, you need to give a meaningful name. You can also declare parameters for the event which can be used to expose the data for the receiver.

Event Definition

 
CLASS lcl_main DEFINITION.
  PUBLIC SECTION.
    EVENTS: cutoff_reached.
ENDCLASS.                    "lcl_main DEFINITION
 

Raising an event can be done via RAISE EVENT

Raise an Event

 
RAISE EVENT cutoff_reached.
 

Handling the Events

First of all you would need to create a receiver object. So, when you declare the method in the receiver class, you define that the method is handler method using FOR EVENT.

Event Handler Definition

 
CLASS lcl_event_handler DEFINITION.
  PUBLIC SECTION.
    METHODS: handle_cutoff_reached
      FOR EVENT cutoff_reached OF lcl_main.
ENDCLASS.                    "lcl_event_handler DEFINITION
 

If some receiver wants to handle the event, it would need to let the system know about its intension. In ABAP, you can do it via SET HANDLER

Setting up the handler

 
  CREATE OBJECT lo_event_handler.
  SET HANDLER lo_event_handler->handle_cutoff_reached FOR lo_main.
 

Sample Example

Check out this Simple example to understand how the events are triggered.

UML

UML is like this for the application:

Code lines

Sample Event Handling

 
*
CLASS lcl_main DEFINITION.
  PUBLIC SECTION.
    DATA: v_num TYPE i.
    METHODS: process IMPORTING iv_num TYPE i.
    EVENTS: cutoff_reached.
ENDCLASS.                    "lcl_main DEFINITION
*
CLASS lcl_event_handler DEFINITION.
  PUBLIC SECTION.
    METHODS: handle_cutoff_reached
      FOR EVENT cutoff_reached OF lcl_main.
ENDCLASS.                    "lcl_event_handler DEFINITION
*
START-OF-SELECTION.
  DATA: lo_main TYPE REF TO lcl_main.
  DATA: lo_event_handler TYPE REF TO lcl_event_handler.
*
  CREATE OBJECT lo_main.
  CREATE OBJECT lo_event_handler.
  SET HANDLER lo_event_handler->handle_cutoff_reached FOR lo_main.
*
  lo_main->process( 5 ).
 
*
CLASS lcl_main IMPLEMENTATION.
  METHOD process.
    v_num = iv_num.
    IF iv_num GE 2.
      RAISE EVENT cutoff_reached.
    ENDIF.
  ENDMETHOD.                    "process
ENDCLASS.                    "lcl_main IMPLEMENTATION
*
CLASS lcl_event_handler IMPLEMENTATION.
  METHOD handle_cutoff_reached.
    WRITE: 'Event Processed'.
  ENDMETHOD.                    "handle_cutoff_reached
ENDCLASS.                    "lcl_event_handler IMPLEMENTATION
 

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

3 Comments

  • Kiran

    Dear Frnd,

    I do not have words to say about our blog.Its simple awesome.This knowledge really helps us.

    Thanks a lot for your contribution.

    Regards,
    Kiran

  • Fawcs

    Hello Naimesh,

    I’ve been trying to go a little further with the events and tried to implement standard ones on other objects, for example, a double click on the ALV on another class.

    I debugged a lot of the standard classes and methods to try and understand the gui framework, but I think I reached a wall here, I can’t implement events that the object doesn’t support!.

    Here’s the discussion I opened in scn: http://scn.sap.com/thread/3416154

    Could you give me any kind of input on this matter?

    Best regards,

    Fawcs

  • steve oldner

    This example is just for events raised in same program? What about between programs?

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.