Gateway Batch Calls from SAPUI5
One of the features available with the latest release of the UI Development Toolkit for HTML5 (1.8.4) is the ability to batch multiple OData operations into a single call.
I thought I would share a quick example of how its done.
Below is a code snippet showing how to batch the creation of multiple Contact entities and POST them to SAP Netweaver Gateway. The code is part of a simple applications which displays Contacts retrieved from a Gateway OData call in a Table Control. The application has a button ‘Batch Save’, when the button is pressed the function below is called, on completion of the $Batch operation the Table Control shows the new Contact Entities. The code also shows how easy it is to deep insert a second entity (CREATE_DEEP_ENTITY), Contact_Status represents a separate Entity which has a many to one relationship with the Contact entity.
batchTest : function(){
that = view.getController();
var status1 = {StatusTxt: 'Okilidokli'},
contactEntry1 = {
FirstName : 'Ned',
LastName : 'Flanders',
Email : '',
Contact_Status: [status1],
},
status2 = {StatusTxt: 'Cuff em Lou'},
contactEntry2 = {
FirstName : 'Chief',
LastName : 'Wiggum',
Email : '',
Contact_Status: [status2],
};
//create an array of batch changes and save
var batchChanges = [];
batchChanges.push( that.oModel.createBatchOperation("Contacts", "POST", contactEntry1) );
batchChanges.push( that.oModel.createBatchOperation("Contacts", "POST", contactEntry2) );
that.oModel.addBatchChangeOperations(batchChanges);
//submit changes and refresh the table and display message
that.oModel.submitBatch(function(data) {
that.oModel.refresh();
sap.ui.commons.MessageBox.show(data.__batchResponses[0].__changeResponses.length
+ " contacts created", sap.ui.commons.MessageBox.Icon.SUCCESS,
"Batch Save", sap.ui.commons.MessageBox.Action.OK);
}, function(err) {
alert("Error occurred ");
});
},
Here is the HTTP Request, multiple operations wrapped up in a single Multipart MIME message.
And the results of the Gateway callback.
Great Blog John !! Were you able to download latest version of UI5 from SDN ?
Hi Vinit
I was able to download the latest version of UI5 from SDN. It took me many attempts, including a number of partial downloads.
Cheers
JSP
John,
Very nice blog.
Your blog tells in detail about batch create .
Can you please tell me how to perform batch update ?
Thanks for this blog, as it covers a topic that I think must come up frequently.
However, did you have to do anything in the backend? You mentioned CREATE_DEEP_ENTITY, which when generated is just made to raise an error. What did you have to do there? Can you maybe share some sample code?
Hi,
for me this is also a interesting question!
I'm curious for some code samples.
Regards
Stefan
Nice Blog
Regards
Manoj
Hi John et al,
We're attempting to to use batch operations in UI5 (1.20.1) to update multiple entities. The problem we have is that when we create batch operations with oData objects that the links which have been expanded from previous oData reads are also serialized (as JSON) and submitted to the server.
I.e. if we have a Customer object that also has a ToCustomerContacts link that has been expanded, if we simply want to update a property in the Customer object, then create a batch operation and submit it, the ToCustomerContacts entities are also serialized and submitted as part of the payload.
Of course this is the desired behavior when we do a CREATE (POST) in order to do a deep insert like your example, but in the case of update we only want to serialize and submit the entity that has changed.
Do you know of a way to NOT serialize the links in the case of a PUT/MERGE?
Thanks,
Derek
Did the trick for me, and honestly felt easier than doing creates and updates through the related oData methods... Thanks John.
Cheers,
Matt
Thanks for the document.
Hi,
In our application, we used the same method. Initially implemented with the ui5 version 1.18. Now we upgrade the version to 1.22.
Now, it is throwing the exception as 'Default changeset implementation allows only one operation'
Below is the request payload:
But the response is Default changeset implementation allows only one operation.
Please help.
Hello,
I am also trying to do batch operation, but i don't know where to write batch request headers in UI5 apllication. There is view.js and contoller.js, in controller i am calling odata service. Where to write the following code:
--batch
Content-Type: multipart/mixed; boundary=changeset
--changeset
Content-Type: application/http Content-Transfer-Encoding: binary
POST TravelagencyCollection HTTP/1.1 Content-Type: application/atom+xml Content-Length: ### *** --changeset Content-Type: application/http Content-Transfer-Encoding: binary
POST TravelagencyCollection HTTP/1.1 Content-Type: application/atom+xml Content-Length: ###
Not able to figure out this.
Thank you,
Regards,
Chetna
Hello john,
Can you please tell me where to write content-type: multipart/mixed;boundary=chnageset etc etc all of that part??
I have view.js file and controller.js.
In controller i am defining a save method, in which i am calling my service. So how to write all that part inside it??
Code snippet:
var oModel = new sap.ui.model.odata.ODataModel("http://***.**.*.***:****/sap/opu/odata/sap/****_SRV/$batch","true","****_****", " ****** ",{
"X-Requested-With": "JSONHttpRequest", "Content-Type": "multipart/mixed;boundary=changeset",
});
for(var i=0;i<rowLen;i++)
{
NewData = {
"ProcessType":"ZDRQ",
"VisitType":"ZDVR",
"VisitDate": visitDateFormat.toString(),
"StartTime": startTimeFormat.toString(),
"EndTime": endTimeFormat.toString(),
//"Activities": $("#act-col7-row0").val(),
"Activities":activity.toString(),
//"ActivityDesc":$("#activDesc-col6-row0").val(),
"ActivityDesc":actvDescArr.toString(),
"DealerNo" : $("#dlrNum-col0-row0").val(),
"UserId": "P00108156"
};
batchChanges.push( oModel.createBatchOperation("/CREATE_DVR", "POST", NewData) );
}
console.log(batchChanges);
oModel.addBatchChangeOperations(batchChanges);
oModel.submitBatch(function(data) {
oModel.refresh();
console.log(data);
}, function(err) {
alert("Error occurred ");
});
Please guide me.
For some very obvious reasons you have redefine 2 DPC_EXT methods:
You do this by going to transaction SEGW. There, select your Web Service project, expand Runtime Artifacts. Right click on *_EX_DPC_EXT and select Go to ABAP Workbench.
Select the method (might have to press EDIT MODE first) and press the button Redefine.
After the selected methods code is displayed, just enter EXIT. and activate.
Do this for both CHANGESET_BEGIN and CHANGESET_END.
After that, I got my batch create operations working.
Hope it helps.
Crisp blog. Thanks John.
BR.
Hi John,
Nice blog john, I too have worked on Batch calls (Create, Update and Delete) Operations through sap hana xsjs services.
Cheers,
Lakshman.
Hello. And what is is the best way to "batch" requests in sap.ui.model.odata.v2.ODataModel ?
Unless you are asking for clarification/correction of some part of the Document, please create a new Discussion marked as a Question. This is especially true when the Document in question is 3 years old and may not have been updated recently.
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. Read the Getting Started documents (link at the top right) including the Rules of Engagement.
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
Hi John,
Thank you so much for your help. It is really helpful.
🙂
Quick question here...
Say I have two servers: gateway server and R/3 server. My GW server calls functions which are located in my R/3 server. Now, if I were to make a POST request of 3 records using batch... how many requests does GW server make to R/3 server?
Hi John,
If i have a Navigation Property from Contact entity to Contact_Status entity,named as ContactStatusDetails, in that case how can i pass the payload using deep inserts?
Hello John,
How to create batch operation with in create entity with out looping in front end, and how to pass all
front end table rows into create entity to store using batch operation with create entity?
Like i have scenario as i need to create a reservation number through BAPI FM, this called in create entity which i cant loop create entity for each and every front end -table( row ). I need only one reservation number for all the table rows.!
Thanks,
Raviteja
Hello John,
I know is a lot of time by now but is there a possibility to add a path in this method?
like for oModel.sPath + "/toComment", oEntry);
Thank you in advance,
Sylvain