web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Delete Contact and remove him from the "to" field in all related E-Mails

(0) ShareShare
ReportReport
Posted on by 423

!!!IMPORTANT!!!  The original code had some issues and was not able to work. Using the To.Array() did only resulted in no error message on the Update. This resulted in a E-Mail with missing information in the "TO" field. (the field was empty).

WORKING CODE AT THE BOTTOM

 

 

How can I delete a already deleted contact email address from the "to" field in all related E-Mails?

Example:

from: user

to: x, y

(x should be removed, the email should look like that)

from: user

to: y

I tried it with C# (Don't use this! Use the code below)

            var queryEmail = new QueryExpression("email");
            queryEmail.Distinct = true;

            queryEmail.ColumnSet.AddColumns("subject", "to", "activityid");
            queryEmail.AddOrder("subject", OrderType.Ascending);

            var email_activityparty = queryEmail.AddLink("activityparty", "activityid", "activityid");
            email_activityparty.EntityAlias = "ae";
            email_activityparty.Columns.AddColumns("partyid");

            email_activityparty.LinkCriteria.AddCondition("partyid", ConditionOperator.Equal, "27d56488-b5ec-e811-a2b8-00505687c621");


            EntityCollection emails = service.RetrieveMultiple(queryEmail);

            Guid guidToCompare = new Guid("27d56488-b5ec-e811-a2b8-00505687c621");

            Dictionary<Guid, string> processed = new Dictionary<Guid, string>();

            foreach (Entity email in emails.Entities)
            {
                EntityCollection to = email.GetAttributeValue<EntityCollection>("to");

                List<EntityReference> toUpdate = new List<EntityReference>();

                foreach (Entity e in to.Entities)
                {
                    EntityReference partyId = e.GetAttributeValue<EntityReference>("partyid");

                    if (partyId.Id != guidToCompare)
                    {
                        toUpdate.Add(partyId);
                    }
                    else
                    {
                        var dropped = partyId.Id;
                    }

                    
                }

                email["to"] = toUpdate;
                //Update record
                service.Update(email);
            }

But I got this error on the update:

{"The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter schemas.microsoft.com/.../Services:entity. The InnerException message was 'Error in line 1 position 1349. Element 'schemas.datacontract.org/.../System.Collections.Generic:value' contains data from a type that maps to the name 'System.Collections.Generic:List`1'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'List`1' and namespace 'System.Collections.Generic'.'.  Please see InnerException for more details."}

Inner Exception: null

Just deactivating the contact will not work, since they named the contact e-mail address like our queue and now it does have multiple connections. If I just delete it, the related entities will generate a error. (For example: If you try the "answer all" button in a email.)

Any ideas would be very nice!

EDIT: I had to rework my Code since it didn't worked. Here is the

Working code:

Dictionary<Guid, string> processed = new Dictionary<Guid, string>();
            Guid guidToCompare = new Guid("27d56488-b5ec-e811-a2b8-00505687c621");

            string connectionString = "AuthType=AD;Url=http://SERVER/ORG";
            CrmServiceClient conn = new CrmServiceClient(connectionString);
            IOrganizationService service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

            var queryEmail = new QueryExpression("email");
            queryEmail.Distinct = true;
            queryEmail.ColumnSet.AddColumns("subject", "to", "from", "activityid", "statuscode", "statecode");
            queryEmail.AddOrder("subject", OrderType.Ascending);
            var email_activityparty = queryEmail.AddLink("activityparty", "activityid", "activityid");
            email_activityparty.Columns.AddColumns("partyid");
            email_activityparty.LinkCriteria.AddCondition("partyid", ConditionOperator.Equal, "27d56488-b5ec-e811-a2b8-00505687c621");

            EntityCollection emails = service.RetrieveMultiple(queryEmail);

            foreach (Entity email in emails.Entities)
            {
                EntityCollection to = email.GetAttributeValue<EntityCollection>("to");

                List<Entity> entityList = new List<Entity>();

                foreach (Entity party in to.Entities)
                {
                    EntityReference partyId = party.GetAttributeValue<EntityReference>("partyid");

                    // sort the unwantend party out
                    if (partyId != null && partyId.Id != guidToCompare)
                    {
                        entityList.Add(party);

                        if(!processed.ContainsKey(email.Id))
                            processed.Add(email.Id, string.Format(email.GetAttributeValue<string>("subject")));
                    }
                    else
                    {
                        //(just to see what is droped)
                        if(partyId != null)
                        {
                            var dropped = partyId.Id;
                        }
                    }
                }

                EntityCollection toUpdate = new EntityCollection();

                foreach (Entity party in entityList)
                {
                    toUpdate.Entities.Add(party);
                }

                email["to"] = toUpdate;

                // Get state
                OptionSetValue statecode = email.GetAttributeValue<OptionSetValue>("statecode"); // 1
                OptionSetValue statuscode = email.GetAttributeValue<OptionSetValue>("statuscode"); //2

                EntityReference emailRef = email.ToEntityReference();

                // Set current email state to draft
                SetStateRequest stateDraft = new SetStateRequest();
                stateDraft.State = new OptionSetValue(0);
                stateDraft.Status = new OptionSetValue(1);

                stateDraft.EntityMoniker = emailRef;

                SetStateResponse stateSetToDraft = (SetStateResponse)service.Execute(stateDraft);

                //Update record
                service.Update(email);

                // Revert state
                SetStateRequest stateOrigin = new SetStateRequest();
                stateOrigin.State = statecode;
                stateOrigin.Status = statuscode;

                stateOrigin.EntityMoniker = emailRef;

                SetStateResponse stateSetToOrigin = (SetStateResponse)service.Execute(stateOrigin);


            }

            var result = processed;

*This post is locked for comments

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Delete Contact and remove him from the "to" field in all related E-Mails

    Hello,

    Try to replace line

    email["to"] = toUpdate;

    to line

    email["to"] = toUpdate.ToArray();

  • bernhards Profile Picture
    423 on at
    RE: Delete Contact and remove him from the "to" field in all related E-Mails

    Thanks! That solved the problem. But why do I need to convert it to an array?

  • Verified answer
    gupta.ambar2009@gmail.com Profile Picture
    797 on at
    RE: Delete Contact and remove him from the "to" field in all related E-Mails

    Hi ,

    Related question to your for Andrew  Answer , you need to take array for TO, CC and BCC field since its party type field which store collection of record (its not a lookup which store only single record )

    Point out for any other details  

  • bernhards Profile Picture
    423 on at
    RE: Delete Contact and remove him from the "to" field in all related E-Mails

    Thanks for your explanation. It is maybe a stupid question, but is this something you just know or was that clear because of the error message I received?

  • gupta.ambar2009@gmail.com Profile Picture
    797 on at
    RE: Delete Contact and remove him from the "to" field in all related E-Mails

    Hi ,

    Its not clear through error message , but i was aware with the partylist behavior  :)

  • bernhards Profile Picture
    423 on at
    RE: Delete Contact and remove him from the "to" field in all related E-Mails

    I understand. :) Thanks a lot!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Wayne Walton Profile Picture

Wayne Walton 2

#2
Good.Panos Profile Picture

Good.Panos 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans