HOW TO SEND EMAIL TO SECONDARY EMAIL ADDRESS USING PLUGINS IN DYNAMICS 365?

SCENARIO:

By default, CRM will send E-mail to “Primary Email Address” for Accounts, Contacts and Leads. Sometimes we may need to send the E-mail to “Secondary Email Address” (or) “Third Email Address”.
Let us see how we will achieve this with some customizations and Plug-in. To achieve the above requirement, we need to follow below mentioned steps:

NAVIGATION STEPS:

Step 1: Navigate to Advanced Settings  -> Solutions -> Entity.
Step 2: Create a new field of data type “Option set” and provide values (emailaddress1, emailaddress2, emailaddress3).
Step 3: Place Email Address 1, Email Address 2, and Email Address 3 of data type “Single Line of Text” of type Email.

SEND EMAIL

Step 4:
Register a Plug-in on “Pre send” of email.
Step 5: Retrieve all the activity parties from the “To” field.
Step 6: Retrieve “Account/Contact” records based on the “partyid” of “To” activities.
Step 7: Replace the “addressused” field of activityparty with selected option of “Email Preference” in “Account/Contact” entity.

Plugin Code
:

using System.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
namespace CreateTaskPlugin
{
public class SendToSafeEmail: IPlugin
{
public void Execute (IServiceProvider sp)
{
var context = (IPluginExecutionContext) sp.GetService(typeof (IPluginExecutionContext));
var factory = (IOrganizationServiceFactory) sp.GetService(typeof (IOrganizationServiceFactory));
var service = factory.CreateOrganizationService(context.UserId);
Entity email = context.PreEntityImages.Values.First();
var recipient = ((EntityCollection) email.Attributes[“to”]).Entities.FirstOrDefault < Entity > ();
var to = (EntityReference) recipient.Attributes[“partyid”];
//Getting attributes from Dyanmics 365
var account = service.Retrieve(“account”, to.Id, new ColumnSet(“emailaddress1”, “emailaddress2”, “emailaddress3”, “adyt_sendemailto”));
if (account.Attributes.Contains(“adyt_sendemailto”))
{
if (((OptionSetValue) account.Attributes[“adyt_sendemailto”]).Value == 870980000)//Email Address 1
{
try
{
if (account.Attributes[“emailaddress1”] != null)
{
((EntityCollection) email.Attributes[“to”]).Entities[0].Attributes[“addressused”] = account.Attributes[“emailaddress1”];
service.Update(email);
}
}
catch (Exception ee)
{
throw new InvalidPluginExecutionException(“Provide Email Address in Email Address 1”);
}
}
else if (((OptionSetValue) account.Attributes[“adyt_sendemailto”]).Value == 870980001)//Email Address 2
{
try
{
if (account.Attributes[“emailaddress2”] != null)
{
((EntityCollection) email.Attributes[“to”]).Entities[0].Attributes[“addressused”] = account.Attributes[“emailaddress2”];
service.Update(email);
}
}
catch (Exception ee)
{
throw new InvalidPluginExecutionException(“Provide Email Address in Email Address 2”);
}
}
else if (((OptionSetValue) account.Attributes[“adyt_sendemailto”]).Value == 870980002)//Email Address 3
{
try
{
if (account.Attributes[“emailaddress3”] != null)
{
((EntityCollection) email.Attributes[“to”]).Entities[0].Attributes[“addressused”] = account.Attributes[“emailaddress3”];
service.Update(email);
}
}
catch (Exception eee)
{
throw new InvalidPluginExecutionException(“Provide Email Address in Email Address 3”);
}
}
}
}
}
}

Step 8: Plug-in will fire on “Pre-operation” of send message of email.
Step 9: Select the Email Address 2 in “Send Email to” and provide Email Address in “Email Address 2” field.
Step 10: Create a New Email, Save and Send Email.

 

 

 

Leave a Reply