Duplicate Detection Plugin Crm 2011


Hi ,

Recently I had a requirement to create Duplicate detection rule using c# (Plugin) in Account Entity, to avoid redundancy.

You can try like bellow method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//CRM SDK Namespace
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;

namespace Sample
{
    public class Duplicate_Detection : IPlugin
    {
        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);
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                var Name = entity.GetAttributeValue<string>("name");

                #region  Retrieve All Account Record

                QueryExpression Account = new QueryExpression { EntityName = entity.LogicalName, ColumnSet = new ColumnSet("name") };
                Account.Criteria.AddCondition("name", ConditionOperator.Equal, Name);
               Account.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);

               EntityCollection RetrieveAccount = service.RetrieveMultiple(Account);

                //If the retrieved Account Count Greater Than 1 , following Error Message Throw

               if (RetrieveAccount.Entities.Count > 1)
                {
                    throw new InvalidPluginExecutionException("Following Record with Same Name Exists");

                }
               else if (RetrieveAccount.Entities.Count == 0)
                {
                    return;
                }
                #endregion
            }
        }
    }
}
  • Register your plugin Message = Create
  • Primary Entity = account
  • Post Operation

Hope This Help You . . .

5 thoughts on “Duplicate Detection Plugin Crm 2011

  1. Hi,

    Nice post!!

    I have got a question for you.

    1. What will happen in case if user import records in CRM. Let’s say 2000 records. (I am sure the plugin will crash).

    Just to let you know that Duplicate detection is the OOB feature in which you can define conditions about when you want to check the duplicate conditions e.g import, record creation etc.

    I think Gib Ihm is right about the record stats. The plugin will work well in the case where user creating records but not in the case where users are going to import large amount of records.

    Regards

Leave a comment