Answered by:
Execute in batches

Question
-
Hi,
I am learning data import process through CRM 2013 sdk. I was able to import and create records in CRM(1 by 1 import). I would like to know how can I leverage my code to execute the same in batches. Here is my code snippet.. someone please guide me on leveraging this code. Thanks in advance.
static void Main(string[] args) { var crmService = CRMHelper.GetCRMService(); //local folder path for testing DataTable encounters = GetDataTableForCSV("C:\\temp\\","import.csv"); foreach (DataRow rowItem in encounters.Rows) { Entity incident = new Entity("incident"); incident.Attributes["new_id"] = (string)rowItem["Id"].ToString(); incident.Attributes["new_caption"] = (string)rowItem["Caption"].ToString(); //all the other field transformations goes here Guid customerid = new Guid("{50H80DF3-7654-E433-9456-0029820G6434}"); EntityReference CustomerId = new EntityReference("contact", customerid); incident["customerid"] = CustomerId; //Create incident crmService.Create(incident); } }
- Edited by ReignFan Thursday, November 5, 2015 2:19 PM
Wednesday, November 4, 2015 9:41 PM
Answers
-
Hello,
You should check ExecuteMultiple message. This is what you should look at.
Dynamics CRM MVP
My blog- Marked as answer by ReignFan Thursday, November 5, 2015 6:55 PM
Wednesday, November 4, 2015 11:17 PMModerator -
Hi,
To batch execute you create a executemultiple-request before your foreach, and instead of doing the create-call you add a create-request to the executemultiple-request. When you've run through your file execute the request. You might want to divide it into smaller batches though and maybe thread it to increase performance, but that's not an exact science
Regards
Rickard Norström Developer CRM-Konsulterna
http://www.crmkonsulterna.se
Swedish Dynamics CRM Forum: http://www.crmforum.se
My Blog: http://rickardnorstrom.blogspot.se- Marked as answer by ReignFan Thursday, November 5, 2015 6:55 PM
Thursday, November 5, 2015 7:43 AM
All replies
-
Hello,
You should check ExecuteMultiple message. This is what you should look at.
Dynamics CRM MVP
My blog- Marked as answer by ReignFan Thursday, November 5, 2015 6:55 PM
Wednesday, November 4, 2015 11:17 PMModerator -
Hi,
To batch execute you create a executemultiple-request before your foreach, and instead of doing the create-call you add a create-request to the executemultiple-request. When you've run through your file execute the request. You might want to divide it into smaller batches though and maybe thread it to increase performance, but that's not an exact science
Regards
Rickard Norström Developer CRM-Konsulterna
http://www.crmkonsulterna.se
Swedish Dynamics CRM Forum: http://www.crmforum.se
My Blog: http://rickardnorstrom.blogspot.se- Marked as answer by ReignFan Thursday, November 5, 2015 6:55 PM
Thursday, November 5, 2015 7:43 AM -
Thanks guys. It is very helpful.
And just a generic question. How many records are considered to be large volume of data which needs batch execution?
- Edited by ReignFan Thursday, November 5, 2015 6:54 PM
Thursday, November 5, 2015 6:54 PM -
Execute Multiple request can be used on volumes of as little as 10 records. Just take into consideration the run time limits as follows;
Use ExecuteMultiple to improve performance for bulk data load
Dynamics CRM 2015Applies To: CRM 2015 on-prem, CRM Online
You can use the ExecuteMultipleRequest message to support higher throughput bulk message passing scenarios in Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM Online 2015 Update, particularly in the case of Microsoft Dynamics CRM Online where Internet latency can be the largest limiting factor. 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.
In general, ExecuteMultipleRequest behaves the same as if you executed each message request in the input request collection separately, except with better performance. Use of the CallerId parameter of the service proxy is honored and will apply to the execution of every message in the input request collection. Plug-ins and workflow activities are executed as you would expect for each message processed.
Custom code in the form of plug-ins and custom workflow activities can even execute ExecuteMultipleRequest. However, there are a few key points to keep in mind. An exception thrown by a synchronous registered plug-in is returned in the response collection item Fault parameter. If a plug-in executes within a database transaction, the plug-in executes ExecuteMultipleRequest, and a transaction rollback is initiated, the rollback includes any data changes resulting from requests executed by ExecuteMultipleRequest.
Example
The following sample code demonstrates a single ExecuteMultipleRequest that performs multiple create operations. Run-time execution options called Settings are used to control the request processing and returned results. These run-time options are discussed in the next section.
// 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); }
To view the complete sample, see Sample: Execute multiple requests.
Specify run-time execution options
The Settings parameter of ExecuteMultipleRequest applies to all of the requests in the request collection controlling execution behavior and results returned. Let’s take a look at these options in more detail.
ExecuteMultipleSettings Member Description When true, continue processing the next request in the collection even if a fault has been returned from processing the current request in the collection. When false, do not continue processing the next request.
When true, return responses from each message request processed. When false, do not return responses.
If set to true and a request does not return a response, because that is its design, the ExecuteMultipleResponseItem for that request is set to null.
However, even when false, the Responses collection will not be empty if errors are returned. If errors are returned, there will be one response item in the collection for each processed request that returned a fault and Fault will be set to the actual fault that occurred.
For example, in a request collection that contains six requests where the third and fifth request return faults, the following table indicates what the Responses collection would contain.
Settings Responses Collection Contents ContinueOnError=true, ReturnResponses=true
6 response items: 2 have Fault set to a value.
ContinueOnError=false, ReturnResponses=true
3 response items: 1 has Fault set to a value.
ContinueOnError=true, ReturnResponses=false
2 response items: 2 have Fault set to a value.
ContinueOnError=false, ReturnResponses=false
1 response item: 1 has Fault set to a value.
An RequestIndex parameter in the response item indicates the sequence number, starting at zero, of the request that the response is associated with. In the previous example, the third request has a request index of 2.
Run-time limitations
There are several constraints related to the use of the ExecuteMultipleRequest as described in the following list.
- No recursion is allowed - ExecuteMultipleRequest cannot invoke ExecuteMultipleRequest. An ExecuteMultipleRequest found in the request collection will generate a fault for that request item.
- Maximum batch size – there is a limit to how many requests can be added to a request collection. If that limit is exceeded, a fault is thrown before the first request is ever executed. A limit of 1000 requests is typical though this maximum amount can be set for the Microsoft Dynamics CRM deployment. The deployment setting for this limit is BatchSize.
- Throttling of concurrent calls – for Microsoft Dynamics CRM 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.
Thursday, November 5, 2015 9:34 PM