Simple.Odata.Client - Batch Processing

1.3k Views Asked by At

I am trying to implement Batch Process for one of our CRM entity.

I have 2 scenarios 1. Add an entity record then update the same entity record (because we can not add inactive record so need second call to make it inactive). 2. get the entity record and unlink the same from another entity.

Sample code is as below:

var record = client.For<entity1>().Filter(p => p.primaryKey == inputParam.entity1.primaryKey).
   Set(new { statecode = 0 }).InsertEntryAsync(false);

 client.For<entity1>().Filter(p => p.primaryKey == record.primaryKey).
   Set(new { statecode = 1 }).UpdateEntryAsync(false);

Also please let me know if there is possibility to retrieve the record and update the record using Odata Batch.

I am using simple.Odata.Client library.

Thanks. Paritosh

1

There are 1 best solutions below

0
On

If you want to use the record in the UpdateEntry, you will have to call InsertEntryAsync with true to signal that you need the returned result before using that in your update request:

var record = await client
   .For<entity1>()
   .Filter(p => p.primaryKey == inputParam.entity1.primaryKey)
   .Set(new { statecode = 0 })
   .InsertEntryAsync(true);

await client
   .For<entity1>()
   .Filter(p => p.primaryKey == record.primaryKey)
   .Set(new { statecode = 1 })
   .UpdateEntryAsync(false);

For OData batch, you will have to make sure the server is supporting batch processing. Then in your code, this is how to do it with Simple.OData.Client pushing a batch of max 100 request per batch:

ODataClientSettings oDataSettings = new ODataClientSettings
{  
  BaseUri = new Uri(ApiUrl)
};
var batch = new ODataBatch(oDataSettings);
var entryCount = 0;
foreach (var item in entityList)
{
  entryCount++;
  batch += async c => await client
    .For<entity1>()
    .Set(item)
    .InsertEntryAsync(false);
  if ((entryCount % 100 == 0) || entryCount == entityList.Count())
  {
    await batch.ExecuteAsync();
    batch = new ODataBatch(oDataSettings);
  }
}