Sometime there is a requirement to execute custom code for performing custom validations, extract data or manipulate data on receiving incoming email in Microsoft Dynamics CRM 2011. The following step by step guide steps explains to develop and register a CRM SDK plugin to achieve this functionality.
[Instructions]1. Use the plugin code below or download the plugin solution wod.Crm.IncommingEmail, make required changes and build plugin assembly file.
2. Use plugin registration tool to register the plugin assembly and then register a new plugin step for Email entity on Create event with Pre-Operation event pipeline stage in Synchronous execution mode
[Plugin: C# Code]
using System; |
using System.Collections.Generic; |
using System.Linq; |
using System.Text; |
using Microsoft.Xrm.Sdk; |
using Microsoft.Xrm.Sdk.Metadata; |
using Microsoft.Xrm.Sdk.Query; |
using Microsoft.Crm.Sdk; |
namespace wod.Crm.IncommingEmail |
{ |
public class wodPlugin : IPlugin |
{ |
public void Execute(IServiceProvider serviceProvider) |
{ |
// Obtain the execution context from the service provider. |
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) |
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)); |
IOrganizationServiceFactory wod_serviceFactory = null; |
IOrganizationService wod_CrmService = null; |
try |
{ |
wod_serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService( typeof(IOrganizationServiceFactory)); |
wod_CrmService = wod_serviceFactory.CreateOrganizationService(context.UserId); |
if (context.MessageName == "Create" && |
context.InputParameters.Contains("Target") && |
context.InputParameters["Target"] is Entity |
) |
{ |
Entity wod_PluginEntity = (Entity)context.InputParameters["Target"]; |
//Checking if plugin is trigger for Email entity |
if (wod_PluginEntity.LogicalName == "email") |
{ |
// Checking if email direction is incomming |
if (wod_PluginEntity.Contains("directioncode")) |
{ |
// Checking if email is incomming |
if (((Boolean)wod_PluginEntity["directioncode"]) == false) |
{ |
EntityCollection wod_IncommingParty = null; |
wod_IncommingParty = (EntityCollection)wod_PluginEntity["from"]; |
// Checking if plugin entity From field is activityparty entity object |
if (wod_IncommingParty != null && wod_IncommingParty[0].LogicalName == "activityparty") |
{ |
EntityReference wod_PartyReference = (EntityReference)wod_IncommingParty[0]["partyid"]; |
// Checking if email is sent by CRM Account |
if (wod_PartyReference.LogicalName == "account") |
{ |
// Retrieve sender Account record |
Entity wod_Account = wod_CrmService.Retrieve("account", wod_PartyReference.Id, new ColumnSet(true)); |
// You can write your code for validation, data manipulation here |
throw new Exception("email account: " + wod_Account["name"]); |
} |
} |
} |
} |
} |
} |
} |
catch (System.Web.Services.Protocols.SoapException ex) |
{ |
throw new InvalidPluginExecutionException(ex.Detail.InnerText); |
} |
catch (Exception ex) |
{ |
throw new InvalidPluginExecutionException(ex.Message); |
} |
} |
} |
} |
i am trying to update the regardingobjectid of the email in my post-processing synchronous plugin to point to an account entity. is it possible? i tryied following code, with no or partial success:
ReplyDelete1. no success (index out of range exception thrown). i have tried early and late bound code too.
early bound:
xrmContext.AddLink(
account,
new Relationship("Account_Emails"),
email);
xrmContext.SaveChanges();
late bound:
service.Associate(account.LogicalName, account.Id, new Relationship() { SchemaName = "Account_Emails" },
new EntityReferenceCollection() { new EntityReference(email.LogicalName, email.Id) });
service.Execute(request);
2. partial success - The Regarding is created, but in the UI there is no text (only an Icon, the accountname is not shown)
service.Associate(account.LogicalName, account.Id, new Relationship() { SchemaName = "Account_Emails" },
new EntityReferenceCollection() { new EntityReference(email.LogicalName, email.Id) });
service.Execute(request);
any ideas? thank you.
number 2. should be without the execute method. i tryied the execute method with an AssociateRequest as follows
ReplyDeleteAssociateRequest request = new AssociateRequest()
{
Target = new EntityReference(email.LogicalName, email.Id),
RelatedEntities = new EntityReferenceCollection
{
new EntityReference(account.LogicalName, account.Id)
},
Relationship = new Relationship()
{
SchemaName = "Account_Emails"
}
};
service.Execute(request);
You made my problem solved.
ReplyDeleteLED signs outdoor
How to test this plugin in CRM 2011
ReplyDeleteHi Manoj,
ReplyDeleteYou can register the plugin as mentioned above and can test the plugin by Tracking an Email from Outlook into Dynamics CRM. Let me know if you face any issue during testing or require any suggestions for extending the plugin code or logic.
Can you help me on the matter that i need a piece of code to strip the domain name and associate it with a known Accounts email's domain name, for example if a company is abc@company.com and another email arrives with def@company.com i want the newly created contact(using workflow on that) to be associated with the company. is it possible?
ReplyDeleteGreat post
ReplyDeleteIn this plugin we receive an information who is the sender. And when we know who is the sender, can we write this information in a customized lookup field to account entity and this field is located on email entity. I want to write the information on a lookup field to use it later in a workflow to create a case.
replica watches, christian louboutin outlet, nike air max, nike roshe run, jordan shoes, louis vuitton, longchamp outlet, michael kors, louis vuitton outlet, longchamp pas cher, nike free, louboutin pas cher, ray ban sunglasses, louboutin outlet, oakley sunglasses, longchamp outlet, air max, kate spade outlet, nike air max, uggs on sale, sac longchamp, polo ralph lauren outlet, louboutin shoes, ugg boots, tory burch outlet, prada handbags, cheap oakley sunglasses, burberry, tiffany and co, oakley sunglasses, tiffany jewelry, ray ban sunglasses, ray ban sunglasses, ralph lauren pas cher, longchamp, oakley sunglasses, louis vuitton outlet, replica watches, louis vuitton, louboutin, oakley sunglasses, ugg boots, gucci outlet, nike free, polo ralph lauren outlet, air jordan pas cher, nike outlet, prada outlet, chanel handbags, louis vuitton
ReplyDeletemichael kors, nike free run uk, ray ban pas cher, nike air max, michael kors, coach outlet, michael kors outlet, converse pas cher, north face, lululemon, nike air max, lacoste pas cher, ralph lauren uk, ugg boots, nike roshe, ugg boots, nike blazer, new balance pas cher, burberry, mulberry, ray ban uk, hogan, tn pas cher, vanessa bruno, vans pas cher, true religion jeans, replica handbags, north face, burberry outlet online, oakley pas cher, air force, hollister pas cher, michael kors outlet, coach purses, michael kors, hermes, michael kors outlet, sac guess, kate spade handbags, abercrombie and fitch, true religion jeans, michael kors, true religion outlet, nike air max, timberland, hollister, michael kors outlet, michael kors outlet, true religion jeans, coach outlet
ReplyDeletelancel, jimmy choo shoes, chi flat iron, new balance, vans, celine handbags, asics running shoes, gucci, converse, soccer jerseys, vans shoes, north face outlet, ralph lauren, reebok shoes, herve leger, nike roshe, timberland boots, ferragamo shoes, nfl jerseys, beats by dre, abercrombie and fitch, birkin bag, hollister, mac cosmetics, soccer shoes, lululemon, ghd, iphone cases, hollister, valentino shoes, longchamp, instyler, p90x workout, oakley, mcm handbags, ray ban, nike air max, babyliss, hollister, north face outlet, bottega veneta, nike trainers, insanity workout, baseball bats, louboutin, nike huarache, wedding dresses, nike air max, mont blanc, converse outlet
ReplyDeletemoncler outlet
ReplyDeletegolden goose sneakers
moncler
nike kyrie 6
golden goose
jordan shoes
supreme t shirt
giannis shoes
off white x nike
moncler coat