Plugin when a case (incident) is resolved in CRM 2013.

Hi,

To fire a plugin when an incident is resolved we need to register the plugin in

Close message for the incident entity.

And in the input parameters of the context we need to check for IncidentResolution.

This entity is a special entity that holds the information about the incident record that is being closed.

The sample plugin below gets the actual end attribute value of IncidentResolution and subtracts created on attribute value of Incident to get the duration and updates the duration (custom field) in the case being resolved.


public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));

if (context.InputParameters.Contains("IncidentResolution"))
{
Entity incidentResolution = (Entity)context.InputParameters["IncidentResolution"];
Guid relatedIncidentGuid = ((EntityReference)incidentResolution.Attributes["incidentid"]).Id;
DateTime actualEnd = DateTime.Now;
if (incidentResolution.Contains("actualend"))
{
actualEnd = (DateTime)incidentResolution.Attributes["actualend"];
}
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

// Get created on of incident

Entity retrievedIncident = service.Retrieve("incident", relatedIncidentGuid, new ColumnSet(true));
DateTime createdOn = (DateTime)retrievedIncident.Attributes["createdon"];

TimeSpan totalTimeSpent = actualEnd.Subtract(createdOn);

Entity incident = new Entity("incident");
incident.Id = relatedIncidentGuid;
incident.Attributes["new_duration"] = totalTimeSpent.TotalMinutes;
service.Update(incident);

}
}

The helpful post

http://varghesedanny.com/2011/02/24/plug-in-when-case-incident-is-closed/

 

 

 

 

 

 

 


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

6 thoughts on “Plugin when a case (incident) is resolved in CRM 2013.”

  1. Hi Buddy,

    Nicely Done.
    I am learning CRM and the next task I m trying is to update a custom entity Resolution field with Case Resolution whenever the case is resolved.
    Can you please help me with the same.

    Like

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s