Sunday 14 April 2013

CRM 2011 Plugin to Update Case (Incident) Before Close

Sometimes it is necessary to update a property on a case just before it is closed in order to capture some important information, such as whether an SLA was met, or to set a resolved date for reporting purposes (if you don’t fancy grappling with case resolution records). Thankfully CRM 2011 provides a message to subscribe to implement this functionality. The “Close” message of “incident” is the message to take advantage of this, and running at “Pre Validation” stage of execution will ensure your changes get applied to the case before it is closed by CRM and no longer updateable. Here is a screenshot illustrating the appropriate message and step to subscribe to:
 
The one thing that you may not expect is the entity you will actually get passed within your plugin will not actually be incident (case) but will actually by Incident Resolution:
IPluginExecutionContext context = localContext.PluginExecutionContext;
Entity incidentResolution = (Entity)context.InputParameters["IncidentResolution"];
 
From this you can get the case to update using a standard retrieve, or your favourite method..:
                    Entity incident = localContext.OrganizationService.Retrieve("incident", ((EntityReference)incidentResolution["incidentid"]).Id, colSet);
 
You could also do things like check the properties of the case if you wanted to do some conditional updates, or go off and set something on the account record (if you couldn’t do it in a workflow of course) and so on and so on…
Now update the case, call an Org. Service update (pass in your case entity) and your case will be updated. Happy days.
Cheers,
Nick