Bulk Data API ExecuteMultipleRequest in UR12

Bulk Data API ExecuteMultipleRequest in UR12

The ExecuteMultipleRequest message was added to the December 2012 Service Update (UR12) in CRM Online and CRM On-Premise, which accepts a collection of message requests, executes them in the order of the input collection and can return a collection of responses that contain the response for each message that occurred or if an error occurred. The CRM SDK version 5.0.13 is required to use the ExecuteMultipleRequest.

Each message request in the collection is processed in a separate database transaction, and the ExecuteMultipleRequest is executed by using the OrganizationService Execute method.

The ExecuteMultipleRequest message was added to the December 2012 Service Update (UR12) in CRM Online and CRM On-Premise, which accepts a collection of message requests, executes them in the order of the input collection and can return a collection of responses that contain the response for each message that occurred or if an error occurred. The CRM SDK version 5.0.13 is required to use the ExecuteMultipleRequest.

Each message request in the collection is processed in a separate database transaction, and the ExecuteMultipleRequest is executed by using the OrganizationService Execute method.

The ExecuteMultipleRequest behaves in the same way as if each message request in the input request collection was executed separately, except that it is processed with better performance. Plug-ins and workflow messages are executed the same way as if each execute message would be called separately.

The sample code below shows how to use the RetrieveMultiple request to retrieve multiple entity records from the database, modify theses records and update them back to the database using the ExecuteMultipleRequest.

The initial step is to instantiate the ExecuteMultipleRequest class so that any updates can be added to the class

ExecuteMultipleRequest multipleRequest = new ExecuteMultipleRequest()
{
Settings = new ExecuteMultipleSettings()
   {

       ContinueOnError = false,

       ReturnResponses = true

    },

    Requests = new OrganizationRequestCollection()

};


We then need to retrieve the records from the CRM database that we want to make modifications to, and make the updates. When calling the update method we are not sending the request to CRM, but only adding the update request to the ExecuteMultipleRequest requests collection:

// Retrieve records and make modifications to existing records

EntityCollection contacts = CRMHelper.crm.RetrieveContacts();

if (contacts.Entities.Count > 0)

{

   foreach (Entity contact in contacts.Entities)

    {

       Guid contactid = contact.Id;

       string emailAddress = contact.Contains(“emailaddress1”) ?
contact.Attributes[“emailaddress1”].ToString() : “”;

       if (!string.IsNullOrEmpty(emailAddress))

       {

          contact.Attributes[“xrm_profileurl”] = “http://www.contoso.com/register.aspx?eid=” +
             contactid.ToString();

          contact.Attributes[“xrm_unsubscribeurl”] = “http://www.contoso.com/communications.aspx?eid=”
             + contactid.ToString();

          UpdateRequest request = new UpdateRequest

          {

             Target = contact

          };

          // Add each updated record to the Multipe Request requests collection

          multipleRequest.Requests.Add(request);

       }

    }

}


Finally after making the updates to each record, we will call the Execute method on the ExecuteMultipleRequest class to retrieve the responses of all the updates.
We can then loop through the responses to verify that each record update was either successful or failed.

// Execute the multiple requests

ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)CRMHelper.xrm5.Execute(multipleRequest);

// Individual Responses and Faults

foreach (ExecuteMultipleResponseItem responseItem in multipleResponse.Responses)

{

   string fullname = ((Contact)(multipleRequest.Requests[responseItem.RequestIndex])
.Parameters[“Target”]).FullName;

   if (responseItem.Response != null)

      Console.WriteLine(String.Format(“Contact {0} updates with profile information”, fullname));

   else if (responseItem.Fault != null)

      Console.WriteLine(String.Format(“Contact {0} could not update. The following error was
returned: {1}”, fullname, responseItem.Fault.Message));