Hi,
In this example there are 2 functions, and accessing ES5 system
Both functions calls the same insertProduct() for inserting to DB.
In the onInsertUsingLoop(), if Loop index is greater than 1, the call reaches error function of oData.create(), and error message is shown. But surprising part, is that the data also gets inserted into table.
Can’t understand, the call reaches, error(), but still data is being inserted to DB table?
Any idea why this is happening.
The code logic, is first search DB for the corresponding ProductID using read(), if record is there, then update the row, if not insert the record using create().
For ease, I have used alerts inside the functions
The View1 Controller code is below
sap.ui.define( ["sap/ui/core/mvc/Controller", "sap/m/MessageBox", "sap/ui/model/odata/v2/ODataModel", "sap/ui/model/json/JSONModel" ], function (Controller, MessageBox, ODataModel, JSONModel) { return Controller.extend("ibm.fin.ar.controller.View1", { onInit: function () { }, onAfterRendering: function () { // Call Function to set number of Records this.onGetCount(); }, onGetCount: function () { // Get Number of Records from ProductSet Table var oDataModel = this.getView().getModel(); var oIntResult, sMsg, olblStatus; olblStatus = this.getView().byId("idCountInfo"); debugger; oDataModel.sequentializeRequests = true; //oDataModel.attachRequestCompleted(function(){ oDataModel.read("/ProductSet/$count", { success: function (oEvent, oResult) { debugger; // check if the result is a number if (isNaN(oResult)) { oIntResult = parseInt(oResult.body); sMsg = "Number of Records in ProductSet Table are " + oIntResult; olblStatus.setText(sMsg); } }, // success error: function (oError) { debugger; console.log(oError); } }); //}); }, onInsertSingleRecord: function () { // insert single record data into ES5 table debugger; // Generate Random Product ID; var sProductID; sProductID = "VS-" + (Math.floor((Math.random() * 100000)) + 123456); this.insertProduct(sProductID); this.onGetCount(); }, onInsertUsingLoop: function () { // Insert by reading from JSON var iLoopLength = 1, iLoopIndex, sProductID; debugger; for (iLoopIndex = 0; iLoopIndex < iLoopLength; iLoopIndex++) { sProductID = "VS-" + (Math.floor((Math.random() * 100000)) + 123456); this.insertProduct(sProductID); } // end For Loop // Get Count of Records this.onGetCount(); }, createProduct: function (payload) { debugger; var oDataModel = this.getView().getModel(); var sProductID = payload.ProductID; oDataModel.sequentializeRequests = true; oDataModel.create("/ProductSet", payload, { success: function (data) { debugger; alert("The product " + sProductID + " has been created successfully"); }, error: function (oError) { alert("Error in creating Product " + sProductID); debugger; } }); }, updateProduct: function (productId, payload) { var oDataModel = this.getView().getModel(); oDataModel.sequentializeRequests = true; oDataModel.update("/ProductSet('" + productId + "')", payload, { success: function () { alert("The product " + productId + " has been updated successfully"); }, error: function (oError) { alert("Error in updating " + productId); debugger; } }); }, insertProduct: function (sProductID) { var that = this; var oDataModel = this.getView().getModel(); oDataModel.sequentializeRequests = true; var oProperties = { ProductID: sProductID, TypeCode: "PR", Category: "Notebooks", SupplierID: "0100000001", Name: "German Notebook2", TaxTarifCode: 1, CurrencyCode: "EUR", MeasureUnit: "EA", Price: "9999" }; oDataModel.read("/ProductSet('" + sProductID + "')", { success: function (oEvent, oResult) { debugger; if (oEvent) { that.updateProduct(sProductID, oProperties); } }, error: function (oError) { debugger; that.createProduct(oProperties); } }); }, }); });
The View1 xml code is below
<mvc:View xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m" xmlns:f="sap.ui.layout.form" xmlns:l="sap.ui.layout" controllerName="abc.controller.View1"> <App> <Page title="oData Test"> <content> <f:SimpleForm id="idOdataForm" editable="true"> <f:content> <l:VerticalLayout> <Label text="Product ID"/> <Input id="idProductID" /> <Label id="idCountInfox" text="" ></Label> <Label id="idCountInfo" ></Label> <Button id="idBtnCount" text="Get Product Count" press="onGetCount"></Button> <Button text="Insert Single Record to Product Table" press="onInsertSingleRecord"></Button> <Button text="Insert Multiple Records using Loop" press="onInsertUsingLoop"></Button> </l:VerticalLayout> </f:content> </f:SimpleForm> </content> <footer> <Toolbar width="100%" id="idToolbar1"> <content> <Button text="Save" width="100px" id="idButton2" press="onSave"/> </content> </Toolbar> </footer> </Page> </App> </mvc:View>