Skip to Content
avatar image
Former Member

Batch Requests using sap.ui.model.odata.v2.ODataModel

Hi! The new Developer Guide is recommending switching to OdataModel V2, but there are few resources on how to switch from one model to another.

For example I am interested in how would batch reading of different entities would look like using the new ODataModel?

Below is the approach for the deprecated ODataModel:

var batchReadOperations = [];
batchReadOperations.push(oInputHelpModel.createBatchOperation("EntityTypeSet_1", "GET"));
batchReadOperations.push(oInputHelpModel.createBatchOperation("EntityTypeSet_2", "GET"));

oInputHelpModel.addBatchReadOperations(batchReadOperations);
oInputHelpModel.submitBatch(
   function (oData) {
     // do stuff
   },

   function (oError) {
   console.log("oError in Batch Read of Input Helps:");
   }
);


How would it look like using the new Model? I check the following link on instantiating ODataModel-s, but the example on batch processing is not very helpful:

Instantiating an OData Model

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • avatar image
    Former Member
    Sep 07, 2016 at 12:52 AM

    Hi German,

    The V2 version is actually pretty slick at handling this. I'll paste a working snippet of mine below with a lot of the extra details stripped out. My service is running on an SAP Gateway. I use a JS Closure to have some of the controls in the scope of the "OnPress" event which will lead to the batch update, but you can imagine that after clicking the "Submit" button you'll start at line 03. in the source below. You'll see I then loop through a table, accruing PUT requests using the key on each row. These are finally submitted in the end with the oModel.submitChanges() function. This should trigger a proper $batch request split into consecutive PUT's just as the SAP Gateway documentation dictates - $batch Processing - SAP Gateway Foundation (SAP_GWFND) - SAP Library.

    In getting my syntax right on all the UI5 issues, I found this helpful: SAPUI5 SDK - Demo Kit

    var _postPaymentClosure = function(itemsTable){
        return function(oEvent){
            var source = oEvent.getSource();
            var oModel = source.getModel();
    
            oModel.setUseBatch(true); //if this is not already done
            var scopeKey = paymentScopeRBG.getSelectedIndex();
            var items = itemsTable.getItems();
            var changeSetId = "foo";
                            
            //Prep header-level group/changeset params
            var mParameters = {
                "groupId": changeSetId,
                "changeSetId": changeSetId
            }; //I'm using both...maybe one isn't needed??
    
         //Loop through the items, updating each
            var row;
            var itemObject;
            var context;
            for(var i = 0; i < items.length; i++){
                row = items[i];
                context = row.getBindingContext();
                itemObject = context.getObject();
                
                //Do whatever updates to the JSON you want here....
                itemObject.PaymentCardID = "bar";
               
              //Then execute the v2 model's Update function
               oModel.update(context.sPath, itemObject, mParameters);
            }
            
    //Finally, submit all the batch changes
            oModel.submitChanges(mParameters);
        };
    };
    

    Cheers!

    -Ian Newton

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 27, 2016 at 09:45 AM

    Refer Batch Processing section of same link where it is mentioned,

    The v2.ODataModel supports batch processing ($batch) in two different ways:

    • Default: All requests in a thread are collected and bundled in batch requests, meaning that request is sent in a timeout immediately after the current call stack is finished. This includes all manual CRUD requests as well as requests triggered by a binding.
    • The requests are stored and can be submitted with a manual submitChanges() call by the application. This also includes all manual CRUD requests as well as requests triggered by a binding.

    Regards,

    Chandra

    Add comment
    10|10000 characters needed characters exceeded