xrm.SaveChanges error while looping through Cases (Reopen, Update, and Close) RRS feed

  • Question

  • I want to reopen, and then update and the close the Case in one go.

    However, xrm.SaveChanges gives the following error "This case has already been resolved. Close and reopen the case record to see the updates."

    But when debugging I can see that the Case actually does change state. Can it be that the update change for all records is only fully completed when reaching xrm.SaveChanges?

    How do I solve this issue?

            protected void ModifyCases_Click(object sender, EventArgs e)
                var xrm = new XrmServiceContext("Xrm");
                var querycases = (
                 from i in xrm.IncidentSet
                 where i.StateCode.Value == 1
                 && i.StatusCode == 5
                 && i.OwnerId.Id.Equals("26489806-250d-e211-8af5-3c4a92dbd83d")
                 select i).Take(100).ToList();
                foreach (Incident incident in querycases)
                    // Reactivate Case
                    SetStateRequest setStateReq = new SetStateRequest();
                    setStateReq.EntityMoniker = new EntityReference(incident.LogicalName, incident.Id);
                    setStateReq.State = new OptionSetValue(0);
                    setStateReq.Status = new OptionSetValue(1);
                    SetStateResponse response = (SetStateResponse)xrm.Execute(setStateReq);
                    // Update Case
                    incident.new_csem_id = incident.IncidentId.ToString();
                    // Case Resolution 
                    Entity caseResolution = new Entity("incidentresolution");
                    caseResolution.Attributes.Add("incidentid", new EntityReference(incident.LogicalName, incident.Id));
                    caseResolution.Attributes.Add("subject", "Auto closed during data migration");
                    // Mark Case as Resolved
                    CloseIncidentRequest req = new CloseIncidentRequest();
                    req.IncidentResolution = caseResolution;
                    req.RequestName = "CloseIncident";
                    OptionSetValue o = new OptionSetValue();
                    o.Value = 5;
                    req.Status = o;
                    CloseIncidentResponse resp = (CloseIncidentResponse)xrm.Execute(req);

    Saturday, January 4, 2014 9:55 PM

All replies

  • Hi,

    OrganizationServiceContext just keeps track of entity or relationship changes you make until it is submitted back to CRM server with SaveChanges call. I guess when you perform multiple transactions to a single record like this, CRM is just looking at the final state when you submit with SaveChanges. I'd try committing after you reactivate the case, then perform update and close follow by another commit.


    Saturday, January 4, 2014 10:46 PM