Send email to secondary email of Account/Contacts – Dynamics CRM

By default, CRM will send e-mail to “emailaddress1” for Accounts and Contacts. Sometimes we may need to send the e-mail to “emailaddress2” (or) “emailaddress3”. We can achieve this with some customizations and Plug-in. Steps to achieve the above requirement as below:

  1. Create a new field (Email Preference) in “Account/Contact” entity with Options “emailaddress1”, “emailaddress2” and “emailaddress3”.
  2. Write a Plug-in on “ Pre send” of email. The plug-in works as below:
  3. Plug-in will fire on “Pre-operation” of send message of email.
  4. Retrieve all the activity parties from the “To” field.
  5. Retrieve “Account/Contact” records based on the “partyid” of “To” activities.
  6. Check the “Email-preference” selected option in “Account/Contact” entity
  7. Replace the “addressused” field of activityparty with selected option of “Email Preference” in “Account/Contact” entity.
  8. Update the email activity.

I have done the same requirement for contact entity as below:

//Plug-in Code to replace the “Email address” in To field.

public void Execute(IServiceProvider serviceProvider)

        {

            IPluginExecutionContext _context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory _serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationService _service = (IOrganizationService)_serviceFactory.CreateOrganizationService(_context.UserId);

            Guid emailId = Guid.Empty;

            if (_context.PrimaryEntityName != “email”)

            {              

                return;        

            }

            if (_context.InputParameters.Contains(“EmailId”)&& _context.InputParameters[“EmailId”] is Guid)

            {

                emailId = (Guid)_context.InputParameters[“EmailId”];

                if (emailId == Guid.Empty)

                {

                    return;

                }

                Entity emailEntity = _service.Retrieve(“email”, emailId, new ColumnSet(true));

                if (emailEntity != null&& emailEntity.Contains(“to”))

                {

                    EntityCollection activityParties = (EntityCollection)emailEntity.Attributes[“to”];

                    foreach (Entity activityParty in activityParties.Entities)

                    {

                        if (activityParty.Contains(“partyid”) && ((EntityReference)activityParty.Attributes[“partyid”]).LogicalName == “contact”)

                        {

                            Entity contact = _service.Retrieve(“contact”, ((EntityReference)activityParty.Attributes[“partyid”]).Id, new ColumnSet(“emailaddress2″,”emailaddress3”, “new_emailpreference”));

                            if (contact.Attributes.Contains(“new_emailpreference”))

                            {

                                if (((OptionSetValue)contact.Attributes[“new_emailpreference”]).Value == 100000001)

                                {

                                    if (contact.Contains(“emailaddress2”) && contact.Attributes[“emailaddress2”] != null)

                                    {

                                        activityParty.Attributes[“addressused”] = (string)contact[“emailaddress2”];

                                    }

                                }

                                else if (((OptionSetValue)contact.Attributes[“new_emailpreference”]).Value == 100000002)

                                {

                                    if (contact.Contains(“emailaddress3”) && contact.Attributes[“emailaddress3”] != null)

                                    {

                                        activityParty.Attributes[“addressused”] = (string)contact[“emailaddress3”];

                                    }

                                }

                            }                           

                        }

                    }

                    _service.Update(emailEntity);

                }

            }

        } 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s