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.
Demo
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
http://jasper07.secondphase.com.au/bapiWarning/webapp/
Usage
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.
ABAP Code
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”
/MyEntity('PI')/Field1
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)
UI5 Code
In your UI5 app, all you have to do is ensure the following is in your manifest.json file
"handleValidation": true,
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.
https://github.com/jasper07/bapiWarning/blob/master/webapp/localService/MockRequests.js
Thanks for nice blog post.
Great blog John. We've just gone live with a Goods Receipt app which called the BAPI in test mode as each item was saved. This approach would be great for something like that. It's good to know that we can pass warnings back without triggering a 400 response.
Cheers Mike
Yeah that was the kind of scenario i was thinking of, various business rules around tolerance and price limits need checking before posting. If you go over or under, its not necessarily an error, but you do want to highlight a warning to the user, so they get a chance to make adjustments.
one that pops up a lot is service confirmation
"you have a $10 limit on expenses, $10.01 will need approval from your manager"
to a contractor that could mean the difference in getting paid today or in 3 months
JSP
Hi,
i got the problem that i get 2 messages in the model, but i only raise one 🙁
John linked my talk at the UI5Con earlier this year in March: Displaying server-side OData messages in ui5 (Ui5con 2017)
There I have also described why you might get double messages. IMHO this is a bug that should be fixed by SAP. For details check the link, it also includes a video...
Hi,
when i use the batch mode for the oData, it removes the "first ariving" message. Any idea?
Hi John,
many thanks for this nice blog!
It is working fine in update-entity scenario's for me. But when i create a new entity the error and warning messages are not being displayed .
Could the problem be, that i don't have an id for my EntitySet to fill the key-field in the method add_message_from_bapi ?
Regards Kai