Batch Calls - V1 and V2 - Read Example

 V2 - class sap.ui.model.odata.v2.ODataModel


onAfterOpen: function (param) {

var oModel = this.getOwnerComponent().getModel();

oModel.setDeferredGroups(["batchget"]);

oModel.setUseBatch(true);



oModel.read("/DocumentPartnerFunctionSet", {

filters: [new Filter("DocumentNumber", sap.ui.model.FilterOperator.EQ, this.oDocNumber)],

groupId: "batchget",

changeSetId: "batchget",

success: function (oResponse) {

var oPFModel = new JSONModel(oResponse.results);

oPFModel.setSizeLimit(oResponse.results.length);

sap.ui.core.Fragment.byId("idRDialog", "idPFMCBR").setModel(oPFModel);

var convTo = this.getView().getModel().getData()[0].ConvPartFuncTo;

var newarray = convTo.split(";");

sap.ui.core.Fragment.byId("idRDialog", "idPFMCBR").setSelectedKeys(newarray);

sap.ui.core.BusyIndicator.hide();

}.bind(this),

error: function (oError) {

this.showReadWriteErrors(oError);

sap.ui.core.BusyIndicator.hide();

}.bind(this)

});





oModel.read("/ConversationReasonCodesSet", {

groupId: "batchget",

changeSetId: "batchget",

success: function (oResponse) {

var oRCModel = new JSONModel(oResponse.results);

oRCModel.setSizeLimit(oResponse.results.length);

sap.ui.core.Fragment.byId("idRDialog", "idRCMCBR").setModel(oRCModel);

//var reasonCodes = this.getView().getModel("docNumModel").getproperty("/ConvReasonCode")[0];

var reasonCodes = this.getView().getModel().getData()[0].ConvReasonCode;

var newarray = reasonCodes.split(";");

sap.ui.core.Fragment.byId("idRDialog", "idRCMCBR").setSelectedKeys(newarray);

sap.ui.core.BusyIndicator.hide();

}.bind(this),

error: function (oError) {

this.showReadWriteErrors(oError);

sap.ui.core.BusyIndicator.hide();

}.bind(this)

});


oModel.submitChanges({

groupId: "batchget",

changeSetId: "batchget",

success: function (data, response) {

oModel.setUseBatch(false);

},

error: function (e) {

oModel.setUseBatch(false);

}

});

},


Changeset related methods are supposed to be triggered only for Create, Update, Delete related operations and read requests related methods will be called separately even though they are in the same batch request.--- this is wr.t Odata.


class sap.ui.model.odata.ODataModel

Deprecated in version: 1.48

var oModel=this.getOwnerComponent().getModel();
for (var i = 0; i < finalArr.length; i++) { // for loop iteration
var oEntry = finalArr[i];
var sPath = "/baaEditQuestSet"; 
var sModel=oModel;
var that=this;
var createBatch = sModel.createBatchOperation(sPath, "POST", oEntry); // create operations in for loop
var batchChanges = [];
batchChanges.push(createBatch);
sModel.addBatchChangeOperations(batchChanges);
sModel.setUseBatch(true);
}


sModel.submitBatch(
function(data, oresponse, msg) {  // success function

sModel.setUseBatch(false);


}, 
function(e) { // error function
that.CloseBusyDialog()
if(e.message==="HTTP request failed"){
var emsg=e.message;
}
else{
var emsg = JSON.parse(e.response.body).error.message.value;
}
MessageBox.show(emsg,{
icon: sap.m.MessageBox.Icon.ERROR,     
title: "Error"

});

sModel.setUseBatch(false);
});


Related Links :







Extra Lines :


Batch Processing

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.

  • Deferred: 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.

The model cannot decide how to bundle the requests. For this, SAPUI5 provides the groupId. For each Context and List Binding and each manual request, a groupId can be specified. All requests belonging to the same group are bundled into one batch request. Request without a groupId are bundled in the default batch group. You can use a changeSetId for changes. The same principle applies: Each change belonging to the same changeSetId is bundled into one changeSet in the batch request. Per default, all changes have their own changeSet.

You can use the setDeferredGroups() method to set a subset of previously defined groups to deferred.

 Note

The same is also valid for setChangeGroups() and getChangeGroups().

All requests belonging to the group are then stored in a request queue. The deferred batch group must then be submitted manually by means of the submitChanges() method. If you do not specify a batch group ID when calling submitChanges, all deferred batch groups are submitted.

 Example

Set a subset of groups to deferred:

// "ODataModel" required from module "sap/ui/model/odata/v2/ODataModel"
var oModel = new ODataModel(myServiceUrl);

Pass the groupId to a binding:

{path:"/myEntities", parameters: {groupId: "myId"}}

Set the groupId to deferred:

  1. Get the list of deferred groups:

    var aDeferredGroups = oModel.getDeferredGroups();

  2. Append your groupId to the list:

    aDeferredGroups=aDeferredGroups.concat(["myId"]);

  3. Set all groups to deferred:

    oModel.setDeferredGroups(aDeferredGroups);

Submit all deferred groups:

oModel.submitChanges({success: mySuccessHandler, error: myErrorHandler});


Comments

Popular posts from this blog

Lets begin

2512380 - How to Transport Custom Themes in UI Theme Designer on ABAP