cancel
Showing results for 
Search instead for 
Did you mean: 

Save multiple records from SAPUI5 to DB Table through ODATA service

former_member264217
Participant
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

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.

former_member264217
Participant
0 Kudos

Tried this:

oDataModelForData.create('/FLBOOKINGSet', oNeedfulData,{changeSetId: w});

It's not working. Actually in case of multiple records, it saves only last entry into database.

Former Member
0 Kudos

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){
						
					}
				});
			}

former_member264217
Participant
0 Kudos

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.

    1. Provisional headers are shown
    2. Accept: multipart/mixed
    3. Accept-Language: en-US
    4. Content-Type: multipart/mixed;boundary=batch_094f-9b12-bbb0
    5. DataServiceVersion: 2.0
    6. MaxDataServiceVersion: 2.0
    7. Origin: https://webidetesting7854730-i334119sapdev.dispatcher.int.sap.hana.ondemand.com
    8. Referer: https://webidetesting7854730-i334119sapdev.dispatcher.int.sap.hana.ondemand.com/webapp/index.html?hc...
    9. sap-cancel-on-close: true
    10. sap-contextid-accept: header
    11. User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
    12. x-csrf-token: oi-h9XJfcVVnZklP-RKo8g==
  1. Request Payload
    1. --batch_094f-9b12-bbb0 Content-Type: multipart/mixed; boundary=changeset_fdcb-16e4-6351

      --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--

  2. Name
    $batch

    Former Member

    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);
    }
    former_member264217
    Participant
    0 Kudos

    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.

    Former Member
    0 Kudos

    Javascript can be tricky for people coming from e.g. ABAP background as it behaves in a different way. Google javascript "closures" it will explain the problem you were having.

    Answers (1)

    Answers (1)

    benu_mariantony2
    Participant
    0 Kudos

    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
    
    
    
    
    
    Former Member
    0 Kudos

    That is using v1 oData model which is obsolete