Run multiple requests with one service call using ExecuteMultipleRequest

If you have been developing on for Dynamics CRM for a while, you probably asked yourself this question at least one: why is there not a way to send multiple service requests with a single call to the organization service. It’s finally here. It was introduced with UR12 on CRM 2011 and is of course available for CRM 2013. The request type is ExecuteMultipleRequest. It’s really simple to use. Here are the steps and code snippet below:

  • Create a collection of entity records you need to action on (create or update or delete)
  • Create the ExecuteMultipleRequest object and add the settings:
    • ContinueOnError
    • ReturnResponse
  • Add a request for each entity to the request collection
  • Call the IOrganizationService Execute method and passing the ExecuteMultipleRequest object in parameter
public void ExecutePluginOperation(LocalPluginContext obj)
{
IOrganizationService service = obj.OrganizationService;
IExecutionContext context = obj.PluginExecutionContext;

Incident incident = (context.InputParameters["Target"] as Entity).ToEntity<Incident>();
EntityCollection entitesToCreate = GetEntitiesToCreate(incident);

ExecuteMultipleRequest requestWithResults = new ExecuteMultipleRequest()
{
// Assign settings that define execution behavior
Settings = new ExecuteMultipleSettings()
{
ContinueOnError = true,
ReturnResponses = true
},
// Create an empty organization request collection.
Requests = new OrganizationRequestCollection()
};

// Add a CreateRequest for each entity to the request collection.
foreach (var entity in entitesToCreate.Entities)
{
if (entity.Id == Guid.Empty)
{
CreateRequest createRequest = new CreateRequest { Target = entity };
requestWithResults.Requests.Add(createRequest);
}
else
{
UpdateRequest createRequest = new UpdateRequest { Target = entity };
requestWithResults.Requests.Add(createRequest);
}            }

// Execute all the requests in the request collection using a single web method call.
ExecuteMultipleResponse responseWithResults = (ExecuteMultipleResponse)service.Execute(requestWithResults);
}

Notes:

  • You can only make multiple requests that make changes or create entities of different types (e.g. Contacts, Accounts)
  • You can make multiple requests of different types (create, updates and delete requests in a single call)

It’s a very nice performance optimization in certain scenarios!

Advertisements

One thought on “Run multiple requests with one service call using ExecuteMultipleRequest

  1. Hello, when I use ExecuteMultipleRequest from inside plugin I am getting a SQL TimeOut Error.
    But when I do not use it (and use instead service.create(entity) for example) it executes without errors

    Any comments?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s