How to send email to the secondary address of contact/account/lead entity

Dynamics CRM by default send email to the main address (emaildresse1)

I have created a plugin to send an email to recipient secondary addresses
Such as a contract that has multiple email addresses,
Its main address (emailadresse1) and  secondary address (emailadress2)and other personal email address (emailadress3).

In some cases we need to send an email to the different mail addresses, so to solve this problem I create two custom boolean fields in the entity email (emailaddress2, and emailaddress3) to choose if we want to send the email to secondary addresses
I have republished it as boxes to check as the screen prints below

email form
Email form

The addresses are marked in red in the cc field (they added automatic by our plugin), this is the default behavior of the CRM, because the CRM resolved the recipient name by its email address1.

Then I added a plugin to add the secondary addresses in the (cc) field, the following method adds the secondary addresses in the cc field

// this is function to check if the customize field is checked and add the specific email adderess to cc field
private static void AddAdressMailToCc(IOrganizationService service, Entity emailEntity, Entity activityParty, string entityName)
{
/****entity represent to recipient like account or contact or lead*****/
Entity entity = service.Retrieve(entityName, ((EntityReference)activityParty.Attributes["partyid"]).Id, new ColumnSet("emailaddress2", "emailaddress3"));
if (entity != null)
{
EntityCollection cc = new EntityCollection();
if (emailEntity.GetAttributeValue("new_adressemail2") == true)
{
if (entity.Contains("emailaddress2") && entity.Attributes["emailaddress2"] != null)
{
EntityCollection ec = emailEntity.GetAttributeValue("cc");
Entity party2 = new Entity("activityparty");
party2["addressused"] = (string)entity["emailaddress2"];
cc.Entities.Add(party2);
}
}
if (emailEntity.GetAttributeValue("new_adressemail3") == true)
{
if (entity.Contains("emailaddress3") && entity.Attributes["emailaddress3"] != null)
{
Entity party3 = new Entity("activityparty");
party3["addressused"] = (string)entity["emailaddress3"];
cc.Entities.Add(party3);
}
}
(emailEntity["cc"] as EntityCollection).Entities.AddRange(cc.Entities);
}
service.Update(emailEntity);
}

And I call this method in the Execute method

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)
{
//contact entity
if (activityParty.Contains("partyid") && ((EntityReference)activityParty.Attributes["partyid"]).LogicalName == "contact")
{
AddAdressMailToCc(service, emailEntity, activityParty, "contact");
}
//account entity
if (activityParty.Contains("partyid") && ((EntityReference)activityParty.Attributes["partyid"]).LogicalName == "account")
{
AddAdressMailToCc(service, emailEntity, activityParty, "account");
}
//lead entity
if (activityParty.Contains("partyid") && ((EntityReference)activityParty.Attributes["partyid"]).LogicalName == "lead")
{
AddAdressMailToCc(service, emailEntity, activityParty, "lead");
}
}
}
}
 And I add the plugin with RegistrationPluginTool :

  •  PreOperation
  • Message: send
  • Primary Entity : Email
Advertisements

Dynamics 365 – Unified Service Desk

Dynamics 365 Funda by Zohaib Uddin Khan

Author: Zohaib Uddin Khan

Let’s discuss about the Unified Service Desk, What is Unified Service Desk or USD?

Unified Service Desk for Microsoft Dynamics 365 provides a configuration-based framework for quickly building agent applications for call centers. You can aggregate customer information from different areas in Microsoft Dynamics 365 into a single desktop and get a 360° view of customer interactions.

It has two components to be installed, one component to be installed in your Organization another is the Client Application.

  1. Browse to Unified Service Desk for Microsoft Dynamics 365.

Dynamics-365-Unified-Service-Desk-1

  1. As I mentioned earlier, USD has two components, so we’ll download:-
  • Dynamics365-USD-2.2.0.755-PackageDeployer.exe (Compulsory)
  • Dynamics365-USD-2.2.1.806-amd64.exe (Optional. Since, my development machine is 64 bit, so I chosed this one.)

or

  • Dynamics365-USD-2.2.1.806-i386.exe (Optional)

Dynamics-365-Unified-Service-Desk-2

  1. Once, download finished, double click the ‘Dynamics365-USD-2.2.0.755-PackageDeployer.exe’.

Dynamics-365-Unified-Service-Desk-3

  1. Enter the necessary information of your organization. Keep remember the user should have ‘System Administrator’ role.

Dynamics-365-Unified-Service-Desk-4

  1. As a new comer to Unified…

View original post 159 more words

Convert Date from UTC to Time Zone of User

The Date/Time fields stored in CRM with the UTC format, and the Time Zone Code associated with each User in the system settings.
To get the Date/Time field from CRM with the Time Zone of specific user, we must pass the Guid of the user as parameter to get the TimeZoneInfo.


public static DateTime ConvertTimeFromUtc(OrganizationService service, Guid userid, DateTime dateToconvert)
{
//replace userid with id of user
Entity userSettings = service.Retrieve("usersettings", userid, new ColumnSet("timezonecode"));
//timezonecode for UTC is 85
int timeZoneCode = 85;
//retrieving timezonecode from usersetting
if ((userSettings != null) && (userSettings["timezonecode"] != null))
{
timeZoneCode = (int)userSettings["timezonecode"];
}
//retrieving standard name
var qe = new QueryExpression("timezonedefinition");
qe.ColumnSet = new ColumnSet("standardname");
qe.Criteria.AddCondition("timezonecode", ConditionOperator.Equal, timeZoneCode);
EntityCollection TimeZoneDef = service.RetrieveMultiple(qe);
TimeZoneInfo userTimeZone = null;
if (TimeZoneDef.Entities.Count == 1)
{
String timezonename = TimeZoneDef.Entities[0]["standardname"].ToString();
userTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timezonename);
}
//converting date from UTC to user time zone
DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(dateToconvert, userTimeZone);
return cstTime;
}

Change the CreatedOn Value

To change the value of createdOn value in MS dynamics CRM with c# :

you must use the early bound to change the value of createdOn
example :
I want to creat a phonecall entity with specific value of createdOn field:

PhoneCall phonecall = new phonecall();
phonecall.Subject = "Communication";
phonecall["createdon"]=new date ("19/07/2016");
phonecall["overriddencreatedon"] = new date("19/07/2016);

service.create(phonecall);

You must change the overriddencreatedon  to change the createdOn.

I used MS dynamics CRM 2016.

Change the CreatedOn Value

To change the value of createdOn value in MS dynamics CRM with c# :

you must use the early bound to change the value of createdOn
example :
I want to created a phonecall entity with specific value of createdOn value:

PhoneCall phonecall = new phonecall();
phonecall.Subject = "Communication";
phonecall["createdon"]=new date ("19/07/2016");
phonecall["overriddencreatedon"] = new date("19/07/2016);

service.create(phonecall);

You must change the overriddencreatedon to change the createdOn.

I used MS dynamics CRM 2016.

Get description of optionSet

Enumeration :

public enum Country
{

[Display(Description=”Tunisia”)]
Tunisia = 1,

[Display(Description=”France”)]
France = 2,

[Display(Description=”UnitedState”)]
UnitedState = 3,
}

To get the description of enumeration :


public static string GetDescriptionFromEnumValue(Enum value)
{
if (value == null)
return null;
try
{
var fieldInfo = value.GetType().GetField(value.ToString());
if(fieldInfo == null)
{ return null;
}
DisplayAttribute attribute = fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false).SingleOrDefault() as DisplayAttribute;
return attribute.Description;
}
//if the value of enum doesn't exist
catch (Exception ex)
{
throw;
}
}