Today, I have used the latest SDK 5.0.13 DLL to take advantage of "ExecuteMultipleRequest" for one of the bulk create and update scenario. This is really a nice SDK message for dealing with loads of data. It drastically improved the overall performance. This is mainly used for Bulk Creating, Updating and Deleting the records.
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.
@Developers, This is a very helpful message inclusion in the December 2012 Service updates (Rollup 12).
Here are the detailed steps with code snippet:
Create Scenario:
- Create an Entity Collection object to hold multiple entity records.
- Call the ExecuteMultipleRequest as in the below code snippet.
//Class level variable
private static EntityCollection _accountCollection = null;
//Create an instance of an entity collection
_accountCollection = new EntityCollection();
//Account object, You can use a for loop to add entity object to a collection.
for (int i = 0; i < 10; i++)
{
var accountEntity = new Entity { LogicalName = "account" };
accountEntity.Attributes.Add("name", "Test Account");
/************************************************************/
//accountId is a GUID of an account used only on UPDATE
accountEntity.Attributes.Add("accountid", accountId);
/************************************************************/
accountEntity.Attributes.Add("accountnumber", "10010010");
//Add an account entity to an entity collection
_accountCollection.Entities.Add(accountEntity);
}
//Create
ExecuteCreateMultipleRequest(service, _accountCollection, tracer)
//Update : Note an entity collection records to have a GUID
ExecuteUpdateMultipleRequest(service, _accountCollection, tracer)
///
/// Latest SDK Message for Multiple record create
///
///
///
///
private static void ExecuteCreateMultipleRequest(IOrganizationService service, EntityCollection input, ITracingService tracer)
{
// Create an ExecuteMultipleRequest object.
var 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()
};
tracer.Trace("Survey Entity Count" + input.Entities.Count);
// Add a CreateRequest for each entity to the request collection.
foreach (var entity in input.Entities)
{
var createRequest = new CreateRequest { Target = entity };
requestWithResults.Requests.Add(createRequest);
}
// Execute all the requests in the request collection using a single web method call.
var responseWithResults = (ExecuteMultipleResponse)service.Execute(requestWithResults);
}
///
/// Latest SDK Message for Multiple record update
///
///
///
///
private static void ExecuteUpdateMultipleRequest(IOrganizationService service, EntityCollection input, ITracingService tracer)
{
// Create an ExecuteMultipleRequest object.
var 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()
};
tracer.Trace("Survey Entity Count" + input.Entities.Count);
// Add a CreateRequest for each entity to the request collection.
foreach (var entity in input.Entities)
{
var updateRequest = new UpdateRequest { Target = entity };
requestWithResults.Requests.Add(updateRequest);
}
// Execute all the requests in the request collection using a single web method call.
var responseWithResults = (ExecuteMultipleResponse)service.Execute(requestWithResults);
}
The same steps would also work on Update, Delete and any other CRM requet messages.
In order to use the above code, you should download the latest 2011SDK_5.0.13 SDK from the below link and use the microsoft.xrm.sdk.dll available in the SDK bin folder.
You can use them from Custom .NET application and also, from the Plugins. The Plugin's would give you better performace than running it from a custom .net application.
Let me know, if you have any specific scenario's in Multiple records creation/Updation/deletion?
I truly love this feature, this opens up a lot of opportunities in building some custom components in CRM. Just great!!!
Hope this helps,
Chaitanya...