How to close cases in crm 2011 programmatically

2019-06-09 01:45发布

问题:

I am doing some update in crm using ssis. I tried to close some cases in crm based on certain conditions. This is my sample code in public override void Input0_ProcessInputRow(Input0Buffer Row) method.

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    // Create a Entity object of type 'case'
    Entity caseEnt = new Entity("incident");


    Entity incidentResolution= new Entity("incidentresolution");
    incidentResolution.Attributes.Add("incidentid", new 
        EntityReference("incident", Row.DEVCaseGUID));


    caseEnt["incidentid"] = Row.DEVCaseGUID;


    //organizationservice.Update(caseEnt);

    //Changes added here by //
  EntityCollection  collection= GetAssociatedActivities(new EntityReference("incident", Row.DEVCaseGUID))

      foreach (Entity activity in collection.Entities)
    {
        CancelActivity(activity, organizationservice);
    }
    // Changes added here //

    // Close the incident with the resolution.
    var closeIncidentRequest = new CloseIncidentRequest
    {
        IncidentResolution = incidentResolution,
        Status = new OptionSetValue(5)
    };

    organizationservice.Execute(closeIncidentRequest);

}

private EntityCollection GetAssociatedActivities(EntityReference regarding)
{
    QueryExpression query = new QueryExpression { EntityName = "activitypointer", ColumnSet = new ColumnSet(new string[] { "activitytypecode" }) };
    query.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, regarding.Id);
    query.Criteria.AddCondition("statecode", ConditionOperator.NotEqual, 1);  //ignore completed
    EntityCollection collection = organizationservice.RetrieveMultiple(query);
   return collection

}

// Cancel an Activity
private static void CancelActivity(Entity entity, IOrganizationService service)
{
    EntityReference moniker = new EntityReference();
    if (entity.LogicalName == "activitypointer")
    {
        if (entity.Attributes.Contains("activityid") & entity.Attributes.Contains("activitytypecode"))
        {
            moniker.LogicalName = entity.Attributes["activitytypecode"].ToString();
            moniker.Id = (Guid)entity.Attributes["activityid"];
            SetStateRequest request = new SetStateRequest();
            request.EntityMoniker = moniker;
            request.State = new OptionSetValue(2);
            request.Status = new OptionSetValue(-1);
            SetStateResponse response = (SetStateResponse)service.Execute(request);
        }
    }
}

Row.DEVCaseGUID is the GUID of the Case. statuscode is 5 for closed. statecode is 2 for Resolved.

I tried follow this example but no success. Or is there any simple way to achieve this ?

回答1:

Closing a Case in CRM is different than setting state/statuscode. An Intermediate entity named IncidentResoultion is created when a case is closed. You can try the following code to close the case programmatically.

Entity incidentResolution= new Entity("incidentresolution");
incidentResolution.Attributes.Add("incidentid", new 
            EntityReference("incident", Row.DEVCaseGUID)); 

        // Close the incident with the resolution.
        var closeIncidentRequest = new CloseIncidentRequest
        {
            IncidentResolution = incidentResolution,
            Status = new OptionSetValue(5)
        };

        organizationservice.Execute(closeIncidentRequest);

Please note that a case can only be marked as Closed/Completed only if all the activititesregarding that Case are completed.

Update 09-Nov-2017: Adding code for closing related activities regarding CASE

private List<Entity> GetAssociatedActivities(EntityReference regarding)
    {
        QueryExpression query = new QueryExpression { EntityName = "activitypointer", ColumnSet = new ColumnSet(new string[] { "activitytypecode" }) };
        query.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, regarding.Id);
        query.Criteria.AddCondition("statecode", ConditionOperator.NotEqual, 1);  //ignore completed
        EntityCollection activities = organizationservice.RetrieveMultiple(query);//change collection to activities
        foreach (Entity activity in activities.Entities)
        {
          CancelActivity(activity, organizationservice);
        }
    }

   // Cancel an Activity
  private static void CancelActivity(Entity entity, IOrganizationService service)
  {
      EntityReference moniker = new EntityReference();
      if (entity.LogicalName == "activitypointer")
      {
          if (entity.Attributes.Contains("activityid") & entity.Attributes.Contains("activitytypecode"))
          {
              moniker.LogicalName = entity.Attributes["activitytypecode"].ToString();
              moniker.Id = (Guid)entity.Attributes["activityid"];
              SetStateRequest request = new SetStateRequest();
              request.EntityMoniker = moniker;
              request.State = new OptionSetValue(2);
              request.Status = new OptionSetValue(-1);
              SetStateResponse response = (SetStateResponse)service.Execute(request);
          }
      }
  }

https://www.magnetismsolutions.com/blog/roshanmehta/2012/2/16/Dynamics_CRM_2011_Closing_all_Related_Activities_for_a_Record.aspx

https://msdynamicscrmblog.wordpress.com/2013/06/18/there-are-still-open-activities-associated-with-this-case-when-resolving-a-case-in-dynamics-crm-2011/