Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member182048
Active Contributor

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.

And the results of the Gateway callback. 

22 Comments
Labels in this area