Featured Post

Web API Requests Series

Web API Series:  Create and Retrieve , Update and Delete , Retrieve Multiple , Associate/Disassociate , Impersonation , Run Workflows

25 February 2015

How to use the ExecuteMultipleRequest to resolve cases

This post is about how to use the "ExecuteMultipleRequest" message to resolve cases in Bulk.
The main advantage is that if you have to update thousands of record you can do that by sending one single server request and not thousands of requests as it happens with the single Request.

This particular example is for changing the state and status of cases. To achieve that you have to use the "CloseIncidentRequest" message.
The "ExecuteMultipleRequest" message allows us to create a collection of  "CloseIncidentRequest" requests and send it to the server to be processed.

Let's start off by creating a "ExecuteMultipleRequest" object:

var myMultipleRequest = new ExecuteMultipleRequest()
                Settings = new ExecuteMultipleSettings()
                    ContinueOnError = true,
                    ReturnResponses = false
                Requests = new OrganizationRequestCollection()

And then we can loop through our list of cases to create a "CloseIncidentRequest" for each case.
I used a helper method to build the CloseIncidentRequest called "BuildIncidentResolutionRequest"
which takes the incident id and the status reason as input:

foreach (var incident in cases.Entities)
var closeIncidentRequest = BuildIncidentResolutionRequest(incident.Id, caseStatusReason);

This method first creates a new Entity (which is an activity of type Case Resolution) called "incidentresolution" which will be used in the "CloseIncidentRequest" this entity is used to resolve the incident and a subject and description can be provided.
Note: If you try to resolve a case using the UI you must enter a resolution (which is the subject) that information will be held in the entity called "incidentresolution" which can be viewed in CRM with an advanced find or in the closed activities linked to the case. An "incidentresolution" record will be created for each case you are resolving.

It then creates the "CloseIncidentRequest".

private CloseIncidentRequest BuildIncidentResolutionRequest(Guid incidentId, int caseStatusReason)
            // Create the incident's resolution.
            var incidentResolution = new Entity("incidentresolution");

            incidentResolution["subject"] = "Case was Resolved";
            incidentResolution["incidentid"] = new EntityReference("incident", incidentId);
            incidentResolution["description"] = "Description";

            // Close the incident with the resolution.
            var closeIncidentRequest = new CloseIncidentRequest
                IncidentResolution = incidentResolution,
                Status = new OptionSetValue(caseStatusReason)

            return closeIncidentRequest;

...and finally the list of requests is sent to the server to be processed.

var responses = (ExecuteMultipleResponse)orgService.Execute(myMultipleRequest);

We can also assert if any error occurred by checking the "IsFaulted" flag.
Anyway for more information refer to the msdn this page.

if (responses.IsFaulted)
         Console.WriteLine("Error: {0}", myMultipleRequest.Responses[0].Fault.Message);