Hi Experts,
I am trying to send email to all required attendees and optional attendees of an appointment using plugin. It works fine on CREATE message but in update message its throwing error as Duplicate key cannot insert.
here is my code.
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using System;
namespace Send_Email_Plugin
{
// Importnant Note-------------------------------------------------------------------------------------------------------------------
// When you are sending emails you have to go to User profile in CRM from which the email gets triggered, and click on "APPROVE EMAIL"
// The Email Box should configured properly.
//-----------------------------------------------------------------------------------------------------------------------------------
public class SendEmail_Plugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Tracing Object
ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
//ExecutionContext Object
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
//OrganizationServiceFactory Object
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
//OrganizationService Object
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
string functionName = "Execute";
try
{
EntityCollection requiredAttendees;
EntityCollection optionalAttendees;
EntityReference RegardingEntRef;
Entity appointmentEn;
if (context.MessageName == "Update")
appointmentEn = context != null && context.InputParameters.Contains("Target") ? (Entity)context.PostEntityImages["PostImage"] : null;
else if (context.MessageName == "Create")
appointmentEn = context != null && context.InputParameters.Contains("Target") ? (Entity)context.InputParameters["Target"] : null;
else
return;
tracing.Trace("retrieved entity :" + appointmentEn.LogicalName + "Message:" + context.MessageName);
if (appointmentEn.LogicalName == "appointment")
{
tracing.Trace("validated entity..");
if (appointmentEn.Attributes.Contains("requiredattendees") && appointmentEn.Attributes.Contains("optionalattendees") && appointmentEn.Attributes.Contains("regardingobjectid"))
{
tracing.Trace("Validated attendees");
//read regarding field
RegardingEntRef = appointmentEn.GetAttributeValue<EntityReference>("regardingobjectid");
tracing.Trace("regarding : " + RegardingEntRef.Name);
//read required and optional attendees
requiredAttendees = appointmentEn.Attributes.Contains("requiredattendees") ? appointmentEn.GetAttributeValue<EntityCollection>("requiredattendees") : null;
optionalAttendees = appointmentEn.Attributes.Contains("optionalattendees") ? appointmentEn.GetAttributeValue<EntityCollection>("optionalattendees") : null;
var appointmentSubject = appointmentEn.Attributes.Contains("subject") ? appointmentEn.GetAttributeValue<String>("subject") : null;
var appointmentLocation = appointmentEn.Attributes.Contains("location") ? appointmentEn.GetAttributeValue<String>("location") : null;
var appointdescription = appointmentEn.Attributes.Contains("description") ? appointmentEn.GetAttributeValue<String>("description") : null;
//send email to required attendees
if (requiredAttendees != null && requiredAttendees.Entities.Count > 0)
{
SendEmail(requiredAttendees, appointmentSubject, appointdescription, service, tracing);
}
//send email to optional attendees
if (optionalAttendees != null && optionalAttendees.Entities.Count > 0)
{
SendEmail(requiredAttendees, appointmentSubject, appointdescription, service, tracing);
}
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(functionName + ": " + ex.Message);
}
}
public void SendEmail(EntityCollection ec, string subject, string desc, IOrganizationService svc,ITracingService trace)
{
trace.Trace("inside SendEmail EC");
// Create a new EntityCollection and add the all parties
EntityCollection to = new EntityCollection();
foreach (var attendees in ec.Entities)
{
to.Entities.Add(attendees);
}
trace.Trace("after EC");
// Create an email with the EntityCollection
Entity email = new Entity("email");
email["subject"] = subject;
email["to"] = to;
email["description"] = desc;
var eid = svc.Create(email);
trace.Trace("after email create");
SendEmailRequest sendEmailReq = new SendEmailRequest
{
EmailId = eid,
TrackingToken = "",
IssueSend = true
};
SendEmailResponse sendEmailResp = (SendEmailResponse)svc.Execute(sendEmailReq);
trace.Trace("after email sent");
}
}
}