Remove irrelevant activities from timeline for a record, by removing the activity parties

Welcome to my old Blogspot blog! You can read this post on our new website: Read this post on our new website 

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 = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
                    <entity name='email'>
                    <attribute name='activityid' />
                    <attribute name='to' />
                    <attribute name='statuscode' />
                    <attribute name='statecode' />
                        <filter type='and'>
                            <condition attribute='createdon' operator='on-or-after' value='2022-05-10' />                           
                            <condition attribute='regardingobjectid' operator='ne' value='{F6C42C30-150C-E511-80C8-00155D0A471A}' />
                        </filter>
                    <link-entity name='activityparty' from='activityid' to='activityid' link-type='inner' alias='ao'>
                        <filter type='and'>
                            <condition attribute='partyid' operator='eq' value='{F6C42C30-150C-E511-80C8-00155D0A471A}' />
                            <condition attribute='participationtypemask' operator='eq' value='2' />
                        </filter>
                    </link-entity>
                    </entity>
                    </fetch>";

    List<Entity> emails = service.RetrieveMultiple(new FetchExpression(query)).Entities.ToList();//.Take(1).ToList();       
    emails.ForEach(email =>
    {
        EntityCollection to = email.GetAttributeValue<EntityCollection>("to");
        if (to != null)
        {
            to.Entities.ToList().ForEach(party =>
            {
                EntityReference partyId = party.GetAttributeValue<EntityReference>("partyid");
                string addressUsed = party.GetAttributeValue<string>("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<OptionSetValue>("statecode"),
            Status = email.GetAttributeValue<OptionSetValue>("statuscode"),
            EntityMoniker = email.ToEntityReference()
        };
        service.Execute(stateRequestAfter);

    });
Hopefully, this post might help someone else who has simiar issues

No comments:

Post a Comment