Set Case Resolution Subject with Plugin in CRM

Hello everyone,

In this article I will be talking about how to Set Case Resolution Subject with Plugin in Microsoft Dynamics CRM.

Set Case Resolution Subject with Plugin

If you have worked with cases (incidents) in Dynamics CRM, you should have seen a screen like below when you click on Resolve Case button:

resolve case

If you want to set the Resolution field value in this screen you don’t have any way other than developing a plugin. I have researched a lot on the internet and found those articles related to my case:

I followed the introductions here even it applies on Dynamics CRM 2011 and could not set the resolution.

I also examined this article but it was not about my case.

Thanks to the writer of this article, it helped me a lot and I could have a lot information about IncidentResolution entity. And after reading that article I decided to develop a plugin which I am going to explain in this post.

 

Some important things that I’ve learned recently about Incident Resolution:

  1. IncidentResolution is a type of Activity.
  2. You cannot interfere IncidentResolution in workflows.
  3. You can interfere IncidentResolution in plugins.

 

Here is my case;

  • I have a custom entity that holds the Resolution types and I have this entity reference as a lookup in my incident form.
  • I want my plugin to set Resolution with the Text value of my Resolution custom entity’s value.
  • I also want the modifier user to be saved to incident as resolver of the incident (case).
  • Finally I want resolve the case with the Resolution I passed to it.

 

Plugin properties:
  • Entity Name: incident
  • Event Pipeline Stage of Execution: Post Operation
  • Message: Update

 

Plugin class code
// Plugin
public class UpdateCaseResolve: IPlugin
{
	enum incident_statuscode
	{
		InProgress = 1,
		OnHold = 2,
		WaitingForDetails = 3,
		Researching = 4,
		ProblemSolved = 5,
		Canceled = 6
	}

	private static readonly string unexpectedExceptionSuppressionMessage = "An error occurred in the {0} plugin and this error was suppressed to allow the continuation of Incident update";

	public void Execute(IServiceProvider serviceProvider)
	{
		try
		{
			IPluginExecutionContext context = (IPluginExecutionContext)
			serviceProvider.GetService(typeof(IPluginExecutionContext));

			IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof(IOrganizationServiceFactory));
			IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

			// The InputParameters collection contains all the data passed
			// in the message request.
			if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
			{
				using(var localContext = new CrmOrganizationServiceContext(service))
				{
				    // Obtain the target entity from the Input Parameters.
					Entity incident = (Entity) context.InputParameters["Target"];

					if (incident != null)
					{
						// if Resolution is not empty, Resolve Case
						if (incident.Attributes["new_caseproblemsolution"] != null)
						{
							// get current user's guid
							Guid ? currentUserId = IncidentHandler.GetCurrentUserId(localContext);
							Guid currentUsedIdValue = currentUserId != null ? currentUserId.Value : Guid.Empty;

							// set incident's resolveruser to the current user
							incident.Attributes["new_resolveruserid"] = new EntityReference(SystemUser.EntityLogicalName, currentUsedIdValue);

							// add incident to service context, because above code line the service context is filled with systemuser entity
							localContext.AddObject(incident);

							// update incident
							localContext.UpdateObject(incident);

							// get Text of caseproblemsolution
							string caseproblemsolutionText = IncidentHandler.GetCaseProblemSolutionText(localContext, incident.Attributes["new_caseproblemsolution"].Id);

							// Create the incident's resolution.
							var incidentResolution = new IncidentResolution
							{
								Subject = caseproblemsolutionText, // set subject with Text of new_caseproblemsolution of incident
								IncidentId = new EntityReference(Incident.EntityLogicalName, incident.Attributes["incidentid"])
							};

							// Close the incident with the resolution.
							var closeIncidentRequest = new CloseIncidentRequest
							{
								IncidentResolution = incidentResolution,
								Status = new OptionSetValue((int) incident_statuscode.ProblemSolved)
							};

							service.Execute(closeIncidentRequest);
						}
					}
				}
			}
		}
		catch (Exception ex)
		{
			throw new Exception(string.Format(unexpectedExceptionSuppressionMessage, this.GetType().ToString()), ex);
		}
	}
}

 

Helper class code
// helper class
public static class IncidentHandler 
{
	/// <summary>
	/// get caseproblemsolution text via giving its id
	/// </summary>
	/// <param name="context"></param>
	/// <param name="caseproblemsolutionId"></param>
	/// <returns></returns>
	public static string GetCaseProblemSolutionText(OrganizationServiceContext context, Guid caseproblemsolutionId) 
	{
		new_caseproblemsolution tempSolution = null;

		string solutionText = null;

		tempSolution = (from t in context.CreateQuery < new_caseproblemsolution > ()
		where t.new_caseproblemsolutionId.Equals(caseproblemsolutionId)
		select t).FirstOrDefault();

		if (tempSolution != null) 
		{
		    solutionText = tempSolution.new_name;
		}

		return solutionText;
	}

	/// <summary>
	/// get current user id
	/// </summary>
	/// <param name="context"></param>
	/// <returns></returns>
	public static Guid ? GetCurrentUserId(OrganizationServiceContext context) 
	{
		WhoAmIRequest request = new WhoAmIRequest();
		WhoAmIResponse response = (WhoAmIResponse) context.Execute(request);
		if (response != null) 
		{
		    return response.UserId;
		}
		else
		{
		    return null;
		}
	}

}

Hope this helps!

Comments

  • Hi Mehmet,

    Can you provide for me the specific references required for your above plugin? I’m struggling to get it to compile as it’s complaining about references for “systemuser”, “IncidentResolution” and “Incident”. My guess is that it requires and early bound file, but I’m not sure which one it is.

    Thanks
    Jason

    • Hi Jason,

      The references that you mentioned are classes that represents regarding entities in CRM. Once you generate a class that holds the entity references via using CrmSvcUtil.exe you will be able to use the references that you mentioned(Use the generated class in your CRM project). I recommend you to take a look at this msdn page so that you get the idea. I hope I could help.

      Good work!
      Mehmet.

  • 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.

    • Hi,

      Thank you for your comment. This article covers the task you’re working on. You should develop a plugin considering what I have explained in the article in order to do what you need to do.

      Kind Regards,

      Mehmet.

Leave a Reply

Your email address will not be published. Required fields are marked *