Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

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

$
0
0

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();
                xrm.UpdateObject(incident);              
                // 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");
                xrm.Create(caseResolution);
                // 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);
            };
            xrm.SaveChanges();
        }



Viewing all articles
Browse latest Browse all 8223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>