cancel
Showing results for 
Search instead for 
Did you mean: 

OData V2 SubmitChanges error call back not working

MioYasutake
Active Contributor

Hello,

I have implemented OData batch processing and have the following problem.

Even though a business exception is raised in the back end, error call back never gets triggered.

(I found it triggerred only when there's a connection error)

My back-end code for raising exception

if sy-subrc <> 0.
  io_message_container->add_message_from_bapi(
    exporting
      it_bapi_messages = lt_return
      iv_add_to_response_header = abap_true
  )
  raise exception type /iwbep/cx_mgw_busi_excetion
    exporting
      message_container = io_message_container.
endif.

Front-end code for calling submitChanges

oModel.submitChanges({
	success: function(oRes) {
		//some code;
	},
	error : function (oErr){
		//some code;
	},
	groupId: sBatchGroup
});

When a exception is raised, I can see error message in console, but error call back is not triggered.

error.png

I've read below threads regarding this issue, but haven't got the solution yet.

https://answers.sap.com/questions/12184545/submitchanges-error-callback-not-working-in-versio.html

https://answers.sap.com/questions/12269639/odata-submitchanges-error-handling.html#

What I need to do is to show a message popover with messages returned from server.

It seems that messages are not coming to message popover unless OData request failes...

Regards,

Mio

maheshpalavalli
Active Contributor
0 Kudos

Just a small quesiton.. How are you getting the messages to the popover? u are planning to read them manually in the error handle?

MioYasutake
Active Contributor
0 Kudos

Thanks Mahesh, I realized this comment function.

My assumption is that if submitChanges() returns error, then message manager can automatically pick the message.

Accepted Solutions (1)

Accepted Solutions (1)

MioYasutake
Active Contributor

The Issue has been resolved by passisng "iv_message_target" to "add_message" method.

( "add_messages_from_bapi" didn't have this parameter).

After that MessageManager started to recieve error messages from back-end.

<back-end code>

lv_target = |/Header('{<objectKey>}')|.
io_message_container->add_message(
  Exporting
    iv_msg_type  = 'E'
    iv_msg_id    = 'ZMSGCLASS'
    iv_msg_number = '000'
    iv_is_leading_message = abap_ture
    iv_add_to_response_header = abap_ture
    iv_message_target = lv_target
) .
 raise exception type /iwbep/cx_mgw_busi_excetion
   exporting
     message_container = io_message_container.

<front-end code>

As submitChanges callback falls success even there's error from backend, I needed addithonal check for error.

oModel.submitChanges({
	success: function(oRes) {
		//Additional check for backend error
		if( !that._responseHasError(oRes) ){
			//Execute Success function									
		}
	},
	error : function (oErr){
		//Execute Error function
	},
	groupId: sBatchGroup
});			

_responseHasError: function(oRes) {
	var bStatusExists = "response" in oRes.__batchResponses[0];
	if (!bStatusExists){
		return false;
	}
	else if ( oRes.__batchResponses[0].response.statusCode >= 400 ){
		return true;
	}
	return false;
},

Answers (2)

Answers (2)

MioYasutake
Active Contributor
0 Kudos

Yes. This app was first implemented with create method and error handling was working as expected.

maheshpalavalli
Active Contributor
0 Kudos

Ohh, I also used or normal operations and not with submit changes. I still believe it should work automatically.. Let me also check when i get time..

Btw, you can click on comment to provide more information or interact with answers. Should post only answers if it is a solution.

DaniEmmenegger
Explorer
0 Kudos

Hi Mio

In my case none of the handlers (no success, no error) are called... what is wrong??

			//Daten ans Backend senden
			//oOdataModel.setDeferredGroups(["CreateInvDoc"]);
			for (i = 0; i < aJsonModelData.length; i++) {
				oOdataModel.create("/CountSet", {
					"Mat": aJsonModelData[i].Mat,
					"Plant": aJsonModelData[i].Plant,
					"StorLoc": aJsonModelData[i].StorLoc,
					"Bat": aJsonModelData[i].Bat,
					"CountQty": aJsonModelData[i].CountedQty,
					"Unit": aJsonModelData[i].Unit
				});
			}

oOdataModel.submitChanges({
 //	groupId: "CreateInvDoc",
 success: this.onUpdateSuccess,
 error: this.onUpdateError	});
	onUpdateError: function (oError){
			MessageToast.show("Fehler");
				
			console.log(oError.toString());
		},

The Inserts go to the backend as batch... everything is nice... but I need the handlers... imho they should also be called without message manager. any ideas?

Regards

Daniel

MioYasutake
Active Contributor
0 Kudos

Hi Daniel,

You can check if batch call was success with below code.

If the call was success, ODataModel should not have pending changes anymore.

Error callback of submitChanges is only called when call to the backend itself failed (such as connection error).

oOdataModel.submitChanges({
 success: function () {
	 if (oOdataModel.hasPendingChanges()) {
		 //error callback
	 }
	 else {
		 //success callback
	 }
 },
 error : //called on connection error 
});

Regards,

Mio

MioYasutake
Active Contributor
0 Kudos

Hi Mahesh,

Messages are supposed to be displayed automatically, once you add below code to controller's init method, and add MessagePopover control to the view.

MessaegManager takes care of fetching and displaying messages.

var oMsgManager = sap.ui.getCore().getMessageManager();
this.getView().setModel(oMsgManager.getMessageModel(), "message");
oMsgManager.registerObject(this.getView(), true);
maheshpalavalli
Active Contributor
0 Kudos

Oh, I was in the assumption that messagemanger takes care of odata messages from thrown from submitchangs as well.

But if you tried with normal create call and all, are they getting recorded by message manger?