Skip to Content

Purpose:

Demonstrate the scenario of retrieving messages from message manager and display in a menu

Scenario:

I have come across the scenario, where user doesn’t like the scrolling to view the messages and also it takes lot of space in the top of application.

Its very hard to scroll when we have to display return messages ( about 20-30 messages ) from BAPI/function/method. Hence displaying return messages in menu is a very good option.

In complex applications, the messages are thrown/caught into message manager in many places/methods.

I would like to demonstrate, the scenario of how to retrieve the messages from message manager and prepare menu and finally clear messages from message manager.

Pre-requisite:

Basic knowledge of Webdynpro ABAP & OO ABAP

Step by step process:

Here, I am going to create a WDA application with sample messages shown in message manager and preparing menu to display these messages

Step1:

Create a WDA component as below with a view

1.PNG

Step 2:

Go to the view V_MAIN layout editor and add a view ui lement MENUBAR as shown below

2.PNG

Step3:

Now, populate/report about 10 messages into message manager and write the below code in WDDOINIT( )


METHOD wddoinit .
  DATA lo_api_controller     TYPE REF TO if_wd_controller.
  DATA lo_message_manager    TYPE REF TO if_wd_message_manager.
  DATA lv_message            TYPE string.
  DATA lv_message_type       TYPE i.
  "================================================
" Get message manager reference
  "================================================
  lo_api_controller ?= wd_this->wd_get_api( ).
  " get message manager reference
  CALL METHOD lo_api_controller->get_message_manager
    RECEIVING
      message_manager = lo_message_manager.
  "================================================
  "Prepare messages & report to message manager
  "================================================
  DO 10 TIMES.
    CLEAR lv_message.
    lv_message = sy-index.
    CONCATENATE 'Message'  lv_message
      INTO lv_message SEPARATED BY space.
    IF sy-index MOD 2 EQ 0.
      lv_message_type = if_wd_message_manager=>co_type_error.
    ELSEIF sy-index MOD 3 EQ 0.
      lv_message_type = if_wd_message_manager=>co_type_warning.
    ELSE.
      lv_message_type = if_wd_message_manager=>co_type_standard.
    ENDIF.
    lo_message_manager->report_message(
      EXPORTING
        message_text              = lv_message
        message_type              = lv_message_type
    ).
  ENDDO.
  "================================================
  " Retrieve messages from menu and prepare menu
  "================================================
 wd_this->prepare_message_menu( ).
ENDMETHOD.




Step 4

Create a method which retrieves the messages from message manager and prepares menu with messages as below


METHOD prepare_message_menu .
  "=======================================
  " Data declarations
  "=======================================
  DATA: lo_menu_bar         TYPE REF TO cl_wd_menu_bar,
        lo_menu             TYPE REF TO cl_wd_menu,
        lo_menu_item        TYPE REF TO cl_wd_menu_item,
        lo_view             TYPE REF TO if_wd_view,
        lv_id               TYPE string,
        lv_menu_title       TYPE string,
        lv_menu_icon      TYPE string,
        lv_menu_item_icon   TYPE string,
        lt_messages         TYPE if_wd_message_manager=>ty_t_messages,
        ls_messages         LIKE LINE OF lt_messages.
  DATA lo_api_controller     TYPE REF TO if_wd_controller.
  DATA lo_message_manager    TYPE REF TO if_wd_message_manager.
  "=============================================
  "Get the view reference
  "=============================================
  lo_view ?= wd_this->wd_get_api( ).
  IF lo_view IS NOT BOUND.
    RETURN.
  ENDIF.
  "=============================================
*   get message manager
  "=============================================
  lo_api_controller ?= wd_this->wd_get_api( ).
  CALL METHOD lo_api_controller->get_message_manager
    RECEIVING
      message_manager = lo_message_manager.
  IF lo_message_manager IS NOT BOUND.
    RETURN.
  ENDIF.
  "=======================================
  " Get messages from message manager
  "=======================================
  CALL METHOD lo_message_manager->get_messages
    RECEIVING
      result = lt_messages.
 
  "=======================================
  " Get menu bar ui element reference
  "=======================================
  lo_menu_bar ?= lo_view->get_element( id = 'MENUBAR' ).
  "Return if lo_menu_bar not bound
  IF lo_menu_bar IS NOT BOUND.
    RETURN.
  ENDIF.
  "=======================================
  " Remove all existing menus
  "=======================================
  lo_menu_bar->remove_all_menus( ).
  lo_menu_bar->set_visible( value = cl_wd_menu_bar=>e_visible-visible ).
  "=======================================
  " Determine the no. of messages and prepare menu title
  "=======================================
  DESCRIBE TABLE lt_messages.
  lv_menu_title = sy-tfill.
  CONCATENATE 'Messages' '('
              lv_menu_title ' )'
  INTO lv_menu_title SEPARATED BY space.
  "=======================================
  "Create new menu
  "=======================================
  cl_wd_menu=>new_menu(
    EXPORTING
      id                  = 'MENU_1'
      title               = lv_menu_title
    RECEIVING
      control             = lo_menu
  ).
  "return if menu was not created successfully
  IF lo_menu IS NOT BOUND.
    RETURN.
  ENDIF.
  CLEAR lv_menu_icon.
  CLEAR lv_menu_item_icon.
  "=======================================
  "Loop over messages
  "=======================================
 
  LOOP AT lt_messages INTO ls_messages.
    MOVE sy-tabix TO lv_id.
    CONDENSE lv_id.
    CONCATENATE 'MENUITEM_' lv_id INTO lv_id.
    "check if message type is error, prepare icon source
    IF ls_messages-msg_type EQ if_wd_message_manager=>co_type_error.
      lv_menu_item_icon = '~Icon/ErrorMessage'.
    ELSEIF ls_messages-msg_type EQ
            if_wd_message_manager=>co_type_warning.
      lv_menu_item_icon = '~Icon/WarningMessage'.
    ELSE.
      lv_menu_item_icon = '~Icon/SuccessMessage'.
    ENDIF.
 
  "=======================================
  " Add menu item
  "=======================================
    cl_wd_menu_action_item=>new_menu_action_item(
      EXPORTING
        id                         = lv_id
        image_source               = lv_menu_item_icon
        text                       = ls_messages-err_message
      RECEIVING
        control                    = lo_menu_item
    ).
    "if menu item ref is not bound, skip adding it to menu
    IF lo_menu_item IS NOT BOUND.
      CONTINUE.
    ENDIF.
   
  "=======================================
    "Add menu item
  "=======================================
    lo_menu->add_item(
      EXPORTING
*        index    =
        the_item = lo_menu_item
    ).
  ENDLOOP.
  "=======================================
  "Add menu
  "=======================================
  lo_menu_bar->add_menu(
    EXPORTING
*      index    =
      the_menu = lo_menu
  ).
  "=======================================
  "Clear messages from message manager
  "=======================================
  lo_message_manager->clear_messages(
    EXPORTING
      including_permanent_msg = abap_true
  ).
ENDMETHOD.


Step 5:

Create an WDA application as below and test for the output.

3.PNG

Output 1: ( Before clearing messages from message manager )

4.PNG

/wp-content/uploads/2013/12/5_347868.png

Output 2: ( After clearing messages from message manager )

/wp-content/uploads/2013/12/6_347874.png

To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply