title: How to close cases in crm 2011 programmatically
body: <p>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 <code>public override void Input0_ProcessInputRow(Input0Buffer Row)</code> method.</p>
  
  <pre><code>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") &amp; 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);
          }
      }
  }
  </code></pre>
  
  <p><strong>Row.DEVCaseGUID</strong> is the GUID of the Case.
  <strong>statuscode</strong> is <code>5</code> for closed.
  <strong>statecode</strong> is <code>2</code> for Resolved.</p>
  
  <p>I tried follow this <a href="https://community.dynamics.com/crm/f/117/t/160206" rel="nofollow noreferrer">example</a> but no success. Or is there any simple way to achieve this ?</p>