Thursday, July 3, 2014

Using ExecuteMultipleRequest to do bulk operations

Today i came across a nice feature provided in SDK which allows me to do bulk operations in a single call to the server.

For example if i have to create 1000 records within a plugin

The only way was to loop the collection of records and use Service.Create(entity);
This will create 1000 calls to the CRM and is time consuming.

Using ExecuteMultipleRequest you can perform this operation by only sending 1 request to the CRM.
Let see how this can be accomplished

Note: ExecuteMultipleRequest is available in CRM sdk update rollup 12 onwards

In the below example i am deleting 1000 records


ExecuteMultipleRequest BulkDeleteRequest = new ExecuteMultipleRequest()
                    // Assign settings that define execution behavior: continue on error, return responses. 
                    Settings = new ExecuteMultipleSettings()
                        ContinueOnError = false,
                        ReturnResponses = true
                    // Create an empty organization request collection.
                    Requests = new OrganizationRequestCollection()
 foreach (var item in recordstoDelete.Entities)
                    DeleteRequest delRequest = new DeleteRequest() { Target = new EntityReference(item.LogicalName, item.Id) };
                ExecuteMultipleResponse BulkDeleteResponse = (ExecuteMultipleResponse)service.Execute(BulkDeleteRequest);

Run-time limitations

  1. Maximum batch size –1000
  2. Throttling of concurrent calls – for Microsoft Dynamics CRM Online there is a limit of 2 concurrent ExecuteMultipleRequest executions per organization
In one of the blog post from Sonoma partner they claim that the performance is increased by 600%
Here is the link