using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using System.Security.Principal;
using Microsoft.Xrm.Sdk.Query;
namespace pluginproject
{
public class TaskCheck : IPlugin
{
//logic call name regarding:regardingobjectid
//csu tasok name cos_csutaskstatus
//if task of a case is completed
//check all the task of the same case
// if all other task are completed too update the descriptions field of the case to all task completed
// Update the Assembly first
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
trace.Trace("optain the tracking service");
try
{
trace.Trace("Inside try");
Entity target = (Entity)context.InputParameters["target"];
//Inheritance OBJECT
trace.Trace("regarding");
EntityReference regardingCaseRef = target["regardingobjectid"] as EntityReference;
Entity regardingCase = service.Retrieve(regardingCaseRef.LogicalName, regardingCaseRef.Id, new ColumnSet(true));
trace.Trace("incident");
QueryExpression qeRelatedTasks = new QueryExpression("incident");
qeRelatedTasks.Criteria.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Equal, regardingCaseRef));
EntityCollection allRelatedTasks = service.RetrieveMultiple(qeRelatedTasks);
trace.Trace("bool");
if (AreAllRelatedTasksCompleted(allRelatedTasks))
{
trace.Trace("Case title: " + regardingCase["title"]);
// update case description = all tasks completed
Entity incident = new Entity("incident")
{
Id = regardingCaseRef.Id
};
incident["description"] = "All Task Completed";
service.Update(incident);
}
//var checkComplete = AreAllRelatedTasksCompleted( allRelatedTasks) ;
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
public bool AreAllRelatedTasksCompleted(EntityCollection relatedTasks)
{
if (relatedTasks.Entities.All(x => ((OptionSetValue)x["cos_csutaskstatus"]).Value == 769190000))
{
//Console.WriteLine("All task completed");
return true;
}
else
{
return false;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using System.Security.Principal;
using Microsoft.Xrm.Sdk.Query;
namespace pluginproject
{
public class TaskCheck : IPlugin
{
//logic call name regarding:regardingobjectid
//csu tasok name cos_csutaskstatus
//if task of a case is completed
//check all the task of the same case
// if all other task are completed too update the descriptions field of the case to all task completed
// Update the Assembly first
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
trace.Trace("optain the tracking service");
try
{
trace.Trace("Inside try");
Entity target = (Entity)context.InputParameters["target"];
//Inheritance OBJECT
trace.Trace("regarding");
EntityReference regardingCaseRef = target["regardingobjectid"] as EntityReference;
Entity regardingCase = service.Retrieve(regardingCaseRef.LogicalName, regardingCaseRef.Id, new ColumnSet(true));
trace.Trace("incident");
QueryExpression qeRelatedTasks = new QueryExpression("incident");
qeRelatedTasks.Criteria.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Equal, regardingCaseRef));
EntityCollection allRelatedTasks = service.RetrieveMultiple(qeRelatedTasks);
trace.Trace("bool");
if (AreAllRelatedTasksCompleted(allRelatedTasks))
{
trace.Trace("Case title: " + regardingCase["title"]);
// update case description = all tasks completed
Entity incident = new Entity("incident")
{
Id = regardingCaseRef.Id
};
incident["description"] = "All Task Completed";
service.Update(incident);
}
//var checkComplete = AreAllRelatedTasksCompleted( allRelatedTasks) ;
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
public bool AreAllRelatedTasksCompleted(EntityCollection relatedTasks)
{
if (relatedTasks.Entities.All(x => ((OptionSetValue)x["cos_csutaskstatus"]).Value == 769190000))
{
//Console.WriteLine("All task completed");
return true;
}
else
{
return false;
}
}
}
}
}