cancel
Showing results for 
Search instead for 
Did you mean: 

Get Success / Error message from OData backend on update / Create operation

Karan_Chopra_
Active Participant
0 Kudos

Hello Experts

I am trying to get get message returned from backend system on successful update / create request, but the code returns nothings and when I check in debug mode it bypasses the success / error functions in oDataModel's create/update methods.

Below is my code:


var oServiceModel = this.getView().getModel();

var relPath = "/IssueDetailSet(" + "Memberid=" + "'" + memberid + "'" + "," + "Bookid=" + "'" + bookid + "'" + ")";

  // Call the update method of the service model

oServiceModel.update(relPath, oData, null, function(success) {

  MessageToast.show("Updated book " + success.Bookid);

   },

  function(error) {

  MessageToast.show(JSON.parse(error.response.body).error.message.value);

  });

at Backend Side I am passing data in UpdateEntityset to below Method

        COPY_DATA_TO_REF(

                exporting

                  IS_DATA = LS_REQUEST_INPUT_DATA

                changing

                  CR_DATA = ER_ENTITY

              ).

Date is going fine and update working fine but I am not sure how to get return Sucess message in JS above

Accepted Solutions (1)

Accepted Solutions (1)

Karan_Chopra_
Active Participant
0 Kudos

yes Mayank

you are Right there was problem in the syntax itself , I have already used the above syntax and now it works fine !!

But there is a small problem now that response does not return anything on success


but if I raise some exception as below , response returns an error values accordingly.


RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception

*      EXPORTING

*          message_container = LO_MESSAGE_CONTAINER.

How can I add some value in response in case of success.

Former Member
0 Kudos

Replace you success callback function with this. Response is sent in the second parameter.

success: function(oData, oResponse)

Regards,

Answers (6)

Answers (6)

vardhan_naik3
Explorer
0 Kudos
error: function (oError) {     
MessageBox.show(
JSON.parse(oError.response.body)["error"]["message"]["value"], {
							title: "Error"
						}
); --> outer 
Karan_Chopra_
Active Participant
0 Kudos

Thanks Mayank !

Problem solved

Thanks Ankit for all the Help!! Appreciated

former_member185414
Active Contributor
0 Kudos

Hi KC,

Happy to hear that your problem was solved but as I see the correct syntax was explained by me in my Second reply itself. I wish you had checked that thoroughly.

BR.

Karan_Chopra_
Active Participant
0 Kudos

Syntax I missed was adding Keywords "success:" and "error:" before the function call, but in your second Reply you explained about the Response part syntax which was anyways helpful.

Thanks for that

former_member185414
Active Contributor
0 Kudos

oh ok I missed that part. Thanks.

BR.

Karan_Chopra_
Active Participant
0 Kudos

Still  no response, nothing returned

even BApi is passing final data in  LT_BAPI_MESSAGE

former_member185414
Active Contributor
0 Kudos

Is it stopping at the breakpoint now ?

Karan_Chopra_
Active Participant
0 Kudos

No still not stopping

Former Member
0 Kudos

Hi,

Can you try like this and check if anything goes into console. Also what is status and response of the request in chrome debugger - network tab.

oModel.update(path, oData, {

success: function(oData, response)

     { console.log(oData, response); },

error: function(oData, response)
     { console.log(oData, response); }

});

Regards,

Karan_Chopra_
Active Participant
0 Kudos

I applied the Break point

But even on applying break point thru console it does not return at success / error function even after hitting back end and update happens without any response back

I applied BP at line 69 and line 75 but it did not return there even after sometime and processing was fully complete

former_member185414
Active Contributor
0 Kudos

The breakpoint which is shown in image is not correct but as you have rightly mentioned please put breakpoint at line number 69 and 75 i.e. put debugger; as the first statement of the success and error handler callback function. After that clear the cache and try running again.

BR

Karan_Chopra_
Active Participant
0 Kudos

Help required Experts !

former_member185414
Active Contributor
0 Kudos

Hello KC,

Multiple ways are there to pass message from backend to UI.

1. Either add two properties message type and message in the structure for 'er_entity'  and populate them. Then you can pass 'er_entity' as you are doing now and it will have the messages. This is not a good way though.

2. A better way is to instantiate a message container and add the messages there which will be available as header in the response of Odata call.

Sample -

Data : lo_message_container TYPE REF TO /iwbep/if_message_container.

*-- At least one message is there - Instantiate a Message Container and use the same

            CALL METHOD me->mo_context->get_message_container

              RECEIVING

                ro_message_container = lo_message_container.

*-- Pass messages returned from UI Request Handler  API to oData Message Container

            CALL METHOD lo_message_container->add_messages_from_bapi

              EXPORTING

                it_bapi_messages          = lt_bapi_message " Source of message

                iv_add_to_response_header = abap_true.

BR.

Karan_Chopra_
Active Participant
0 Kudos

Hello Ankit

Still the success function is getting bypassed as you can see below in highlighted text.

Also the value at back-end is getting passed to METHOD lo_message_container->add_messages_from_bapi

Highlighted text  LS_REQUEST_INPUT_DATA-BOOKID has  value but nothing is getting returned back in Success.Bookid in function above in JS.

former_member185414
Active Contributor
0 Kudos

Hi KC,

The call to success and error handler functions are done asynchronously (makes sense also) because backend processing takes some time. So put a debugger statement in both and check.

Backend code seems correct. To be able to get the message as part of HTTP header modify the success/error handler and include a second argument. Example-

function(oData,oResponse) {

var sCompleteMessage = oResponse.headers["sap-message"];

  var oMessage = JSON.parse(sCompleteMessage);

MessageToast.show(oMessage.message);

}

BR.

Karan_Chopra_
Active Participant
0 Kudos

Yes Ankit

got that point so I was expecting it will call the messageToast message after sometime but unfortunately not.

Anyways can you please provide exact implementation of  function you are trying to say, I am completely new to JS

below is the code

  1. oServiceModel.update(relPath, oData, null, function(success) { 
  2.   MessageToast.show("Updated book " + success.Bookid); 
  3.    }, 
  4.   function(error) { 
  5.   MessageToast.show(JSON.parse(error.response.body).error.message.value); 
  6.   }); 
former_member185414
Active Contributor
0 Kudos

Hi KC,

Please modify as below and put debugger to check.


oServiceModel.update(relPath, oData, null, function(oData,oResponse) {

var sCompleteMessage = oResponse.headers["sap-message"];

  var oMessage = JSON.parse(sCompleteMessage);

MessageToast.show(oMessage.message);

   },

  function(oResponse) {

var sCompleteMessage = oResponse.headers["sap-message"];

  var oMessage = JSON.parse(sCompleteMessage);

MessageToast.show(oMessage.message);

  });


BR.

Karan_Chopra_
Active Participant
0 Kudos

hi Ankit

Still no change , message toast is not getting triggered

former_member185414
Active Contributor
0 Kudos

Please put breakpoints as shown and check by pressing F12. Also check the backend system (gateway system if any service error is getting logged)

oServiceModel.update(relPath, oData, null, function(oData,oResponse) {

debugger;

var sCompleteMessage = oResponse.headers["sap-message"];

  var oMessage = JSON.parse(sCompleteMessage);

MessageToast.show(oMessage.message);

   },

  function(oResponse) {

debugger;

var sCompleteMessage = oResponse.headers["sap-message"];

  var oMessage = JSON.parse(sCompleteMessage);

MessageToast.show(oMessage.message);

  });

Karan_Chopra_
Active Participant
0 Kudos

Yes applied BP at same places but as you can see it does not go to the response function even after hitting Back-end, at first run code goes to line 79 after update call and does not return to response section afterwards.

There is no error coming from backend accept the message I am passing from there as above

former_member185414
Active Contributor
0 Kudos

Hi KC,

The breakpoint should be hit. The processing happens asynchronously and after the backend logic is executed the control has to reach either success handler function or the error handler function.

Please re check thoroughly.

PS: Apologies for delayed response.