duplicate account name plugin RRS feed

  • Question

  • Hi,


    I am new to crm. How to Write a plugin on Account entity .

    It should check if there is any other account already existing with the same name. System should not allow the creation of duplicate accounts with the same name.
    Thursday, March 6, 2014 6:30 AM

All replies

  •  register plugin

    on create , account, preoperation , sync mode.

    try this code.....

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using System.Runtime.Serialization;
    using Microsoft.Xrm.Sdk.Query;
    namespace HtmlcollectionCreate
        public class Class1 : IPlugin
            public void Execute(IServiceProvider serviceprovider)
                IPluginExecutionContext context = (IPluginExecutionContext)serviceprovider.GetService(typeof(IPluginExecutionContext));
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                    Entity entity = (Entity)context.InputParameters["Target"];
                    IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceprovider.GetService(typeof(IOrganizationServiceFactory));
                    ITracingService trace=(ITracingService)serviceprovider.GetService(typeof(ITracingService));
                    IOrganizationService service = factory.CreateOrganizationService(context.UserId);
                    OrganizationServiceContext orgcontext = new OrganizationServiceContext(service);
                    if (entity.Attributes.Contains("name"))

                        int count = getcount(service, entity.Attributes["name"].ToString(), trace);
                       if (count > 1)
                           throw new InvalidPluginExecutionException("Record with name : " + entity.Attributes["name"].ToString() + " allready exist, Duplicate data not allowed.");

            public int getcount(IOrganizationService service, string name, ITracingService trace)
                int count = 0;
               string query="<fetch mapping='logical' aggregate='true' version='1.0'>"+
        "<entity name='account'>"+
            "<attribute name='accountid' aggregate='count' alias='count' />"+
                "<condition attribute='name' operator='eq' value='"+name+"' />"+
               EntityCollection result = service.RetrieveMultiple(new FetchExpression(query));
               if (result.Entities.Count > 0)
                   Entity myEntity = (Entity)result.Entities[0];
                    var keys = myEntity.Attributes.Keys;
                    if (myEntity.Attributes[keys.First()].ToString() == "Microsoft.Xrm.Sdk.AliasedValue")
                         count = Convert.ToInt32(((Microsoft.Xrm.Sdk.AliasedValue)myEntity.Attributes[keys.First()]).Value.ToString());

                return count;


    Hope this helps. ----------------------------------------------------------------------- Santosh Bhagat If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"

    Thursday, March 6, 2014 6:53 AM
  • Pavandh, Jason Lattimer wrote a Duplicate Detection solution that might be of some help (https://crm2103dupedetect.codeplex.com/). Writing a duplicate detection plugin isn't that hard, however explaining it to someone using nothing but text is probably even harder than writing the plugin itself.

    If you're still determined:



    Thursday, March 6, 2014 6:55 AM