locked
Close all open task RRS feed

  • Question

  • I am trying to close all open tasks on the case (when the case satisfies a particular condition)

                            foreach (Task t in crm.TaskSet.Where(c => c.RegardingObjectId.Id == incidentid &&  c.StateCode !=1))
                            {
                             
                                t.StateCode= 1; --- getting compile error, I cant set this to 1 as it is readonly
                                crm.UpdateObject(t);
                                crm.SaveChanges();

                            }

    How do i specify the task object 't'  has to be closed..?
    Thursday, April 25, 2013 2:09 PM

Answers

  • You need to use a SetStateRequest to close the task as completed, try with this code:

    // better to filter the open task (statecode 0) because there are also cancelled to consider (statecode 2)
    foreach (Task t in crm.TaskSet.Where(c => c.RegardingObjectId.Id == incidentid &&  c.StateCode == 0)) 
    {
        SetStateRequest state= new SetStateRequest();
        state.State = new OptionSetValue(1); // statecode 1 equals completed
        state.Status = new OptionSetValue(5); // statuscode 5 equals completed
        state.EntityMoniker = t.ToEntityReference();
        SetStateResponse stateSet = (SetStateResponse)crm.Execute(state);
    }



    My blog: www.crmanswers.net

    • Proposed as answer by Guido PreiteMVP Thursday, April 25, 2013 2:37 PM
    • Marked as answer by Shiju H Friday, April 26, 2013 10:01 PM
    Thursday, April 25, 2013 2:35 PM

All replies

  • To change the status and status reason use this:

    crm.Execute(new SetStateRequest
                    {
                    EntityMoniker = new EntityReference(A.LogicalName, A.Id),
                    State = new OptionSetValue(1),
                    Status = new OptionSetValue(2)
                    });



    My Weblog | My Website

    Thursday, April 25, 2013 2:30 PM
    Moderator
  • Hi,

    You need to make a call to SetState:

    SetStateRequest request = new SetStateRequest();
    request.EntityMoniker = new EntityReference("task", t.activityid);
    request.State = new OptionSetValue(1);
    request.Status = new OptionSetValue((5);
    service.Execute(request);

    hth


    Scott Durow
    Read my blog: www.develop1.net/public     Follow Me on Twitter
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Thursday, April 25, 2013 2:31 PM
    Answerer
  • You need to use a SetStateRequest to close the task as completed, try with this code:

    // better to filter the open task (statecode 0) because there are also cancelled to consider (statecode 2)
    foreach (Task t in crm.TaskSet.Where(c => c.RegardingObjectId.Id == incidentid &&  c.StateCode == 0)) 
    {
        SetStateRequest state= new SetStateRequest();
        state.State = new OptionSetValue(1); // statecode 1 equals completed
        state.Status = new OptionSetValue(5); // statuscode 5 equals completed
        state.EntityMoniker = t.ToEntityReference();
        SetStateResponse stateSet = (SetStateResponse)crm.Execute(state);
    }



    My blog: www.crmanswers.net

    • Proposed as answer by Guido PreiteMVP Thursday, April 25, 2013 2:37 PM
    • Marked as answer by Shiju H Friday, April 26, 2013 10:01 PM
    Thursday, April 25, 2013 2:35 PM
  • is there any other way than using microsoft.crm.sdk.proxy.dll  ? I don't have it in my plugin yet.

    Thursday, April 25, 2013 2:44 PM