0
votes

I perform an update via my OData service like this:

oModel.create('/Carriers', oEntry, null, function () {
    oModel.refresh();
    sap.m.MessageBox.show("Saved", sap.m.MesESS);
}, function (err) {
    var message = $(err.response.body).find('message').first().text();
    sap.m.MessageBox.show(message, sap.m.MessageBox.Icon.ERROR);
});

If I get an error message in the response I am unable to display the message text.

I create the error like this:

CALL METHOD lo_message_container->add_message_text_only
 EXPORTING
  iv_msg_type = 'E'
  iv_msg_text = msg_text.
 RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
 EXPORTING
  message_container = lo_message_container.
 ENDIF.

The err.response.body looks like this:

"{"error":{"code":"SY/530","message":{"lang":"en","value":"This is the error I am trying to show)"}, "innererror":{"transactionid":"B20B61E5143BF10E92CB000C29D28D3A","timestamp":"20150922092421.4230000","Error_Resolution":{"SAP_Transaction":"Run transaction /IWFND/ERROR_LOG on SAP NW Gateway hub system and search for entries with the timestamp above for more details","SAP_Note":"See SAP Note 1797736 for error analysis (https://service.sap.com/sap/support/notes/1797736)"},"errordetails":[{"code":"","message":"This is the error I am trying to show ","propertyref":"","severity":"error","target":""},{"code":"/IWBEP/CX_MGW_BUSI_EXCEPTION","message":"","propertyref":"","severity":"error","target":""}]}}}"

I was trying this but it does not work...

var message = $(err.response.body).find('message').first().text();
sap.m.MessageBox.show(message, sap.m.MessageBox.Icon.ERROR);
1
did you try inspecting the err-element? via console.log(err); there might be the message somewhere, I only have an example how to retrieve the message from a Batch, this would be: oModel.submitBatch(function(data) { console.log(data); console.log(data.__batchResponses); console.log(data.__batchResponses[0].__changeResponses[0]); console.log(data.__batchResponses[0].__changeResponses[0].data.Response); }dotchuZ

1 Answers

1
votes

It looks like your error is mistaking JSON for a DOM object.

jQuery (the $-sign function) is meant to select DOM elements with the tag name, classes or the id.

To find the right key in an JSON structure, you can use normal JavaScript dot- or brackets-notation:

var message = err.response.body.error.message.value;