Search code examples
odatasapui5

SAPUI5 oData.V2 How to invoke a function after everything in a batch request is done?


I have an issue while making an SAPUI5 odata V2 batch request :

           var that = this;
            var oServiceModel = that.getModel("oServiceModel");
                odataMod = this.getModel("Service");
            odataMod.setUseBatch(true);
            var aData = oServiceModel.getData();
            var stupidService = _.filter(aData, function (ae) {
                return ae.Info === "-N/A";
            });
             var i = 0 ; 
            _.forEach(stupidService, function (sap) {
                oGlobalBusyDialog.setText("Deleting service :" + sap.ObjectID);
                oGlobalBusyDialog.setTitle("Deleting Service");
                oGlobalBusyDialog.open();
                that.removeService(sap).then(function () {
                    if (i === 615) {
                        oGlobalBusyDialog.close();
                    }
                }).catch(function () {});
            });

my Delete function is like this:

removeService: function (service) {


    var that = this;
    return new Promise(
        function (resolve, reject) {
            odataMod.remove('/ProjectTaskServiceCollection(\'' + service.ObjectID + '\')/', {
                success: function (oData) {
                    resolve(oData);
                },
                error: function (oResult) {
                    that.handleError(oResult);
                    oGlobalBusyDialog.close();
                    reject(oResult);
                }
            });
        });

What's happening ,is that if I'm trying to delete 500 entry, and if 200 entry cannot be deleted, the error message gets displayed 200 times

How to make it in a way to only display the error message once ?

Also, I want to turn off the batch request once everything is done odataMod.setUseBatch(false); how to do it ?

*EDIT: *

I've manage to do :

var aDeffGroup = odataMod.getDeferredGroups();
//add your deffered group
aDeffGroup.push("deletionGroup");
for (var s = 0; s < 5; s++) {
    odataMod.remove('/ProjectTaskServiceCollection(\'' + stupidService[s].ObjectID + '\')/', {
        //pass groupid to remove method.
        groupId: "deletionGroup"
    });
}

odataMod.submitChanges({
    // your deffered group id
    groupId: "deletionGroup",
    success: function() {
        //Get message model data from Core and it contains all errors
        // Use this data to show in dialog or in a popover or set this to your local model see below code
        var aErrorData = sap.ui.getCore().getMessageManager().getMessageModel();
        console.log(aErrorData);
    }
});

yet stills my console.log(aErrorData); still prints multiple error message

enter image description here


Solution

  • Instead of doing individual deletion odata calls. Add these all remove methods in a single group, then call odatamod.submitChanges() method.

    Example:

    //get all deffered groups
        var aDeffGroup = odataMod.getDeferredGroups();
    
    //add your deffered group
        aDeffGroup.push("deletionGroup");
    
    //set it back again to odatamodel
        odataMod.setDeferredGroups(aDeffGroup);
    
        odataMod.remove('/ProjectTaskServiceCollection(\'' + service.ObjectID + '\')/', {
     //pass groupid to remove method.
             groupId: "deletionGroup"});
         odataMod.submitChanges({
         // your deffered group id
         groupId:"deletionGroup",
         success: function() {
           //Get message model data from Core and it contains all errors
           // Use this data to show in dialog or in a popover or set this to your local model see below code
          var aErrorData = sap.ui.getCore().getMessageManager().getMessageModel();
    
       });