Monday, August 22, 2011

Dynamics CRM 2011 : Execute Custom Business Logic in Plugin on Incomming Email Tracking


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);
            }
        }
    }
}


11 comments:

  1. 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:

    1. 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.

    ReplyDelete
  2. number 2. should be without the execute method. i tryied the execute method with an AssociateRequest as follows

    AssociateRequest 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);

    ReplyDelete
  3. How to test this plugin in CRM 2011

    ReplyDelete
  4. Hi Manoj,

    You 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.

    ReplyDelete
  5. 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?

    ReplyDelete
  6. Great post

    In 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.

    ReplyDelete