Show BAPI Warning messages in UI5 without throwing exceptions
I have been using the sap.ui.core.message.MessageManager for a while now. What I really like is the ease in which it handles messages raised on the Client side and the Server side in almost the same way. One of the nice features of the Message Manager I especially like is the ability to link a message to a target field and bring the attention of the message to the user.
In this blog I will demonstrate how to trigger and show back-end BAPI Warning, Error, Information and Success messages without throwing exceptions.
The Gif below shows an example of server side messaging, initially showing messages included in the http header and after that showing messages in the response body as part of business exception.
Try the app for yourself
There are numerous use cases, the one that is front and centre in my mind is a BAPI simulate scenario.
Your application allows the user to change multiple lines and post the changes in a batch request. The changes are processed through a BAPI and you wish to inform the user of warnings and information relating to the data changes before committing.
When processing BAPI messages in our Gateway service, we can either chose to add the messages to the header property “sap-message”, this will send a HTTP 204 response, telling the app the call was successful, else we can throw an exception, which it will send a HTTP 400 response and add the messages in the response body and the call of course will result in a failure.
LOOP AT lt_return ASSIGNING <fs_return>. " add message from bapi structure mo_context->get_message_container( )->add_message_from_bapi( EXPORTING is_bapi_message = <fs_return> iv_entity_type = iv_entity_set_name it_key_tab = VALUE /iwbep/t_mgw_name_value_pair( ( name = 'KEY1' value = er_entity-key1 ) ) iv_add_to_response_header = boolc( er_entity-throw_exception = abap_false ) iv_message_target = CONV string( <fs_return>-field ) ). ENDLOOP. IF er_entity-throw_exception = abap_true. RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception EXPORTING message_container = mo_context->get_message_container( ). ENDIF.
Code above, if we don’t need to throw an exception, set IV_ADD_TO_RESPONSE_HEADER to True
example messages in Header response
example messages in Body response
To get the message to show inline our Input fields, we can use IV_MESSAGE_TARGET, in this field we want to put the binding context path for the field.
We can get the path value easily using the UI5 Chrome extension “contextPath” + “propertyPath”
In our Gateway service we can derive the context path from the request URI like pic below
Note we can only map IV_MESSAGE_TARGET on Errors and Warnings, an assertion in SAPUI5 will fail if we try and map on Information and or Successes (kind of makes sense)
In your UI5 app, all you have to do is ensure the following is in your manifest.json file
I wrote some code for the creation of header and body messages for mockserver requests, handy if you want to test server side field validation.