Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Event Handling for messages

Former Member
0 Kudos

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.

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

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

2 REPLIES 2

naimesh_patel
Active Contributor
0 Kudos

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

uwe_schieferstein
Active Contributor
0 Kudos

Hello Anee

Events are not used for this kind of message handling.

If you want to collect all messages then use a message handler (see sample report ZUS_SDN_ABAP_OO_MSG_HANDLING ).

Alternatively, you may define an exception class and raise a class-based exception which contains the detailed error message.

The following sample report is based on the more elaborate Wiki posting

[Message Handling - Finding the Needle in the Haystack|https://wiki.sdn.sap.com/wiki/display/profile/2007/07/09/MessageHandling-FindingtheNeedleintheHaystack]


*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_ABAP_OO_MSG_HANDLING
*&
*&---------------------------------------------------------------------*
*& Thread: Event Handling for messages
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1052131"></a>
*&---------------------------------------------------------------------*

REPORT  zus_sdn_abap_oo_msg_handling.

TYPE-POOLS: abap.

*----------------------------------------------------------------------*
*       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: constructor.
    METHODS: get_data
                   IMPORTING
                 value(id_carrid) TYPE s_carr_id.
    METHODS: has_messages
               RETURNING value(rd_result)  TYPE abap_bool.
    METHODS: display_messages.
    EVENTS: data_not_found.

  PROTECTED SECTION.
    DATA: mo_msglist    TYPE REF TO if_reca_message_list.
ENDCLASS.                    "lcl_mytestclass DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_mytestclass IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_mytestclass IMPLEMENTATION.
  METHOD constructor.
    me->mo_msglist = cf_reca_message_list=>create( ).
  ENDMETHOD.                    "constructor

  METHOD has_messages.

    IF ( me->mo_msglist->is_empty( ) = abap_false ).
      rd_result = abap_true.
    ENDIF.
  ENDMETHOD.                    "has_messages
  METHOD display_messages.
* define local data
    DATA:
      ld_handle           TYPE balloghndl,
      lt_log_handles      TYPE bal_t_logh,
      ls_profile          TYPE bal_s_prof.


    " Get log handle of collected message list
    ld_handle = me->mo_msglist->get_handle( ).
    APPEND ld_handle TO lt_log_handles.


*   get a display profile which describes how to display messages
    CALL FUNCTION 'BAL_DSP_PROFILE_DETLEVEL_GET'
      IMPORTING
        e_s_display_profile = ls_profile.  " tree & ALV List


*   set report to allow saving of variants
    ls_profile-disvariant-report = sy-repid.
*     when you use also other ALV lists in your report,
*     please specify a handle to distinguish between the display
*     variants of these different lists, e.g:
    ls_profile-disvariant-handle = 'LOG'.


    CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'
      EXPORTING
        i_s_display_profile          = ls_profile
        i_t_log_handle               = lt_log_handles
*       I_T_MSG_HANDLE               =
*       I_S_LOG_FILTER               =
*       I_S_MSG_FILTER               =
*       I_T_LOG_CONTEXT_FILTER       =
*       I_T_MSG_CONTEXT_FILTER       =
*       I_AMODAL                     = ' '
*     IMPORTING
*       E_S_EXIT_COMMAND             =
      EXCEPTIONS
        profile_inconsistent         = 1
        internal_error               = 2
        no_data_available            = 3
        no_authority                 = 4
        OTHERS                       = 5.
    IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*           WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.


  ENDMETHOD.                    "display_messages

  METHOD: get_data.
    SELECT * FROM spfli INTO TABLE gt_spfli
      WHERE ( carrid = id_carrid ).
    IF ( sy-subrc NE 0 ).
**      RAISE EVENT data_not_found.
      IF 1 = 2. MESSAGE e154(bc_datamodel_service). ENDIF.
*   Flight not found (table SFLIGHT)

      CALL METHOD me->mo_msglist->add
        EXPORTING
*          is_message   =
          id_msgty     = 'E'
          id_msgid     = 'BC_DATAMODEL_SERVICE'
          id_msgno     = '154'
*          id_msgv1     =
*          id_msgv2     =
*          id_msgv3     =
*          id_msgv4     =
          id_detlevel  = '1'
*        IMPORTING
*          es_message   =
          .
    ENDIF.

    IF ( gt_spfli IS INITIAL ).
    ELSE.
      SELECT * FROM scarr INTO TABLE gt_scarr
      FOR ALL ENTRIES IN gt_spfli
        WHERE  carrid = gt_spfli-carrid.
    ENDIF.

    IF ( gt_scarr is initial ).
      " How to call event for flight_name_not_found ?"
**        RAISE EVENT flight_name_not_found.
      IF 1 = 2. MESSAGE e159(bc_datamodel_service) WITH '&all'. ENDIF.
*   Airline & not found
      CALL METHOD me->mo_msglist->add
  EXPORTING
*          is_message   =
    id_msgty     = 'E'
    id_msgid     = 'BC_DATAMODEL_SERVICE'
    id_msgno     = '159'
    id_msgv1     = '&all'
*          id_msgv2     =
*          id_msgv3     =
*          id_msgv4     =
    id_detlevel  = '2'
*        IMPORTING
*          es_message   =
    .
    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.

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.

PARAMETER:
  p_carrid    TYPE s_carr_id DEFAULT 'AA'.

START-OF-SELECTION.


START-OF-SELECTION.
  CREATE OBJECT: oref, h1.
**  SET HANDLER h1->handle_event FOR ALL INSTANCES.
  CALL METHOD oref->get_data( p_carrid ).

  IF ( oref->has_messages( ) = abap_true ).
    oref->display_messages( ).
  ENDIF.

END-OF-SELECTION.

Regards

Uwe