Sometimes we got requirements which include more than one request in custom code. And we generally used service.Create and service.Update for those actions. But then we start to have impact on plugin performance if plugin is attempting to create, update or delete a number of records. By using ExecuteMultipleRequest we can execute all requests in batches which improve plugin performance.
ExecuteMultipleRequest accepts an input collection of message Requests, executes each of the message requests in the order they appear in the input collection, and optionally returns a collection of Responses containing each message’s response or the error that occurred. Each message request in the input collection is processed in a separate database transaction.
ExecuteMultipleRequest is executed by using the IOrganizationService.Execute method.
Limitations
- Avoid recursion otherwise will get error for that request.
- Maximum batch size is 1000 so try to keep number of requests is 1000 or less if that limit is exceeded, a fault is thrown before the first request is ever executed.
- For Microsoft Dynamics 365 (online) there is a limit of 2 concurrent ExecuteMultipleRequest executions per organization . If that limit is exceeded, a “Server Busy” fault is thrown before the first request is ever executed. For an on-premises deployment, throttling is not enabled by default. The deployment setting for this limit is ExecuteAsyncPerOrgMaxConnectionsPerServer.
// Get a reference to the organization
service.
using (_serviceProxy = new
OrganizationServiceProxy(serverConfig.OrganizationUri,
serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials))
{
// Enable early-bound type support to
add/update entity records required for this sample.
_serviceProxy.EnableProxyTypes();
#region Execute Multiple with Results
// Create an ExecuteMultipleRequest object.
requestWithResults = 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()
};
// Create several (local, in memory) entities
in a collection.
EntityCollection
input = GetCollectionOfEntitiesToCreate();
// Add a CreateRequest for each entity to the
request collection.
foreach (var entity in input.Entities)
{
CreateRequest createRequest = new CreateRequest {
Target = entity };
requestWithResults.Requests.Add(createRequest);
}
// Execute all the requests in the request
collection using a single web method call.
ExecuteMultipleResponse responseWithResults =
(ExecuteMultipleResponse)_serviceProxy.Execute(requestWithResults);
// Display the results returned in the
responses.
foreach (var responseItem in
responseWithResults.Responses)
{
// A valid response.
if (responseItem.Response != null)
DisplayResponse(requestWithResults.Requests[responseItem.RequestIndex],
responseItem.Response);
// An error has occurred.
else if (responseItem.Fault != null)
DisplayFault(requestWithResults.Requests[responseItem.RequestIndex],
responseItem.RequestIndex, responseItem.Fault);
}
|
You can follow below link to get more information.
https://msdn.microsoft.com/en-us/library/jj863631.aspx#limitations