on 2019 Nov 26 7:49 AM
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>
Hi Vikas Sreedharan,
I see that you are using sequentializeRequests = true; Not completely sure what it does, my guess is that it sends multiple requests? and not in one batch?
If yes, this might be the issue, obviously if one request in the loop gets completed before sending all the requests, it will call success or error based on the response of the odata call. So better to send them in the batch request and if the backend is abap based, then ask them to implement the changeset process to handle multiple data save at the same time.
So that is the reason why maybe some data is inserted and you might have error for some other data. BTW, what is the error u are getting?
Thanks,
Mahesh
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Mahesh.
The error message shown is
"{"message":"HTTP request failed",
"statusCode":500,
"statusText":"",
"headers":[],
"responseText":"<?xml version=\"1.0\" encoding=\"utf-8\"?>
<error xmlns=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\">
<code> 0050569259751EE4BA9710043F8A5115</code>
<message xml:lang=\"en\">In the context of Data Services an unknown internal server error occurred</message>
</error>"}"
User | Count |
---|---|
76 | |
10 | |
10 | |
10 | |
10 | |
9 | |
8 | |
7 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.