on 05-30-2017 7:58 AM
Hi Everyone,
I want to save multiple records from UI to DB tables through Odata service.
So I created a post request for saving data and sent multiple records one by one. But it's saving only one record. and if I comment code line no. 4 then it's show an error message that default changeset can handle only one operation.
Code is shown below:
var onewModel = this.getView().getModel("new");
var aBookings = onewModel.getProperty("/Bookings");
var oDataModelForData = this.getView().getModel();
oDataModelForData.setUseBatch(false);
var oNeedfulData = {};
var oTable = this.getView().byId("tablegrid");
var aItems = oTable.getItems();
for( var w = 0; w <aItems.length ; w++) {
oNeedfulData.AirlineCode = aBookings[w].AirlineCode;
oNeedfulData.ConnectionNumber = aBookings[w].ConnectionNumber;
oNeedfulData.CityFrom = aBookings[w].CityFrom;
oNeedfulData.CityTo = aBookings[w].CityTo;
oDataModelForData.create('/FLBOOKINGSet', oNeedfulData);
}
Thanks in advance
Regards
Sukhram Bhamboo
Try this:
oDataModelForData.create('/FLBOOKINGSet', oNeedfulData,{changeSetId: w});
It will send each entry in a separate change set and the changeset id will be your loop's iterator variable 'w'.
And if you want to process all records in one changeset you need to implement changeset_begind and changeset_process methods in the Gateway. You can check class /IWBEP/CL_MGW_RT_SFLIGHT for an example.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Have you changed this oDataModelForData.setUseBatch(false); to oDataModelForData.setUseBatch(true); ?
If you put a breakpoint in your create method in gateway does it trigger for each record or only once?
This is code from my app:
for (var i = 0; i < aConfigItems.length; i++) {
var oIbase = this._createIbasePayload(aConfigItems, aConfigItems[i]);
this._oDataModel.create("/IbaseHeaderSet", oIbase, {
changeSetId: i,
success: function(oData){
},
error: function(oError){
}
});
}
I changed this oDataModelForData.setUseBatch(false); to oDataModelForData.setUseBatch(true);
I put a breakpoint in create method in gateway. It triggers for each record.
but problem is that I am sending different different data records from ui but when i check $batch then it shows me same data. I think that is the problem. Can u suggest any kind of help?
Here I am sending two different data records with AirlineCode, ConnectionNumber, BookingId, CityFrom, CityTo etc.
But when I check $batch file it shows me first record twice.
--changeset_fdcb-16e4-6351 Content-Type: application/http Content-Transfer-Encoding: binary
POST FLBOOKINGSet HTTP/1.1 sap-contextid-accept: header Accept: application/json Accept-Language: en-US DataServiceVersion: 2.0 MaxDataServiceVersion: 2.0 sap-cancel-on-close: true x-csrf-token: oi-h9XJfcVVnZklP-RKo8g== Content-Type: application/json Content-Length: 188
{"AirlineCode":"LH","ConnectionNumber":"0400","BookingID":"QJPTPYVL","CityFrom":"FRANKFURT","CityTo":"NEW YORK","Distance":"6162","DistanceID":"KM","BookingDate":"\/Date(1498826154000)\/"} --changeset_fdcb-16e4-6351--
--batch_094f-9b12-bbb0 Content-Type: multipart/mixed; boundary=changeset_ce00-bacd-00cc
--changeset_ce00-bacd-00cc Content-Type: application/http Content-Transfer-Encoding: binary
POST FLBOOKINGSet HTTP/1.1 sap-contextid-accept: header Accept: application/json Accept-Language: en-US DataServiceVersion: 2.0 MaxDataServiceVersion: 2.0 sap-cancel-on-close: true x-csrf-token: oi-h9XJfcVVnZklP-RKo8g== Content-Type: application/json Content-Length: 188
{"AirlineCode":"LH","ConnectionNumber":"0400","BookingID":"QJPTPYVL","CityFrom":"FRANKFURT","CityTo":"NEW YORK","Distance":"6162","DistanceID":"KM","BookingDate":"\/Date(1498826154000)\/"} --changeset_ce00-bacd-00cc--
--batch_094f-9b12-bbb0 Content-Type: application/http Content-Transfer-Encoding: binary
GET FLBOOKINGSet/$count HTTP/1.1 sap-contextid-accept: header Accept: text/plain, */*;q=0.5 Accept-Language: en-US DataServiceVersion: 2.0 MaxDataServiceVersion: 2.0 sap-cancel-on-close: true x-csrf-token: oi-h9XJfcVVnZklP-RKo8g==
--batch_094f-9b12-bbb0 Content-Type: application/http Content-Transfer-Encoding: binary
GET FLBOOKINGSet?$skip=0&$top=105 HTTP/1.1 sap-contextid-accept: header Accept: application/json Accept-Language: en-US DataServiceVersion: 2.0 MaxDataServiceVersion: 2.0 sap-cancel-on-close: true x-csrf-token: oi-h9XJfcVVnZklP-RKo8g==
--batch_094f-9b12-bbb0--
There may be an issue with this code:
oNeedfulData.AirlineCode = aBookings[w].AirlineCode;
oNeedfulData.ConnectionNumber = aBookings[w].ConnectionNumber;
oNeedfulData.CityFrom = aBookings[w].CityFrom;
oNeedfulData.CityTo = aBookings[w].CityTo;
where oNeedfulData it will always be a reference to the array record rather than a copy hence pointing to the last record.
Try this instead:
for( var w = 0; w <aItems.length ; w++) {
var oNeedfulData = {
AirlineCode : aBookings[w].ConnectionNumber,
ConnectionNumber: aBookings[w].ConnectionNumber,
CityFrom : aBookings[w].CityFrom,
CityTo : aBookings[w].CityTo
};
oDataModelForData.create('/FLBOOKINGSet', oNeedfulData);
}
Hi,
I applied your suggested changes as well as changeSetId also and now it's working perfectly fine. So thanks a lot for your suggestion and giving your valuable time.
But I didn't get a point. Why it was not working in previous scenario? We were putting all the data records in variable oNeedFulData one by one via for loop and then wrote oDataModelForData.create('/FLBOOKINGSet', oNeedfulData, {changeSetId : w}); for each record.
I saw oNeedFulData in debugger mode it was having new record every time.
So Can you explain what is the reason behind it?
Finally Once more thank you for your valuable suggestions.
you can try using batch operation
var batchArray = [];
if (!this.oModel) {
this.oModel = new ODataModel("/sap/opu/odata/sap/BPMON_SELOPS_SRV/", {
defaultBindingMode: "TwoWay",
refreshAfterChange: false
});
}
for (var h = 0; h < renamed.length; h++) {
batchArray.push(this.oModel.createBatchOperation("entityset(data1, data2, data3)", "PUT"/"POST"/"DELETE", oEntry));
}
this.oModel.addBatchChangeOperations(batchArray);
this.oModel.setUseBatch(true);
this.oModel.submitBatch(jQuery.proxy(this.successcallback, this), jQuery.proxy(this.failurecallback, this), true);
In the NetWeaver Gateway, you need to redefine the changesetbegin and changesetend methods
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.