sending data to the back-end using JSON format
Hi,
Introduction:
In UI5 we can send multiple records using batch operation. Here i have a application which can send data to the back-end using array of objects and i also gone through other blogs in SCN which describes about batch operations but they used static objects for sending data. In this application you can able to send more number of records using dynamic object creation by converting data into JSON format.
Description:
The application contains some input fields and table for storing data for performing operations like add, change and delete.After completion of data operations if you want send multiple records to the back-end using batch operations,first you have convert data into JSON format.
Below code snippet describes how to get table data and pushing into an array.
var items = [];// intializing an array
var aTableData = oTable.getModel().getData();// getting table data
for (i = 0; i < oTableData.length; i++) {
items.push( {
“ItemKey”: aTableData[i].Item,
“ItempartGrp”: oTableData[i].itemcodegroup,
“ItempartCod”: oTableData[i].itemcode,
“ItemdefectGrp”: oTableData[i].problemcodegroup,
“ItemdefectCod”: oTableData[i].problemcode,
“ItemdefectShtxt”: oTableData[i].problemdescription,
“CauseGrp”: oTableData[i].causegroup,
“CauseCod”: oTableData[i].causecode,
“CauseShtxt”: oSel[i].causedescription,
});
}
// oEntry is the main object to send data using batch operation.
//Pushing table data with some other values(i,e NotifType,FunctionLoc).
var oEntry = {
“NotifType”: xtitle,
“Qmnum”: ” “,
“NotifShorttxt”: shrttext,
“FunctionLoc”: FuncLocation,
“Equipment”: Equipmentno,
“NavNotifItems”: JSON.parse(JSON.stringify(items)),
// converting data into json format
// string type will converted into json object by parsing it.
};
var sServiceUrl = “http://your url SRV/”; // ODATA URL
var oDModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true);
//creating batch operation
oDModel.create(“/url”, oEntry, null, function(oData, oResponse)
{
//function openDialog() {
var oDialog1 = new sap.m.Dialog();
oDialog1.setTitle(“Success Message”);
var ovalue = new sap.m.Text({
text: “Notification” + oData.Qmnum + ” is Created”
});
var oVmain = new sap.m.FlexBox(“Dialog”, {
items: [ovalue]
});
oDialog1.addContent(oVmain);
oDialog1.addButton(new sap.m.Button({
text: “OK”,
press: function() {
oDialog1.close();
}
}));
oDialog1.open();
// };
console.log(oResponse);
sap.ui.getCore().byId(“detailPage”).rerender();
},
function(err) {
//Error Callback
jQuery.sap.require(“sap.m.MessageBox”);
sap.m.MessageBox.show((JSON.parse(err.response.body).error.message.value));
}
table screen shot for performing data operations
NOTE : Upto my knowledge this is the best way to adding table data to the back-end using batch operation by converting data into JSON format.
if any doubts regarding this hit a comment and if any better methods please let me know.
Thanks and Regards
Venkatachala C.K
Hi Venkat,
It's very useful blog.
looks good.
thank you
Hi Venkat ,
its really nice blog very helpful.
Hi Venkatachala,
Nice try, But I have some query.
1. Creating Batch operation - SAPUI5-Odata Model has batch operation property to send the series of jobs into database. But here you are using for loop with oDATA call for oTableData.length number of time, is it any specific reason?
Gateway Batch Calls from SAPUI5
JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.model.odata.ODataModel
2. Correct me if I am wrong, If oDATAModel calls occurs oTableData.length number of times, It might give a performance issue and reduce the database health.
Assume my table has 10,000 records, will this code calls 10,000 times to trigger oDATAModel class?
Thanks,
Karthik A
Hi Karthik,
Sorry i forgot to close for loop.(i corrected now)
actually i'm pushing all data to an array(items) and i'm putting array in to an object(oEntry).
Here oDATAModel call occurs only one time using batch operation and i'm taking oTableData.length to push all table items at time.
thanks for correcting me
Hi Venkatachala,
Your code is returning an error for me:Error while parsing an XML stream
How did you set your Entity in the gateway so that one parameter can receive a table?
The problem is when I call the service with the parameter: "NavNotifItems": JSON.parse(JSON.stringify(items)).
Thanks
Hi Sashko Janev,
if possble can u post your code.
I will try to correct it
thanks
Venkat
Thanks for the reply, i figured it out.
Regards
Is there any thing wrong in this code?
No, perfectly fine. The problem was when I tried passing integers and the code couldn't parse them to string for some reason.
However here's my working code for future reference:
for(i=0;i<3;i++){
items.push({
"Qmnum": '11',
"Kunum": '22'
});
};
var oEntry = {
"Qmnum": '1',
"Kunum": '2',
"inTab": JSON.parse(JSON.stringify(items)),
};
oModel.create("/oDeepInsertSet", oEntry, null, function(oData, oResponse)
{
oJSONModel.setData(oData.inTab);
},
function(err) {
// Error Callback
jQuery.sap.require("sap.m.MessageBox");
sap.m.MessageBox.show((JSON.parse(err.response.body).error.message.value));
});
this.setModel(oJSONModel, 'theModel');
ok thank you.
Nice blog:)
But i have another doubt..
I have created a matrix layout to get user inputs and trying to manually show that inputs getting added to table (just to display) on screen by adding those inputs,,and then i want to post the table data to back end array using batch operation..
Can u help me on how to add user inputs to my table on screen???
Please create a new Discussion marked as a Question. The Comments section of a Blog (or Document) is not the right vehicle for asking questions as the results are not easily searchable. Once your issue is solved, a Discussion with the solution (and marked with Correct Answer) makes the results visible to others experiencing a similar problem. If a blog or document is related, put in a link.
NOTE: Getting the link is easy enough for both the author and Blog. Simply MouseOver the item, Right Click, and select Copy Shortcut. Paste it into your Discussion. You can also click on the url after pasting. Click on the A to expand the options and select T (on the right) to Auto-Title the url.
Thanks, Mike (Moderator)
SAP Technology RIG
I need to copy that whole reply to use SEVERAL places elsewhere. 😛
Hi Christopher,
Feel free. I keep the text file (just sent to you in an email) open all day with mostly canned responses for just this kind of situation. Run Simple! after all.
Cheers, Mike
SAP Technology RIG
Hi venkatachala,
Helpful post but for me this code ran into some issues.
Where you're pushing table data with some other values(i,e NotifType,FunctionLoc) there are variables used like xTitle for NotifType, FuncLoc for Function location and so on. Are these mandatory fields to be declared? also if it is then what do we put in these fields?
For e.g. what do I put in the variable xTitle that is assigning value to NotifType?
Because I am not having the right data for this variable I am getting the following error-
Hello Chandan,
This is just a sample data in JSON format for posting, and the same payload is not mandatory for all the scenarios. If i know your scenario may be i can help you with that.
Thanks
Venkat
Why do I need the oEntry?
Another question please: what do I have to implement on sap gateway side?
Thank you very much.
Regards.
Hello David Raven,
oEntry is the payload structure to post data into backend using oData create operation.
Thanks
Venkat
Hi Venkat,
Thanks for your response.
In my case now that I have made my own oEntry payload with just one field for e.g. "Jobs"
When I execute the code I get the error that the property "Jobs" is invalid.
something like this-
"message":{"lang":"en","value":"Property 'Jobs' is invalid"}
I feel there should be something maintained at the backend side? isn't it? at the gateway maybe?
What is NavNotifItems in your backend code (in SAP)?
Regards,
Chandan
Hi again,
And where did you create NavNotifItems ?
Thank you.
Hi David, I hope you got the answer to Property ‘NotifType’ is invalid.Â
I am also getting the same error message. either Venkadachalam or any helper can help me as I am new in OData? My requirement is not a deep insert to push master and details data. I just wanted to push simple only one table rows. thx. - Swami