This blog explains how to resolve the case programmatically. It can be used
in Plugin or Custom workflow or from any web application that interacts with
CRM.
We need to use CloseIncidentRequest
to close an incident (case) with a case resolution.
Incident cannot be closed if there are any activities with "Open"
status. So we need to programmatically complete all the activities associated
to incident and then close the incident.
First we will see how to complete all the activities associated to case.
Below function accepts IOrganizationService and incident GUID as an parameter
to make the status of all the activities as “Complete” for the incident id
passed.
Note: We need to complete only activities that are Open.
private void CompleteAssociatedActivities(IOrganizationService
service, Guid
incidentId)
{
using (OrganizationServiceContext context = new OrganizationServiceContext(service))
{
try
{
var query = (from a in context.CreateQuery("activitypointer")
where (a.GetAttributeValue<Guid>("regardingobjectid") == incidentId) && a.GetAttributeValue<int>("statecode") != 1
select new { Activity = a }).ToList();
foreach (var v in query)
{
if (v.Activity.Contains("activitytypecode")
&& !Guid.Empty.Equals(v.Activity.Id))
{
SetStateRequest
setStateRequest = new SetStateRequest();
// Completing Interaction activty
setStateRequest.EntityMoniker
= new EntityReference(v.Activity["activitytypecode"].ToString(),
v.Activity.Id);
// Set the State and Status OptionSet Values to completed.
setStateRequest.State = new OptionSetValue(1);
setStateRequest.Status = new OptionSetValue(2);
// Execute the Response
SetStateResponse
setStateResponse = (SetStateResponse)service.Execute(setStateRequest);
}
}
}
catch (Exception ex)
{
throw; // or log the error
}
}
}Next step is to close the incident as we completed all the activities associated to the incident. Below function uses CloseIncidentRequest to close Incident with resolution.
private void ResolveIncident(IOrganizationService service, Guid recordGUID, string entityName)
{
CloseIncidentRequest closeIncidentRequest = new CloseIncidentRequest();
try
{
closeIncidentRequest.IncidentResolution = new Entity("incidentresolution");
closeIncidentRequest.IncidentResolution["subject"] = "Resolved
incident";
closeIncidentRequest.IncidentResolution["incidentid"] = new EntityReference(entityName, recordGUID);
closeIncidentRequest.Status = new OptionSetValue(5);
CompleteAssociatedActivities(service, recordGUID); // Makes all the activities associated to case to complete
before resolving a case
service.Execute(closeIncidentRequest);
}
catch (Exception ex)
{
throw;
}
}
Hope this blogs helps to understand resolving incident by completing all activities.
No comments:
Post a Comment