SAPUI5 create multiple value(batch) using post

2019-08-01 08:10发布

问题:

To post data in the SAP Back-end I use:

oModel.create("/Dummyset", oEntry); //so far it works fine

Now I have multiple lines in my oEntry and it doesn't work. I found the following solution

aBatchOperation.push(contactBatchOperation);        
oModel.addBatchChangeOperations(aBatchOperation);
oModel.submitBatch(fSuccess,fError,true);

But unfortunately it's not working with my OData version 2.0 . I always get an error as

"addBatchChangeOperations is not a function"

Then I tried to find out which function I can use with OData v2. I could find this.

batchChanges.push(oModel._createBatchRequest("/AttributesSet", "POST", wert.Atrributes[i].name));
oModel._submitBatchRequest(oModel.setProperty("/AttributesSet", batchChanges), true);

But it still does not work. How do I fix it ?

回答1:

I suppose you want to bundle several create requests into one batch, right?

For ODataModel create method, you can define additional groupId. See below.

mParameters.groupId? ID of a request group; requests belonging to the same group will be bundled in one batch request

Basically you can submit multiple create with the same groupId which you can define yourself.

First you have to set a certain deferredGroups for ODataModel

var aDeferredGroup = oModel.getDeferredGroups().push("batchCreate");
oModel.setDeferredGroups(aDeferredGroup);

Then you call multiple create.

var mParameters = {groupId:"batchCreate"};
oModel.create("/Dummyset", oEntry1, mParameters);
oModel.create("/Dummyset", oEntry2, mParameters);
oModel.create("/Dummyset", oEntry3, mParameters);

At last, you can call submitChanges with one single batch for multiple requests.

oModel.submitChanges(mParameters);