Skip to Content
Author's profile photo Ramakrishnappa Gangappa

How to Report only one error message for many mandatory fields in Web Dynpro ABAP

We all know that, the mandatory checks on a view can be done by using method CHECK_MANDATORY_ATTR_ON_VIEW( ) of class CL_WD_DYNAMIC_TOOL.

Yes, it works great, but it displays an error message “Fill all required entry fields” for every attribute. i.e. if we have 100 attributes, we get 100 error messages displayed

Initial output:

err.JPG

Error messages from mandatory checks… it shows a message per attribute

err1.JPG

A huge scroll bar appears for all messages and its very tedious job to navigate to each and every message. Also, it occupies lot of place on the top of view.

Okay, then what to do ?

          if we want to display only one message for all required/mandatory fields of the given context element.

The solution:

  • Do not display messages from method CHECK_MANDATORY_ATTR_ON_VIEW i.e. pass parameter DISPLAY_MESSAGES = ABAP_FALSE
  • Collect messages into an internal table lt_messages
  • Now, lt_messages has the information about the context element and it attributes which are failed during mandatory check validation
  • Collect all attributes of each context element separately into table lt_attributes
  • Use method REPORT_ELEMENT_ERROR_MESSAGE( ) of class IF_WD_MESSAGE_MANAGER to report single error message for all mandatory fields in the given context element

The solution looks cool  is n’t it?

Write the below code in WDDOBEFOREACTION( ) method of view.

Note: here the messages are shown per context element i.e. if we have a view with many context node’s attributes, we get one message per context element.


WDDOBEFOREACTION( )

METHOD wddobeforeaction .

  DATA lo_api_controller            TYPE REF TO if_wd_view_controller.

  DATA lo_action                       TYPE REF TO if_wd_action.

  DATA lo_message_manager     TYPE REF TO if_wd_message_manager.

  DATA lt_attributes                   TYPE string_table.

  DATA lt_messages            TYPE cl_wd_dynamic_tool=>t_check_result_message_tab.

  DATA ls_messages            LIKE LINE OF lt_messages.

  lo_api_controller = wd_this->wd_get_api( ).

  lo_action = lo_api_controller->get_current_action( ).

  CALL METHOD lo_api_controller->get_message_manager

    RECEIVING

      message_manager = lo_message_manager.

  IF lo_action IS BOUND.

    CASE lo_action->name.

      WHEN ‘CHECK_DATA’.

        cl_wd_dynamic_tool=>check_mandatory_attr_on_view(

          EXPORTING

        view_controller = lo_api_controller

        display_messages = abap_false

          IMPORTING

        messages = lt_messages ).

        CLEAR lt_attributes.

        “sort by context element,

        SORT lt_messages BY context_element.

        LOOP AT lt_messages INTO ls_messages.

          APPEND ls_messages-attribute_name TO lt_attributes.

          AT END OF context_element.

            ” if no attributes found for errors, skip error message

            IF lt_attributes[] IS INITIAL.

              CONTINUE.

            ENDIF.

            ”         report message

            CALL METHOD lo_message_manager->report_element_error_message

              EXPORTING

                message_text = ‘Please fill all required fields’

                element      = ls_messages-context_element

                attributes   = lt_attributes.

     

           “Clear attributes list, for next context element

            CLEAR lt_attributes.

          ENDAT.

        ENDLOOP.

    ENDCASE.

  ENDIF.

ENDMETHOD.

Output – 1:

Displaying Only one error message per context element

err2.JPG

Output – 2:

User fills all the required fields in section 1 and presses the check_data button,

err3.JPG

————————————-

Hope this helps for those have requirement for displaying single message per section ( context element )

Also, check the below document for reporting single message for all attributes of a context node  in WDA

How to Report one attribute error message for multiple fields in Web Dynpro ABAP

————————————

Your valuable feedback / suggestions / comments are always welcome 

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Rama,

      Thanks for posting useful document..Keep posting.

      Thanks

      KH

      Author's profile photo Ramakrishnappa Gangappa
      Ramakrishnappa Gangappa
      Blog Post Author

      Thanks KH 🙂

      Author's profile photo Mohamed Hassan
      Mohamed Hassan

      After Greetings,

      thanks for this posting it helped me ,,

      but in my case I need it for the table element with Layout Data ["GridData"] to also return one message for all lines in the table.

      If there is a way to do it, I'd be grateful.

      thanks again for this post,

      please fill all required fields.png