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"];
Entity entity = service.Retrieve("task", target.Id, new ColumnSet(true));
//Inheritance OBJECT
trace.Trace("check target does have regar dingobjectid proprty?");
EntityReference regardingCaseRef = entity["regardingobjectid"] as EntityReference;
//chi nhan nhung cot bi thay doi
trace.Trace("1");
Entity regardingCase = service.Retrieve(regardingCaseRef.LogicalName, regardingCaseRef.Id, new ColumnSet(true));
trace.Trace("2");
QueryExpression qeRelatedTasks = new QueryExpression("task");
trace.Trace("3");
qeRelatedTasks.Criteria.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Equal, regardingCaseRef.Id));
qeRelatedTasks.ColumnSet = new ColumnSet(true);
trace.Trace("4");
EntityCollection allRelatedTasks = service.RetrieveMultiple(qeRelatedTasks);
trace.Trace("check condition");
if (AreAllRelatedTasksCompleted(allRelatedTasks))
{
trace.Trace("Case title: " + regardingCase["title"]);
regardingCase["description"] = "All Task Completed";
service.Update(regardingCase);
}
//var checkComplete = AreAllRelatedTasksCompleted( relatedTasks) ;
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
public bool AreAllRelatedTasksCompleted(EntityCollection relatedTasks)
{
if (relatedTasks.Entities.All(x => x.GetAttributeValue<OptionSetValue>("cos_csutaskstatus")?.Value == 769190000))
{
//Console.WriteLine("All task completed");
return true;
}
else
{
return false;
}
}
}