Skip to Content

One of the features available with the latest release of the UI Development Toolkit for HTML5 (1.8.4) is the ability to batch multiple OData operations into a single call.

I thought I would share a quick example of how its done.

Below is a code snippet showing how to batch the creation of multiple Contact entities and POST them to SAP Netweaver Gateway. The code is part of a simple applications which displays Contacts retrieved from a Gateway OData call in a Table Control. The application has a button ‘Batch Save’, when the button is pressed the function below is called, on completion of the $Batch operation the Table Control shows the new Contact Entities.  The code also shows how easy it is to deep insert a second entity (CREATE_DEEP_ENTITY), Contact_Status represents a separate Entity which has a many to one relationship with the Contact entity.

batchTest : function(){
    that = view.getController();
    var status1 = {StatusTxt: 'Okilidokli'},
      contactEntry1 = {
        FirstName : 'Ned',
        LastName : 'Flanders',
        Email : '',
        Contact_Status: [status1],
      },
      status2 = {StatusTxt: 'Cuff em Lou'},
      contactEntry2 = {
        FirstName : 'Chief',
        LastName : 'Wiggum',
        Email : '',
        Contact_Status: [status2],
      };
    //create an array of batch changes and save
    var batchChanges = [];
    batchChanges.push( that.oModel.createBatchOperation("Contacts", "POST", contactEntry1) );
    batchChanges.push( that.oModel.createBatchOperation("Contacts", "POST", contactEntry2) );
    that.oModel.addBatchChangeOperations(batchChanges);
    //submit changes and refresh the table and display message
    that.oModel.submitBatch(function(data) {
      that.oModel.refresh();
      sap.ui.commons.MessageBox.show(data.__batchResponses[0].__changeResponses.length
        + " contacts created", sap.ui.commons.MessageBox.Icon.SUCCESS,
      "Batch Save", sap.ui.commons.MessageBox.Action.OK);
    }, function(err) {
      alert("Error occurred ");
    });
  },
 

Here is the HTTP Request, multiple operations wrapped up in a single Multipart MIME message.

/wp-content/uploads/2012/11/batch_request_157304.png

And the results of the Gateway callback. 

/wp-content/uploads/2012/11/contact_list_157300.png

To report this post you need to login first.

19 Comments

