Tuesday, February 5, 2013

Use ExecuteMultipleRequest for performance improvement in bulk data load and other operations


 A new request called ExecuteMultipleRequest has been added to the Update rollup 12 for the Bulk Data Load. Using this request, multiple request can be executed with the single server call. The main advantage of this request is that it will definitely improve the performance since in one round trip to the server, it actually posts more than 1 message to be executed. With reduced round trips for each individual request the performance improvement is exponential.

ExecuteMultipleRequest accepts the collection of different request and executes each request in a order they appear in the collection. This request will optionally returns the response of each request or error occurred while executing the request. Each message request in the input collection is processed in a separate database transaction.

Members of ExecuteMultipleRequest:

1.    Requests: It is a collection of message requests to execute.

2.    Settings: It contains settings that define whether execution should continue if an error occurs executing a request and if responses for each message request processed are to be returned.
Below is the example, where different requests like Create/Update are added to ExecuteMultipleRequest collection and submit it for processing in a single round trip.
                    // Create an ExecuteMultipleRequest object.
                    ExecuteMultipleRequest requestWithResults = new ExecuteMultipleRequest()
                    {
                        // Assign settings that define execution behavior: continue on error, return responses.
                        Settings = new ExecuteMultipleSettings()
                        {
                            ContinueOnError = true,
                            ReturnResponses = true
                        },
                        // Create an empty organization request collection.
                        Requests = new OrganizationRequestCollection()
                    };
                   //create account entity
                   Entity accountEntity=new Entity("account");
                   //set name for the account
                   accountEntity.Attributes["name"] = "New Account "+new DateTime();
                   //create CreateRequest object
                   CreateRequest createRequest = new CreateRequest { Target = accountEntity };
                   //add to ExecuteMultipleRequest Collection
                   requestWithResults.Requests.Add(createRequest);
                   //retrieve contact entity
                   Entity contactEntity = _service.Retrieve("contact", new Guid("FC298C3B-2E4F-     E211-9DED-1CC1DE6DAA3E"),                                      new Microsoft.Xrm.Sdk.Query.ColumnSet());
                   //set Business Phone for the contact
                   contactEntity.Attributes["telephone1"]="1234567";
                   //create UpdateRequest
                   UpdateRequest updateRequest = new UpdateRequest { Target = contactEntity };
                   //add to ExecuteMultipleRequest Collection
                   requestWithResults.Requests.Add(updateRequest);
                   // Execute all the requests in the request collection using a single web method call.
                   ExecuteMultipleResponse responseWithResults = (ExecuteMultipleResponse)_service.Execute(requestWithResults);
                        if (responseWithResults.IsFaulted)
                        {
                            // Display the results returned in the responses.
                            foreach (var responseItem in responseWithResults.Responses)
                            {
                                // A valid response.
                                if (responseItem.Response != null) { }
                               
                                // An error has occurred.
                                if (responseItem.Fault != null) { }
                              
                            }
                        }

Members of ExecuteMultpleResponse:

1.    IsFaulted: It can be used  to identify if any of the messages submitted failed. Once you have identified an error you can loop through the response item to read the error description per message failure.

2.    Responses: Indicates the collection of responses.

Note: This request will only work on organizations that have UR12 applied or on Online organizations that have been upgraded to Polaris.



For further read on this, you can also refer to Microsoft CRM SDK documentation on ExceuteMultiple here.



No comments:

Post a Comment