cancel
Showing results for 
Search instead for 
Did you mean: 

Save Multiple Row data to BackEnd

0 Kudos

Hi,

I'm having issues with saving multiple rows to the back-end. I don't know how to send all the rows in batch, so I was trying to send each row at a time but it hits <oModel.create("/MRU_ESTSet", oEntry> after it has put the last row into the oEntry.

submitButtonPress: function() {
	var oModel = this.getModel();
	var hasChanges = oModel.hasPendingChanges();

	if (hasChanges) {
		var mcJson = {};
                //get only rows with changes
		var modelChanges = oModel.getPendingChanges();
		mcJson = modelChanges;

		var mcJsonLength = Object.keys(mcJson).length;
		var mcJsonKey = Object.keys(mcJson);
		var officeCode = this.byId("officeCombo").getValue();
		var oEntry = {};

       //for each row get data
	for (var i = 0; i < mcJsonLength; i++) {
		var item = mcJsonKey[i];
		var obj = modelChanges[item];
		var estDate = this.convertDate(obj.ESTIMATE_DATE);

                oEntry.MRU_ID = obj.EST_MRU_ID.toString();
		oEntry.ESTIMATE_PRCT = obj.ESTIMATE_PRCT;
		oEntry.INSTALL_READ = obj.INSTALL_READ;
		oEntry.PLAN_ESTIMATE = obj.EST_INSTALL;
		oEntry.MRU_DATE = estDate;
		oEntry.OFFICE_CODE = officeCode.toString();*/


oModel.create("/MRU_ESTSet", oEntry, {
        success: function(oData, response) {
	       sap.m.MessageBox.alert("MRU: " + oEntry.MRU_ID + " EST DATE:                " + oEntry.MRU_DATE + " SAVED!");},
	error: function(oError) {
		sap.m.MessageBox.alert("Error Saving Entries!!");
	}
	});

	}
	} else {
	sap.m.MessageBox.alert("No Changes To Submit");
	}

}	

Accepted Solutions (0)

Answers (2)

Answers (2)

Thank You, Sergio but I only want to get the rows that changed using .getPendingChanges().

This is what ended up working for me. Adding this to the function:

oModel.setUseBatch(true);
oModel.create("/MRU_ESTSet", oEntry, {
                    method: "POST",
                    success: function(oData) {
                        //sap.m.MessageBox.alert("success sent!");
                    },
                    error: function(oError) {
                        //sap.m.MessageBox.alert("Error Saving Entries!!");
                    }
                });
            }
            oModel.submitChanges({
                success: function(oData, response) {
                    sap.m.MessageBox.success("Success Saving Entries!");
                },
                error: function(oError) {
                    sap.m.MessageBox.error("Error Saving Entries!!");
                }
            }); 

So the function ends up like this and only sends one confirmation instead of many:

submitButtonPress: function() {
        var oModel = this.getModel();
        oModel.setUseBatch(true);
        var hasChanges = oModel.hasPendingChanges();
        if (hasChanges) {
            var mcJson = {};
            var modelChanges = oModel.getPendingChanges();
            mcJson = modelChanges;
            var mcJsonLength = Object.keys(mcJson).length;
            var mcJsonKey = Object.keys(mcJson);
            var officeCode = this.byId("officeCombo").getValue();
            for (var i = 0; i < mcJsonLength; i++) {
                var item = mcJsonKey[i];
                var obj = modelChanges[item];
                var estDate = this.convertDate(obj.ESTIMATE_DATE);
                var oEntry = {
                    MRU_ID: obj.EST_MRU_ID,
                    ESTIMATE_PRCT: obj.ESTIMATE_PRCT,
                    INSTALL_READ: obj.INSTALL_READ,
                    PLAN_ESTIMATE: obj.EST_INSTALL,
                    MRU_DATE: estDate,
                    OFFICE_CODE: officeCode
                };
                oModel.create("/MRU_ESTSet", oEntry, {
                    method: "POST",
                    success: function(oData) {
                        //sap.m.MessageBox.alert("success sent!");
                    },
                    error: function(oError) {
                        //sap.m.MessageBox.alert("Error Saving Entries!!");
                    }
                });
            }
            oModel.submitChanges({
                success: function(oData, response) {
                    sap.m.MessageBox.success("Success Saving Entries!");
                },
                error: function(oError) {
                    sap.m.MessageBox.error("Error Saving Entries!!");
                }
            });
        } else {
            sap.m.MessageBox.alert("No Changes To Submit");
        }
    },
SergioG_TX
Active Contributor
0 Kudos

Elena,

if you have set up a model - you could simply read the model from the controller.

var aData = your_controller.getView().getModel('YOUR_NAMED_MODEL').getProperty('/modelPRoperty');

once you have your array of data in that variable.. you can pass it as an array.. you do not need to iterate thru any rows... simply read your model... hope it helps