Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Event Handling for messages

I would like to know how to handle events for messages i maintained in zmesg01.

messages are follow: when gv_spfli is initial

"No such flight available.

when gv_scarr is initial.

"Flight name not available with help of raise event ?

pls suggest.

*----------------------------------------------------------------------*
*       CLASS lcl_mytestclass DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_mytestclass definition.
 public section.
   data: gt_spfli type table of spfli initial size 20,
           gt_scarr type table of scarr initial size 20.

   methods: get_data.
   events: data_not_found.
endclass.                    "lcl_mytestclass DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_mytestclass IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_mytestclass implementation.
 method: get_data.
   select * from spfli into table gt_spfli.
   if ( sy-subrc <> 0 ).
     raise event data_not_found.
     endif.
select * from scarr into gt_scarr 
for all entries in gt_flight where  carrname = gt_spfli-carrname.
if sy-subrc  <> 0.
" How to call event for flight_name_not_found ?"
raise event flight_name_not_found.
  endmethod.                    "get_data
  endclass.                    "lcl_mytestclass IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS handler DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class handler definition.
 public section.
   methods handle_event
           for event data_not_found of lcl_mytestclass.

endclass.                    "handler DEFINITION

*----------------------------------------------------------------------*
*       CLASS handler IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class handler implementation.
 method handle_event.
   write: / 'Data not found'.
 endmethod.                    "handle_excess
endclass.                    "handler IMPLEMENTATION


data: oref type ref to lcl_mytestclass,
     h1   type ref to handler.
start-of-selection.
create object: oref, h1.
set handler h1->handle_event for all instances.
call method oref->get_data.

Thanks in advance.

Anee.

Former Member
replied

You can use the exporting Parameters in the EVENT to let the event handler know, from where you raised the event.

Like:

*----------------------------------------------------------------------*
*       CLASS lcl_mytestclass DEFINITION
*----------------------------------------------------------------------*
CLASS LCL_MYTESTCLASS DEFINITION.
  PUBLIC SECTION.
    DATA: GT_SPFLI TYPE TABLE OF SPFLI INITIAL SIZE 20,
            GT_SCARR TYPE TABLE OF SCARR INITIAL SIZE 20.

    METHODS: GET_DATA.
    EVENTS: DATA_NOT_FOUND EXPORTING VALUE(ERROR_NO) TYPE I.  " <<
ENDCLASS.                    "lcl_mytestclass DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_mytestclass IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS LCL_MYTESTCLASS IMPLEMENTATION.
  METHOD: GET_DATA.
    SELECT * FROM SPFLI INTO TABLE GT_SPFLI.
    IF ( SY-SUBRC NE 0 ).
      RAISE EVENT DATA_NOT_FOUND EXPORTING ERROR_NO = 1.
    ENDIF.
   select * from scarr into table gt_scarr
   for all entries in gt_spfli where  carrname = gt_spfli-carrname.
    IF SY-SUBRC  NE 0.
" How to call event for flight_name_not_found ?"
*    raise event flight_name_not_found.
      RAISE EVENT DATA_NOT_FOUND EXPORTING ERROR_NO = 2.
    ENDIF.
  ENDMETHOD.                    "get_data
ENDCLASS.                    "lcl_mytestclass IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS handler DEFINITION
*----------------------------------------------------------------------*
CLASS HANDLER DEFINITION.
  PUBLIC SECTION.
    METHODS HANDLE_EVENT
            FOR EVENT DATA_NOT_FOUND OF LCL_MYTESTCLASS
            IMPORTING ERROR_NO.

ENDCLASS.                    "handler DEFINITION

*----------------------------------------------------------------------*
*       CLASS handler IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS HANDLER IMPLEMENTATION.
  METHOD HANDLE_EVENT.
    CASE ERROR_NO.
      WHEN 1.
        WRITE: / 'Flight Not found'.
      WHEN 2.
        WRITE: / 'Data not found'.
    ENDCASE.
  ENDMETHOD.                    "handle_excess
ENDCLASS.                    "handler IMPLEMENTATION


DATA: OREF TYPE REF TO LCL_MYTESTCLASS,
     H1   TYPE REF TO HANDLER.

START-OF-SELECTION.
  CREATE OBJECT: OREF, H1.
  SET HANDLER H1->HANDLE_EVENT FOR ALL INSTANCES.
  CALL METHOD OREF->GET_DATA.

Regards,

Naimesh Patel

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question