Recently, I had faced an issue. A Contact record ABC was updated with email address 2 field to the Customer Service Email (customerservice@company.com) on the 10th of May.
Upon investigating, one of the users intentionally updated this contact record.
This led to all of the Customer Service emails getting tracked to the timeline of ABC’s Contact record.
The customer service email address was removed from the Contact record on 20th May. While no new emails are getting tracked to the timeline, but any replies to the already tracked emails are still syncing to the Contact daily.
Potential Solution: A ticket was raised with MS for finding a solution to this issue. They proposed deleting and recreating the contact record, however, the contact contains several child records and the impact would be severe. So an alternate solution could be to remove the Contact (ABC) from the “To” field of the tracked emails, by running a script.
Following is the code that I have used to remove the ABC contact record from the collection of the “TO” parties, for the closed email messages.
Guid contactGuid = new Guid("f6c42c30-150c-e511-80c8-00155d0a471a");
string query = @" ";
List emails = service.RetrieveMultiple(new FetchExpression(query)).Entities.ToList();//.Take(1).ToList();
emails.ForEach(email =>
{
EntityCollection to = email.GetAttributeValue("to");
if (to != null)
{
to.Entities.ToList().ForEach(party =>
{
EntityReference partyId = party.GetAttributeValue("partyid");
string addressUsed = party.GetAttributeValue("addressused");
if (partyId?.Id == contactGuid && addressUsed == "customerservice@company.com")
{
to.Entities.Remove(party);
}
});
}
//Set to Draft
var stateRequest = new SetStateRequest
{
State = new OptionSetValue(0),
Status = new OptionSetValue(1),
EntityMoniker = email.ToEntityReference()
};
service.Execute(stateRequest);
//Update Parties
Entity emailToUpdate = new Entity("email");
emailToUpdate.Id = email.Id;
emailToUpdate["to"] = to;
service.Update(emailToUpdate);
//Revert Status
var stateRequestAfter = new SetStateRequest
{
State = email.GetAttributeValue("statecode"),
Status = email.GetAttributeValue("statuscode"),
EntityMoniker = email.ToEntityReference()
};
service.Execute(stateRequestAfter);
});
Hopefully, this post might help someone else who has similar issues
Leave a Reply