cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI Table Batch Create not working

adnanmaqbool
Contributor
0 Kudos

Hi All

I have a Table Control in View having multiple editable rows of data. We want to submit all rows data to backend system using Batch of all rows but on save button backend system getting duplicated data of last row in all records , lets say if table is having 8 rows, all 8 rows receiving data of 8th row (last record) only. Which means batch is not working or some issue with loop in below mentioned code. Please check.

onSave: function(oEvent) {
		this._oViewModel.setProperty("/enableCreate", false);
		var requestBody = {};
            	var that = this,			oView = this.getView(),
					oModel = this.getModel();
//spath = oView.getBindingContext().getPath();
					//oModel = sap.ui.getCore().getModel("Leaves"),
					
//var bindingContext = this.getView().getBindingContext();
		//	var object = bindingContext.getObject();
			var oTable = this.getView().byId("table_id");
			var leave = sap.ui.getCore().getModel("Leaves");
			var len = oTable.getItems().length; // Get total Line Items in table
                 oModel.setUseBatch(true);
           
            oModel.setDeferredGroups(["foo"]);
            var mParameters = {
            groupId:"foo",
            success:function(odata, resp){ 
            	console.log(resp); },
            	error: function(odata, resp)
            	{ console.log(resp); }
            	
            };
		/********** Updating Odata MOdel ******************/
			
                for (var i = 0; i < len; i++) {
                        var oEntry = leave.oData.results[i];                        
                        requestBody.Comments = this.getView().byId("com_id").getValue();
                        requestBody.Pernr = leave.oData.results[i].Pernr;
                        requestBody.Pernr = leave.oData.results[i].Pernr;
                        requestBody.Atext = leave.oData.results[i].Atext;                        
                        requestBody.Begda = leave.oData.results[i].Begda;
                        requestBody.Endda = leave.oData.results[i].Endda;
                        requestBody.Ktalg = leave.oData.results[i].Ktalg;
                        requestBody.Subty = leave.oData.results[i].Subty;

                        	
                oModel.create("/LeaveRequestSet", requestBody, mParameters, {
        				 method: "POST",
            	   success: function(data) {

        				 },
        		 error: function(e) {

        			 } });   }			    

				/********** Updating Odata MOdel ******************/
				// abort if the  model has not been changed
				if (!oModel.hasPendingChanges("foo")) {
					MessageBox.information(
						this._oResourceBundle.getText("Foo"), {
							id: "noChangesInfoMessageBox",
							styleClass: that.getOwnerComponent().getContentDensityClass()
						}
					);
					return;
				}
				this.getModel("appView").setProperty("/busy", true);
				if (this._oViewModel.getProperty("/mode") === "edit") {
					// attach to the request completed event of the batch
					oModel.attachEventOnce("batchRequestCompleted", function(oEvent) {
						if (that._checkIfBatchRequestSucceeded(oEvent)) {
							that._fnUpdateSuccess();
						} else {
							that._fnEntityCreationFailed();
							MessageBox.error(that._oResourceBundle.getText("updateError"));
						}
					});
				}
				

			oModel.submitChanges(mParameters);

			},
			<br>

Accepted Solutions (1)

Accepted Solutions (1)

adnanmaqbool
Contributor
0 Kudos

Issue have been resolved. Issue was array within loop . Following code is working.

for (var i = 0; i < len; i++) {
                        var oEntry = leave.oData.results[i];
                        //requestBody.RequestId = object.RequestId;
                        //requestBody.Gjahr = object.Gjahr;
                       var requestBody1 = {
                     Comments : this.getView().byId("com_id").getValue(),
                        Pernr : leave.oData.results[i].Pernr,
                        Atext : leave.oData.results[i].Atext,
                        Begda : leave.oData.results[i].Begda,
                        Endda : leave.oData.results[i].Endda,
                        Ktalg : leave.oData.results[i].Ktalg,
                        Subty : leave.oData.results[i].Subty
                        };

                        	
   oModel.create("/LeaveRequestSet", requestBody1, mParameters, {
        				 method: "POST",
            	success: function(data) {

        				 },
        		 error: function(e) {

        			 }

Answers (0)