You must be Logged on to comment or reply to a post.

  1. Martin Ceronio

    Thanks for this blog, as it covers a topic that I think must come up frequently.

    However, did you have to do anything in the backend? You mentioned CREATE_DEEP_ENTITY, which when generated is just made to raise an error. What did you have to do there? Can  you maybe share some sample code?

    (0) 
  2. Derek Wichmann

    Hi John et al,

    We’re attempting to to use batch operations in UI5 (1.20.1) to update multiple entities.  The problem we have is that when we create batch operations with oData objects that the links which have been expanded from previous oData reads are also serialized (as JSON) and submitted to the server.

    I.e. if we have a Customer object that also has a ToCustomerContacts link that has been expanded, if we simply want to update a property in the Customer object, then create a batch operation and submit it, the ToCustomerContacts entities are also serialized and submitted as part of the payload.

    Of course this is the desired behavior when we do a CREATE (POST) in order to do a deep insert like your example, but in the case of update we only want to serialize and submit the entity that has changed.

    Do you know of a way to NOT serialize the links in the case of a PUT/MERGE?

    Thanks,

    Derek

    (0) 
  3. Matt Harding

    Did the trick for me, and honestly felt easier than doing creates and updates through the related oData methods… Thanks John.

    Cheers,

    Matt

    (0) 
  4. Hima B

    Hi,

    In our application, we used the same method. Initially implemented with the ui5 version 1.18. Now we upgrade the version to 1.22.

    Now, it is throwing the exception as ‘Default changeset implementation allows only one operation’

    Below is the request payload:

    RequestPayload.jpg

    But the response is Default changeset implementation allows only one operation.

    Please help.

    (0) 
    1. chetna dabra

      Hello,

      I am also trying to do batch operation, but i don’t know where to write batch request headers in UI5 apllication. There is view.js and contoller.js, in controller i am calling odata service. Where to write the following code:

      –batch

      Content-Type: multipart/mixed; boundary=changeset

      –changeset

      Content-Type: application/http Content-Transfer-Encoding: binary

      POST TravelagencyCollection HTTP/1.1 Content-Type: application/atom+xml Content-Length: ### *** –changeset Content-Type: application/http Content-Transfer-Encoding: binary

      POST TravelagencyCollection HTTP/1.1 Content-Type: application/atom+xml Content-Length: ###

      Not able to figure out this.

      Thank you,

      Regards,

      Chetna

      (0) 
  5. chetna dabra

    Hello john,

    Can you please tell me where to write content-type: multipart/mixed;boundary=chnageset etc etc all of that part??

    I have view.js file and controller.js.

    In controller i am defining a save method, in which i am calling my service. So how to write all that part inside it??

    Code snippet:

    var oModel = new sap.ui.model.odata.ODataModel(“http://***.**.*.***:****/sap/opu/odata/sap/****_SRV/$batch“,”true”,”****_****”, ” ****** “,{

      “X-Requested-With”: “JSONHttpRequest”, “Content-Type”: “multipart/mixed;boundary=changeset”,

      });

      for(var i=0;i<rowLen;i++)

      {

      NewData = {

      “ProcessType”:”ZDRQ”,

      “VisitType”:”ZDVR”,

      “VisitDate”: visitDateFormat.toString(),

      “StartTime”: startTimeFormat.toString(),

      “EndTime”: endTimeFormat.toString(),

      //”Activities”: $(“#act-col7-row0”).val(),

      “Activities”:activity.toString(),

      //”ActivityDesc”:$(“#activDesc-col6-row0”).val(),

      “ActivityDesc”:actvDescArr.toString(),

      “DealerNo” : $(“#dlrNum-col0-row0”).val(),

      “UserId”: “P00108156”

      };

       batchChanges.push( oModel.createBatchOperation(“/CREATE_DVR”, “POST”, NewData) ); 

      }

      console.log(batchChanges);

      oModel.addBatchChangeOperations(batchChanges); 

      oModel.submitBatch(function(data) { 

           oModel.refresh(); 

          console.log(data);

          

         }, function(err) { 

           alert(“Error occurred “); 

         }); 

    Please guide me.

    (0) 
  6. German Mumma

    For some very obvious reasons you have redefine 2 DPC_EXT methods:

    • /IWBEP/IF_MGW_CORE_SRV_RUNTIME~CHANGESET_BEGIN
    • /IWBEP/IF_MGW_CORE_SRV_RUNTIME~CHANGESET_END

    You do this by going to transaction SEGW. There, select your Web Service project, expand Runtime Artifacts. Right click on *_EX_DPC_EXT and select Go to ABAP Workbench.

    Select the method (might have to press EDIT MODE first) and press the button Redefine.

    After the selected methods code is displayed, just enter EXIT. and activate.

    Do this for both CHANGESET_BEGIN and CHANGESET_END.

    After that, I got my batch create operations working.

    Hope it helps.

    (0) 
  7. Lakshmi Narayana

    Hi John,

    Nice blog john, I too have worked on Batch calls (Create, Update and Delete) Operations through sap hana xsjs services.

    Cheers,

    Lakshman.

    (0) 
    1. Michael Appleby

      Unless you are asking for clarification/correction of some part of the Document, please create a new Discussion marked as a Question.  This is especially true when the Document in question is 3 years old and may not have been updated recently.

      The Comments section of a Blog (or Document) is not the right vehicle for asking questions as the results are not easily searchable.  Once your issue is solved, a Discussion with the solution (and marked with Correct Answer) makes the results visible to others experiencing a similar problem.  If a blog or document is related, put in a link.  Read the Getting Started documents (link at the top right) including the Rules of Engagement. 

      NOTE: Getting the link is easy enough for both the author and Blog.  Simply MouseOver the item, Right Click, and select Copy Shortcut.  Paste it into your Discussion.  You can also click on the url after pasting.  Click on the A to expand the options and select T (on the right) to Auto-Title the url.

      Thanks, Mike (Moderator)

      SAP Technology RIG

      (0) 
  8. Gorje Fregorio

    Quick question here…
    Say I have two servers: gateway server and R/3 server. My GW server calls functions which are located in my R/3 server. Now, if I were to make a POST request of 3 records using batch… how many requests does GW server make to R/3 server?

    (0) 

Leave a Reply