cancel
Showing results for 
Search instead for 
Did you mean: 

How to add/set multiple entities to parent entity? And Deep Insert alternative in Offline

Former Member
0 Kudos

Hi

What about multiple entities (e.g. Order x Items)? How can I add/set items to the parent entity?

As you stated, Deep Insert is not supported in Offline Mode. In this blog:

It says that this must be done with a batch call. Do you have any examples?

Could you help us? We are developing an Android Offline Application.

Thank you!

Best regards,

Rodrigo

Message was edited by: Michael Appleby

Accepted Solutions (0)

Answers (1)

Answers (1)

midhun_vp
Active Contributor
0 Kudos

Hi Rodrigo,

Batch requests allow you to group multiple Odata requests into single HTTP request.

GET, POST, PUT, DELETE requests can be added to a batch request. You have to manage it from the device code.

Ex.

private static final String ODATA_BATCH_VERB = "$batch";

...

//Create the Batch Request

BatchRequest mBatchRequest = new BatchRequest(appEndPoint + ODATA_BATCH_VERB);

mBatchRequest.setListener(this);

mBatchRequest.setPriority(IRequest.PRIORITY_HIGH);

//Create the Batch Request

mBatchRequest.addRetrieveRequestToBatch(getRequest);

mBatchRequest.addRequestToChangeset(deleteRequest);

mBatchRequest.addRequestToChangeset(createRequest);

mBatchRequest.addRequestToChangeset(updateRequest);

//Close change Requests

mBatchRequest.closeExistingChangeSet();

//Execute the batch request

requestManager().makeRequest(mBatchRequest);

Regards,

Midhun VP

0 Kudos

Hi Midhun VP,

In our scenario, 1 Header has many Line-Items. we have to post the header and line-items record in single request.

Like GenericList in SUP 2.X. For this scenario, Batch process is applicable? or how to solve this.

Regards,

Dinesh

Former Member
0 Kudos

Hi ,

Thanks for your answer.

I'm wondering how can I correlate parent and child entities in a batch request.

Keep in mind that the entities does not exist in the backend yet.

Best regards,

Rodrigo

claudiapacheco
Product and Topic Expert
Product and Topic Expert
0 Kudos

Thanks Midhun VP, for your code snippet. Please keep in mind that this code does not use the offline store class. The SDK SP05 provides support for backward compatibility, so this code should work.

Hi ,

If you want to use the offline store SDK SP05+, a colleague, http://scn.sap.com/people/adam.hurst2 from SAP, pointed me to the following solution using the offline store.

      // Create changeSet

      ODataRequestChangeSet changeSetItem = new ODataRequestChangeSetDefaultImpl();

     // Create the parent Entity

      ODataEntity parentEntity = new ODataEntityDefaultImpl("something.Order");

      parentEntity.getProperties().put("orderid", new ODataPropertyDefaultImpl("orderid", "1234"));

      // Create parent item request

      ODataRequestParamSingle req1 = new ODataRequestParamSingleDefaultImpl();

      // Allocate parent Entity

      req1.setResourcePath("Order");

      req1.setMode(Mode.Create);

      req1.setPayload(parentEntity);

      //The contentID acts as a placeholder for address of the created entity. 

      //You can use it to address the new entity in subsequent requests in the same change set.

      req1.setContentID("ID1");

      //Note: headers may not be required... test with and without it in case of issues

      Map<String, String> createHeaders = new HashMap<String, String>();

      createHeaders.put("accept", "application/atom+xml");

      createHeaders.put("content-type", "application/atom+xml");

      req1.setOptions(createHeaders);

      // Add request 1 to changeset

      changeSetItem.add(req1);

      // add the child item to the same changeset

      ODataEntity childEntity = new ODataEntityDefaultImpl("something.OrderItems");

      parentEntity.getProperties().put("orderitemid", new ODataPropertyDefaultImpl("orderitemid", "4321"));

      // Create child item request

      ODataRequestParamSingle req2 = new ODataRequestParamSingleDefaultImpl();

      // Allocate child Entity

      req2.setResourcePath("$ID1/OrderItems");

      req2.setMode(Mode.Create);

      req2.setPayload(childEntity);

      req2.setOptions(createHeaders);

      // Add request 2 to changeset

      changeSetItem.add(req2);

    

      // Add changeset to batch request

      ODataRequestParamBatch requestParamBatch = new ODataRequestParamBatchDefaultImpl();

      requestParamBatch.add(changeSetItem);

      // Send batch request through the offline store

       store.executeRequest(requestParamBatch);

For more information about the offline store visit

Best regards,

Claudia