Recently I had a requirement where one of our client’s team wanted a feature for automatically filling "Regarding" filed on Tracking any Sent Email from Outlook by looking for the associated record of the first email address in "To" field then set "Regarding" filed with the associated record if found.
I have covered their need by Plugin, below are the code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk;
using System.Data.SqlClient;
namespace CustomerPlugin
{
public class SetRegardingFromOutlook : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = context.InputParameters["Target"] as Entity;
if (entity.LogicalName != "email")
{
return;
}
if (context.MessageName == "Create")
{
if (entity.Contains("directioncode"))
{
if (((Boolean)entity["directioncode"]) == true)// Outgoing
{
if (entity.Contains("statecode"))
{
OptionSetValue entityStatusCode = new OptionSetValue();
entityStatusCode = (OptionSetValue)entity.Attributes["statuscode"];
if (entityStatusCode.Value == 3)//Sent
{
var to = (EntityCollection)null;
Guid partyId = Guid.Empty;
if (entity.Attributes.Contains("to"))
to = entity.GetAttributeValue<EntityCollection>("to");
if (to != null && to.Entities.Count > 0)
{
var party = to.Entities[0];
var partyType = party.GetAttributeValue<EntityReference>("partyid").LogicalName;
partyId = party.GetAttributeValue<EntityReference>("partyid").Id;
entity.Attributes.Add("regardingobjectid", new EntityReference(partyType, partyId));
service.Update(entity);
}
}
}
}
}
}
}
}
catch (Exception ex)
{
tracingService.Trace("Failed to Create Email From Outlook : " + ex.Message.ToString());
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
Below how to register this Plugin :
Hope its help!
I have covered their need by Plugin, below are the code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk;
using System.Data.SqlClient;
namespace CustomerPlugin
{
public class SetRegardingFromOutlook : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = context.InputParameters["Target"] as Entity;
if (entity.LogicalName != "email")
{
return;
}
if (context.MessageName == "Create")
{
if (entity.Contains("directioncode"))
{
if (((Boolean)entity["directioncode"]) == true)// Outgoing
{
if (entity.Contains("statecode"))
{
OptionSetValue entityStatusCode = new OptionSetValue();
entityStatusCode = (OptionSetValue)entity.Attributes["statuscode"];
if (entityStatusCode.Value == 3)//Sent
{
var to = (EntityCollection)null;
Guid partyId = Guid.Empty;
if (entity.Attributes.Contains("to"))
to = entity.GetAttributeValue<EntityCollection>("to");
if (to != null && to.Entities.Count > 0)
{
var party = to.Entities[0];
var partyType = party.GetAttributeValue<EntityReference>("partyid").LogicalName;
partyId = party.GetAttributeValue<EntityReference>("partyid").Id;
entity.Attributes.Add("regardingobjectid", new EntityReference(partyType, partyId));
service.Update(entity);
}
}
}
}
}
}
}
}
catch (Exception ex)
{
tracingService.Trace("Failed to Create Email From Outlook : " + ex.Message.ToString());
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
Below how to register this Plugin :
Hope its help!