Thursday, May 30, 2013

MS CRM 2011 ExecuteMultipleRequest SDK Message

When working with Microsoft Dynamics CRM 2011 SDK, more often similar request need to be made to CRM each operation. Thus during the Microsoft Dynamics CRM 2011 Update Rollup 12 a new message request has been added ExecuteMultipleRequestwhich accepts several request to execute them once.

ExecuteMultipleRequest will behave similar as if each was executed separately but this will improve the performance drastically. Both Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online support this batching of requests into a single web service method call.

Below is sample scenario were Multiple Contact need to be created in CRM:

#region Execute Multiple with Results
    // Create an ExecuteMultipleRequest object.
    requestMultiple = 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()
    };
  
for (int i = 0;i < 1000; i++){
    CreateRequest conactReq = new CreateRequest();
    
Contact ct = new Contact {
LastName = "Contact",
FirstName= "Name" + i;
};
conactReq.Target = ct;
// Add Each request to ExecuteMultipleRequest
requestMultiple.Requests.Add(ctRequest);

}

//Finally Execute the ExecuteMultipleRequest using ExecuteMethod
ExecuteMultipleResponse emResults = (ExecuteMultipleResponse)_orgService.Execute(requestMultiple);
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

The Execute Multiple settings allow us to configure if we require responses and how to proceed when an error occurs. In Settings option ContinueOnError to true the final request is processed even if few records fail. In setting option ReturnResponses to true returns a collection of responses for each message’s response.


Hope this is helpful!

No comments:

Post a Comment