Skip to main content

Notifications

D365 CRM Email - How to determine if a particular email address is in the recipient list?

Introduction

In Dynamics 365 CRM, Email functionality is crucial in effective communication with customers and stakeholders.
One common requirement is to determine whether a specific email address exists in the recipient list of an Email record.
In this blog, I will explore the steps necessary to accomplish this task.

Where is the recipient's email address saved?

All the recipient's information is stored in the Activity party entity. An email record has multiple activity parties, such as sender, to, cc, bcc, etc.
You can refer to the screenshot below, which displays a list of all the roles of the person in the activity. 
Take this record for example:
 

Step 1: Retrieve the Recipient List

This can be achieved by accessing the ActivityParty entity and retrieving this field programmatically.
 
Option1: Get all recipient email addresses from Code through Earlybound
 public List<string> GetRecipientEmailAddressByEarlyBound(Guid emailId)
        {
            var emailAddressList = this._serviceContext.ActivityPartySet.Where(
                                                         a => a.ActivityId.Equals(emailId)
                                                         &&
                                                         (
                                                             (int)a.ParticipationTypeMask == (int)activityparty_participationtypemask.ToRecipient
                                                         ||
                                                             (int)a.ParticipationTypeMask == (int)activityparty_participationtypemask.CCRecipient
                                                         ||
                                                             (int)a.ParticipationTypeMask == (int)activityparty_participationtypemask.BCCRecipient
                                                         )
                                                     )
                                                     .Select(a => a.AddressUsed).ToList();
            return emailAddressList;
        }
 
 
Option2: Get all recipient email addresses from Code through Latebound
public List<string> GetRecipientEmailAddressByFetchXml(Guid emailId)
        {
            List<string> emailAddressList = new List<string>();
            string fetchXmlFormat = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
                                      <entity name='activityparty'>
                                        <attribute name='participationtypemask' /> 
                                        <attribute name='activityid' /> 
                                        <attribute name='addressused' /> 
                                        <filter type='and'>
                                          <condition attribute='activityid' operator='eq' value='{0}' />
                                      <condition attribute='participationtypemask' operator='in'>
                                              <value>4</value>
                                              <value>3</value>
                                              <value>2</value>
                                            </condition>
                                     </filter> 
                                      </entity>
                                    </fetch>";

            var fetchResult = this._orgService.RetrieveMultiple(new FetchExpression(string.Format(fetchXmlFormat, emailId.ToString())));
            foreach (var activityParty in fetchResult.Entities)
            {
                if (activityParty.Contains("addressused"))
                {
                    var addressUsed = activityParty.FormattedValues["addressused"].ToString();
                    emailAddressList.Add(addressUsed);
                } 
            }
            return emailAddressList;
        }
 

Step2: Iterate through the Recipient List

Next, iterate through the recipient list and compare each email address with the specific email address you want to validate. There are different approaches to accomplish this, such as using a workflow, a plugin, or JavaScript code.
During the iteration, if a match is found between the specific email address and any email address in the recipient list, you can conclude that the email address is present in the recipient list. Accordingly, you can proceed with the desired actions or logic based on this confirmation.
 

Conclusion:

Determining if a specific email address exists in the recipient list in D365 CRM Email functionality is crucial for various customization and business requirements.
Remember, these steps can be tailored based on your specific CRM customizations and requirements.
 
 
The End
 

Comments

*This post is locked for comments