Skip to Content
Author's profile photo John Patterson

Gateway Batch Calls from SAPUI5

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

Assigned Tags

      22 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Great Blog John !! Were you able to download latest version of UI5 from SDN ?

      Author's profile photo John Patterson
      John Patterson
      Blog Post Author

      Hi Vinit

      I was able to download the latest version of UI5 from SDN. It took me many attempts, including a number of partial downloads.

      Cheers

      JSP

      Author's profile photo Former Member
      Former Member

      John,

      Very nice blog.

      Your blog tells in detail about batch create .

      Can you please tell me how to perform batch update ?

      Author's profile photo Former Member
      Former Member

      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?

      Author's profile photo Former Member
      Former Member

      Hi,

      for me this is also a interesting question!

      I'm curious for some code samples.

      Regards

      Stefan

      Author's profile photo Former Member
      Former Member

      Nice Blog

      Regards

      Manoj

      Author's profile photo Derek Wichmann
      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

      Author's profile photo Matt Harding
      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

      Author's profile photo Vishnu Pankajakshan Panicker
      Vishnu Pankajakshan Panicker

      Thanks for the document.

      Author's profile photo Former Member
      Former Member

      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.

      Author's profile photo chetna dabra
      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

      Author's profile photo chetna dabra
      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.

      Author's profile photo Former Member
      Former Member

      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.

      Author's profile photo Ankit Maskara
      Ankit Maskara

      Crisp blog. Thanks John.

      BR.

      Author's profile photo Lakshmi Narayana
      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.

      Author's profile photo Former Member
      Former Member

      Hello.  And what is is the best way to "batch" requests in sap.ui.model.odata.v2.ODataModel ?

      Author's profile photo Michael Appleby
      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

      Author's profile photo Former Member
      Former Member

      Hi John,

      Thank you so much for your help. It is really helpful.

      🙂

      Author's profile photo Former Member
      Former Member

      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?

      Author's profile photo Priya Jha
      Priya Jha

      Hi John,

      If i have a Navigation Property from Contact  entity to Contact_Status entity,named as ContactStatusDetails, in that case how can i pass the payload using deep inserts?

      Author's profile photo Former Member
      Former Member

      Hello John,

       

      How to create batch operation with in create entity with out looping in front end, and how to pass all

      front end table rows into create entity to store using batch operation with create entity?

       

      Like i have scenario as i need to create a reservation number through BAPI FM, this called in create entity which i cant loop create entity for each and every front end -table( row ). I need only one reservation number for all the table rows.!

       

      Thanks,

      Raviteja

       

      Author's profile photo Sylvain Michel
      Sylvain Michel

      Hello John,

      I know is a lot of time by now but is there a possibility to add a path in this method?
      like for oModel.sPath + "/toComment", oEntry);

      Thank you in advance,

      Sylvain