private static Entity SetCaseStatusAndSave(Entity incident, string state, string resolutionSubject, OrganizationServiceContext service) { var id = incident.Id; if (string.Compare(state, "Active", StringComparison.InvariantCultureIgnoreCase) == 0) { service.SetState(0, -1, incident); } else if (string.Compare(state, "Resolved", StringComparison.InvariantCultureIgnoreCase) == 0) { var resolution = new Entity("incidentresolution"); resolution.SetAttributeValue("incidentid", incident.ToEntityReference()); resolution.SetAttributeValue("statuscode", new OptionSetValue(-1)); resolution.SetAttributeValue("subject", resolutionSubject); service.CloseIncident(resolution, -1); } else // Canceled { service.SetState(2, -1, incident); } return(service.CreateQuery("incident").First(i => i.GetAttributeValue <Guid?>("incidentid") == id)); }