Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

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

Posted on by Microsoft Employee

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;
}
}


}
}

  • a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: with different regarding task it gonna change the case status to the same regarding task

    No duplicates, please.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    update the case status depend on task regarding status

    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;
            }
  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: with different regarding task it gonna change the case status to the same regarding task

    thansk

  • Suggested answer
    Gowri Halan Profile Picture
    Gowri Halan 10 on at
    RE: with different regarding task it gonna change the case status to the same regarding task

    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
    Community Member Microsoft Employee on at
    RE: with different regarding task it gonna change the case status to the same regarding task

    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

  • Community Member Profile Picture
    Community Member Microsoft Employee 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

    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
    Community Member Microsoft Employee 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

    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;
    }
    }


    }
    }

  • Gowri Halan Profile Picture
    Gowri Halan 10 on at
    RE: with different regarding task it gonna change the case status to the same regarding task

    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
    Community Member Microsoft Employee on at
    RE: with different regarding task it gonna change the case status to the same regarding task

    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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: with different regarding task it gonna change the case status to the same regarding task

    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.

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,564 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,651 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans