Answered by:
Error in line xrm.SaveChanges(); while updating more than one email record

Question
-
If I reference a single ActivityId it works fine.
The following error is shown "An error occured while processing this request" at line xrm.SaveChanges();
What am I doing wrong?
protected void ModifyEmails_Click(object sender, EventArgs e) { var xrm = new XrmServiceContext("Xrm"); var GetEmails = xrm.EmailSet .Where(c => c.StateCode.Equals(1)) //.Where(c => c.ActivityId.Equals("e85d7026-0167-e311-b548-3c4a92dbd83d")) .Where(c => c.DirectionCode.Equals(true)) .Where(c => c.mp_contactid2.Equals(null)) .Take(5) .ToList(); foreach (Email email in GetEmails) { EntityCollection Recipients = email.GetAttributeValue<EntityCollection>("to"); ActivityParty ap = Recipients[0].ToEntity<ActivityParty>(); email.mp_contactid2 = new EntityReference(Contact.EntityLogicalName, ap.PartyId.Id); xrm.UpdateObject(email); }; xrm.SaveChanges(); }
- Edited by Allen Campbell Thursday, January 2, 2014 10:44 PM
Thursday, January 2, 2014 10:43 PM
Answers
-
The code looks ok, my best guess is that not all the first "to" recipient are contacts.
My blog: www.crmanswers.net - Rockstar 365 Profile
- Marked as answer by Allen Campbell Saturday, January 4, 2014 9:46 PM
Thursday, January 2, 2014 11:19 PM -
Hi,
Have you checked what the inner exception says?
Ronald
- Marked as answer by Allen Campbell Saturday, January 4, 2014 9:46 PM
Thursday, January 2, 2014 11:20 PM
All replies
-
The code looks ok, my best guess is that not all the first "to" recipient are contacts.
My blog: www.crmanswers.net - Rockstar 365 Profile
- Marked as answer by Allen Campbell Saturday, January 4, 2014 9:46 PM
Thursday, January 2, 2014 11:19 PM -
Hi,
Have you checked what the inner exception says?
Ronald
- Marked as answer by Allen Campbell Saturday, January 4, 2014 9:46 PM
Thursday, January 2, 2014 11:20 PM -
If I reference a single ActivityId it works fine.
The following error is shown "An error occured while processing this request" at line xrm.SaveChanges();
What am I doing wrong?
protected void ModifyEmails_Click(object sender, EventArgs e) { var xrm = new XrmServiceContext("Xrm"); var GetEmails = xrm.EmailSet .Where(c => c.StateCode.Equals(1)) //.Where(c => c.ActivityId.Equals("e85d7026-0167-e311-b548-3c4a92dbd83d")) .Where(c => c.DirectionCode.Equals(true)) .Where(c => c.mp_contactid2.Equals(null)) .Take(5) .ToList(); foreach (Email email in GetEmails) { EntityCollection Recipients = email.GetAttributeValue<EntityCollection>("to"); ActivityParty ap = Recipients[0].ToEntity<ActivityParty>(); email.mp_contactid2 = new EntityReference(Contact.EntityLogicalName, ap.PartyId.Id); xrm.UpdateObject(email); }; xrm.SaveChanges(); }
Changed my code to the below and it works fine:
protected void ModifyEmails_Click(object sender, EventArgs e) { var xrm = new XrmServiceContext("Xrm"); var queryemails = ( from e1 in xrm.EmailSet join i in xrm.IncidentSet on e1.RegardingObjectId.Id equals i.IncidentId where e1.StateCode.Value == 1 && e1.StatusCode.Value == 3 && e1.DirectionCode == true && e1.mp_contactid2 == null where i.StateCode.Value == 1 && i.OwnerId.Id.Equals("26489806-250d-e211-8af5-3c4a92dbd83d") select e1).Take(20).ToList(); foreach (Email email in queryemails) { EntityCollection Recipients = email.GetAttributeValue<EntityCollection>("to"); ActivityParty ap = Recipients[0].ToEntity<ActivityParty>(); if (ap.PartyId.LogicalName == "contact") { email.mp_contactid2 = new EntityReference(Contact.EntityLogicalName, ap.PartyId.Id); } xrm.UpdateObject(email); }; xrm.SaveChanges(); }
- Edited by Allen Campbell Saturday, January 4, 2014 9:48 PM
Saturday, January 4, 2014 9:47 PM