RSS

ExecuteMultipleRequest For Bulk Operation

07 Aug

Introduction

In the Microsoft Dynamics CRM 2011 Update Rollup 12, a new message request named ExecuteMultipleRequest is added. Let’s explore more about the usage.

What it does?

It accepts Collection of Requests as input and returns Collection of Responses. The request is executed batch by batch.

For Online CRM, maximum Batch Size is 1000. So, for Online CRM, maximum 1000 requests can be executed at a time.

For On Premise, Batch Size can be increased.

Example

Below are the examples of the ExecuteMultipleRequest.

  • Bulk Lead Creation

    //The method takes IOrganization service and total number of records to be created as input
     private void CreateLeadMultiple(IOrganizationService service, int batchSize)
     {
         //To execute the request we have to add the Microsoft.Xrm.Sdk of the latest SDK as reference
         ExecuteMultipleRequest req = new ExecuteMultipleRequest();
         req.Requests = new OrganizationRequestCollection();
         req.Settings = new ExecuteMultipleSettings();
         req.Settings.ContinueOnError = true;
         req.Settings.ReturnResponses = true;
    
         try
         {
             for (int i = 0; i < batchSize; i++)
             {
                 Entity lead = new Entity("lead");
                 lead["subject"] = "Test Lead";
                 lead["firstname"] = "Requested Lead";
                 lead["lastname"] = "Name " + i;
                 var createRequest = new CreateRequest();
    
                 createRequest.Target = lead;
                 req.Requests.Add(createRequest);
              }
    
              var res = service.Execute(req) as ExecuteMultipleResponse;  //Execute the collection of requests
          }
    
          //If the BatchSize exceeds 1000 fault will be thrown.In the catch block divide the records into batchable records and create
          catch (FaultException<OrganizationServiceFault> fault)
          {
              if (fault.Detail.ErrorDetails.Contains("MaxBatchSize"))
              {
                  var allowedBatchSize = Convert.ToInt32(fault.Detail.ErrorDetails["MaxBatchSize"]);
                  int remainingCreates = batchSize;
    
                  while (remainingCreates > 0)
                  {
                      var recordsToCreate = Math.Min(remainingCreates, allowedBatchSize);
                      CreateLeadMultiple(service, recordsToCreate);
                      remainingCreates -= recordsToCreate;
                  }
              }
          }
    }
    
  • Bulk Entity State and Status Change

    Here is another example of changing the State and Status of records by ExecuteMultipleRequest.

    This code will work, if the total Records <=1000, otherwise we have to break the records into batches to execute one by one.

    for (int i = count; i < ent.Entities.Count; i++)
    {
        SetStateRequest changeState = new SetStateRequest();
        changeState.EntityMoniker = new EntityReference();
        changeState.EntityMoniker.Id = ent.Entities[i].Id;
        changeState.EntityMoniker.LogicalName = ent.Entities[i].LogicalName;
        changeState.State = new OptionSetValue(0);
        changeState.Status = new OptionSetValue(1);
        req.Requests.Add(changeState);
    }
    
    // Here req is the object of ExecuteMultipleRequest class.
    var res = service.Execute(req) as ExecuteMultipleResponse;
    

By Ranjan Parhi
Senior Software Engineer @Team DynamicsCRM.
Mindfire Solutions

 
1 Comment

Posted by on August 7, 2013 in SDK Messages

 

Tags: , , , , , , , , , , , , , , , ,

One response to “ExecuteMultipleRequest For Bulk Operation

  1. Mayank Bhardwaj

    February 20, 2014 at 5:34 am

    How do i use this in Silverlight. Cant seem to find a way. Could you help?

     

Leave a reply to Mayank Bhardwaj Cancel reply