Technical Articles
Code Snippet Series: Eliminate Duplicate Messages from Mandatory Field Check
This post is part of a series on code snippets. The complete list of posts in the series is available in the document Code Snippets: A Blog Series.
Calling method CL_WD_DYNAMIC_TOOL=>CHECK_MANDATORY_ATTR_ON_VIEW during WDDOBEFOREACTION is a convenient way to check that a view’s required entry fields are filled prior to executing an action’s logic. Unfortunately, the method returns an identical message for each attribute not filled, resulting in duplicate messages.
With the advent of new ABAP language feature GROUP BY for internal tables introduced with ECC 740 SP08, developers have a new option to eliminate these duplicate messages while still highlighting multiple entry fields in error.
METHOD wddobeforeaction .
DATA lt_attributes TYPE string_table.
* -- Check mandatory input fields
cl_wd_dynamic_tool=>check_mandatory_attr_on_view(
EXPORTING
view_controller = wd_this->wd_get_api( )
display_messages = abap_false
IMPORTING
messages = DATA(lt_messages)
).
DATA(lo_controller) = CAST if_wd_controller( wd_this->wd_get_api( ) ).
DATA(lo_msg_mgr) = lo_controller->get_message_manager( ).
* Eliminate duplication of message, "Fill all required entry fields".
LOOP AT lt_messages ASSIGNING FIELD-SYMBOL(<msg>)
GROUP BY ( key1 = <msg>-t100_message-msgid
key2 = <msg>-t100_message-msgno ).
LOOP AT GROUP <msg> ASSIGNING FIELD-SYMBOL(<member>).
APPEND <member>-attribute_name TO lt_attributes.
ENDLOOP. " <member>
lo_msg_mgr->report_element_t100_message(
msg = <msg>-t100_message
element = <msg>-context_element
attributes = lt_attributes
).
ENDLOOP. " <msg>
ENDMETHOD.
Hello Amy,
Thank you for your useful topic.
I just have 2 remarks :
1 - I think you should add a clear to your variable 'lt_attributes' (in case there are 2 or more types of messages)
2 - May be you should add a third key to group by the 'context_element'. I think there will be an issue if your view contain multiple fields which are binded to differents nodes
Best regards,
Hicham K.
The code can definitely be changed to accommodate different scenarios! In the scenario I illustrate, I want to display just a single message, "Fill all required entry fields" so in this instance I want to ignore message type in order not to get a *warning* "Fill all required entry fields" and also an *error* "Fill all required entry fields"-- although that particular message is always an error.
Likewise for context element-- if I have attributes from two different elements with this condition, I still want to issue the message to the user just once. Again though, these criteria are specific to the scenario I'm illustrating and the code can be adjusted for different requirements.
Cheers,
Amy