Skip to Content
Author's profile photo Ankur Gokhale

How to implement Batch Processing in SAPUI5 and Gateway Builder for oData V2?

Hello everyone,

I was trying to implement batch processing in oData v2 and noticed very few documents available describing the process. Most of the articles on internet are copied from other sources. Batch processing stands for processing multiple records in one go.

I finished the implementation in SAPUI5 as well as in Gateway Service. I will try to explain this process in this post.

 

In this example, the version of sap-ui-core library is set to 1.54.6. Please find following code block that you have to set in Controller.

var tmpModel = new ODataModel("https://xxyyzz.com/sap/opu/odata/<your_service>_SRV/", true);
tmpModel.setDefaultBindingMode(sap.ui.model.BindingMode.TwoWay);
tmpModel.setUseBatch(true);
this.getView().setModel(tmpModel, "tmpModel");
tmpModel.setDeferredGroups(["foo"]);
var mParameters = {groupId:"foo",success:function(odata, resp){ console.log(resp); },error: function(odata, resp) { console.log(resp); }};

for(var m=0; m<oPayload.length; m++) {
	tmpModel.update("/YOUR_ENTITYSet(Key1='Valu1',Key2='Value2')", oPayload[m], mParameters);
}
tmpModel.submitChanges(mParameters);

First, we have to define an oData model with a service URL and set its default Binding mode to TwoWay. Since we are going to send payload to Gateway server in a batch, we set the parameter value as true of a function setUseBatch associated with oDataModel. The fundamental idea is to first prepare requests by looping over Items of your table or list, attach it to a batch using groupId and fire.

Next, we attach the defined model to application’s view and set the deferred group to ‘foo’.

To perform actual interactions with oData service, we have commonly used methods associated with every model such as read(), create(), update(), delete() etc. In this example, I am updating the values from a table hence using tmpModel.update().

In third parameter of method update(), we have to pass an object with a property as groupId. This will not fire any request immediately but keeps them in a stack. Its value must match with the deferred group that we have set earlier i.e. foo. To read more about parameters, please go through oData Model methods documentation.

One important point here to note is that there must be no spaces in the values set for a key. It means it could be ‘Value1’ or ‘%20%20Value1’ but not ‘  Value1’. A space here would result as a Bad request response in Gateway Builder.

We can see this result from t-code /IWFND/ERROR_LOG. Select a line from Overview table => Click Replay button => Select SAP Gateway Client.

From Backend server where oData service resides, its about redefining 2 methods from DPC_EXT class of service.

First open the class using SE24 t-code, then under left pane find following methods, right click on them and select redefine.

  • /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_BEGIN
  • /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CHANGESET_END

 

We do not have to write anything in them. For tmpModel.update(), set a breakpoint in UPDATE_ENTITY method of your class.

Number of times UPDATE ENTITY will execute = Number of iterations we have in for loop.

We can also have a callback execution attached to a batch request. This we have to pass in mParameters object. The SAPUI5 code block is self explanatory.

And here comes the end of the document.

 

Thanks

 

 

Assigned Tags

      6 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Burak Özçetin
      Burak Özçetin

      It is very helpful thanks.

      How do I remove an inserted record?

      for(var m=0; m<oPayload.length; m++) {
      	tmpModel.update("/YOUR_ENTITYSet(Key1='Valu1',Key2='Value2')", oPayload[m], mParameters);
      }
      Author's profile photo Ankur Gokhale
      Ankur Gokhale
      Blog Post Author

      Burak,

      Sorry for late reply, I am not so active on scn and blogs. I believe tmpModel.remove() should trigger DELETE_ENTITY method.

      Author's profile photo Burak Özçetin
      Burak Özçetin

      No.I didn’t mean it.This code is appending entries to mParameters. I want to remove record from mParameters.How can I do this?

      Thanks.

       

      oPayload.length = 5;
      for(var m=0; m<oPayload.length; m++) {
          if(oPayload[m].xxx === null){
            //error! check value.
              return;
          }
      	tmpModel.update("/YOUR_ENTITYSet(Key1='Valu1',Key2='Value2')", oPayload[m], mParameters);
      }
      

       

      This code is appending record to mParameters.
      m for 1 okay ADDED,
      m for 2 okay ADDED,
      m for 3 not okay value is initial.Getting error message.

      I changing data and now pressing save button again.
      m for 1 okay ADDED,
      m for 2 okay ADDED,
      m for 3 okay ADDED,
      m for 4 okay ADDED,
      m for 5 okay ADDED

      Total count is not 5.It is 7.
      SubmitChanges is sending 7 record to ODATA.Because mParameters has 7 record.
      If I get error message I should remove record from mParameters or clear data.
      How can I do this?
      Thanks.

      Author's profile photo Michelle Crapo
      Michelle Crapo

      Very Nice!

      Michelle

      Author's profile photo Naseem VP
      Naseem VP

      Dear Ankur,

      very helpful and clearly described.

       

      Best regards,

      Naseem

      Author's profile photo Ella Maria
      Ella Maria

      Great information, I will tweet to my friends to get them to check it out. keep it up. Thanks for sharing!