cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

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

ChandraMahajan
Active Contributor
0 Kudos

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

Former Member
0 Kudos

That is not really helpful.

There is no reasonable end-to-end example for implementing the batch read operation for ODataModelV2.

This is how I tried to do it after trying to put together what was written in the doc:


var oInputHelpModel = new ODataModelV2(sServiceUrl);

            oInputHelpModel.read({

                path: "/EntityOne?$filter=ValueID eq '026'",

                parameters: {batchGroupId: "myId"}

            });

            oInputHelpModel.read({

                  path: "/EntityTwo",

                  parameters: {batchGroupId: "myId"}

            });

            oInputHelpModel.read({

                path: "/EntityOne?$filter=ValueID eq '027'",

                parameters: {batchGroupId: "myId"}

            });

            oInputHelpModel.read({

                  path: "/EntityThree",

                  parameters: {batchGroupId: "myId"}

            });

            oInputHelpModel.setDeferredBatchGroups(["myId"]);

            oInputHelpModel.submitChanges({

                batchGroupId: "myId",

                success: function (oData) {

                    console.log("Success!");

                    console.log(oData)

                },

                error: function (oError) {

                    console.log(oError);

                }

            });

This caused an error: Object doesn't support property or method 'lastIndexOf' <---- sap-ui-core.js (144,2986)

So obviously I am doing something wrong.

What am I trying to achieve:

Read multiple entitysets in one batch request and get the response data with data from all entitysets

0 Kudos

any solution for this??