Thursday, 27 November 2014

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

Hello, Welcome....

We all know that, the mandatory checks on a view can be done by using methodCHECK_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:




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


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



Output - 2:


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




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

2 comments:

  1. Are you looking to enhance your skills in SAP Web DynPro? Look no further! ERP Training Delhi offers an exceptional SAP Web DynPro Course in Delhi that will take your career to new heights. In this comprehensive training program, you'll gain the knowledge and expertise necessary to excel in developing cutting-edge web applications using SAP's Web DynPro technology.

    ReplyDelete
  2. Are you looking to enhance your skills in SAP WebDynpro? Look no further! ERP TRAINING GURGAON offers comprehensive and industry-focused SAP WebDynpro Training in Gurgaon . With our expert trainers and cutting-edge curriculum, we provide you with the knowledge and practical experience to excel in the field of SAP WebDynpro development.

    ReplyDelete