web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

with different regarding task it gonna change the case status to the same regarding task

(0) ShareShare
ReportReport
Posted on by

this only work for reject. Any one have any idea if you chose others choice it gonna change the case status to that

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"];
Entity entity = service.Retrieve("task", target.Id, new ColumnSet(true));

//Inheritance OBJECT
trace.Trace("check target does have regardingobjectid 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;
}
}


}
}

I have the same question (0)
  • Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Your requirement is not clear to me.

    Please provide more details on the requirement and the issue you are facing.

  • Community Member Profile Picture
    on at

    in the task i have mul choices like reject, approve,etc and in the case i have csucase task with the same choice with task.

    so  in the code above you can only choose one choices for every case.

    i want to each case have diff choice depent on what the user choose in the csutask.

  • Community Member Profile Picture
    on at

    in the task i have mul choices like reject, approve,etc and in the case i have csucase task with the same choice with task.

    so  in the code above you can only choose one choices for every case.

    i want to each case have diff choice depent on what the user choose in the csutask.

    thanks

  • Gowri Halan Profile Picture
    10 on at

    If I am not wrong your requirement is when an task for a case entity status is updated, then check all the tasks status and if all the tasks are completed, update case entity ?

  • Community Member Profile Picture
    on at

    this only work for reject. Any one have any idea if you chose others choice it gonna change the case status to that

    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"];
    Entity entity = service.Retrieve("task", target.Id, new ColumnSet(true));

    //Inheritance OBJECT
    trace.Trace("check target does have regardingobjectid 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;
    }
    }


    }
    }

  • Community Member Profile Picture
    on at

    this only work for reject. Any one have any idea if you chose others choice it gonna change the case status to that

    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"];
    Entity entity = service.Retrieve("task", target.Id, new ColumnSet(true));

    //Inheritance OBJECT
    trace.Trace("check target does have regardingobjectid 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;
    }
    }


    }
    }

  • Community Member Profile Picture
    on at

    yes but now i want it gonna check all task if all task is complete =>> complete

    reject=>> reject

    this code above can only do reject

  • Suggested answer
    Gowri Halan Profile Picture
    10 on at

    So if I am not wrong below condition check reject condition ? add one more OR condition for completed status it should work.

    if (relatedTasks.Entities.All(x => x.GetAttributeValue<OptionSetValue>("cos_csutaskstatus")?.Value == 769190000))

    But looking at your code there are chances for lots of improvement.

    1. You dont have to query case entity at all

    2. dont add new column(true). only specify required column (new column("statecode","statuscode",etc..)).

  • Community Member Profile Picture
    on at

    thansk

  • Community Member Profile Picture
    on at

    I have this code below. If all the task is reject =>> the case status=>> reject, but i want if i choose other like approve or sth it it gonna do the same with case status 

    like i choose csu task status == complete =>> csu case status== complete 

    it has complete, reject,on hold,pending

        Entity target = (Entity)context.InputParameters["target"];
                    //retrieve
                    Entity entity = service.Retrieve("task", target.Id, new ColumnSet(true));
                    //pointer
                    EntityReference regardingCaseRef = entity["regardingobjectid"] as EntityReference;
                    Entity regardingCase = service.Retrieve(regardingCaseRef.LogicalName, regardingCaseRef.Id, new ColumnSet(true));
                    //retrieve multiple using query expression
                    QueryExpression qeRelatedTasks = new QueryExpression("task");
                    qeRelatedTasks.ColumnSet = new ColumnSet(true);
                    qeRelatedTasks.Criteria.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Equal, regardingCaseRef.Id));
                    EntityCollection relatedTasks = service.RetrieveMultiple(qeRelatedTasks);
    
                    if (AllTaskReject(relatedTasks))
                    {
                        trace.Trace("status check");
                        regardingCase["cos_csucasestatus"] = new OptionSetValue(769190006);
                        trace.Trace("status check 2");
    
                        service.Update(regardingCase);
                        trace.Trace("status check 3");
                        /* Entity incident = new Entity("incident");
                     incident.Id = regardingCaseRef.Id;
                     trace.Trace("1");
                     incident["cos_csucasestatus"] = new OptionSetValue(769190006);
                     service.Update(incident);*/
                        // regardingCase.Attributesk["cos_csucasestatus"] = new OptionSetValue(769190006);
                    }
    
                }
                catch
                {
                    throw;
                }
    
    
    
            }
    
    
    
        }
        public bool AllTaskReject(EntityCollection relatedTasks)
        {
            //check all the the task if rejected continue if on task not rejected do nothing
    
            if (relatedTasks.Entities.All(x => x.GetAttributeValue<OptionSetValue>("cos_csutaskstatus")?.Value == 769190003))
            {
    
                return true;
            }
            else
            {
                return false;
            }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans