locked
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);
                    trace.Trace("started");
                    if (entity.Attributes.Contains("name"))
                    {

                        int count = getcount(service, entity.Attributes["name"].ToString(), trace);
                       trace.Trace(count.ToString());
                       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' />"+
            "<filter>"+
                "<condition attribute='name' operator='eq' value='"+name+"' />"+
                   "</filter>"+
        "</entity>"+
    "</fetch>";
               trace.Trace(query);
               EntityCollection result = service.RetrieveMultiple(new FetchExpression(query));
               if (result.Entities.Count > 0)
               {
                   trace.Trace(result.Entities.Count.ToString());
                   Entity myEntity = (Entity)result.Entities[0];
                    var keys = myEntity.Attributes.Keys;
                    trace.Trace(myEntity.Attributes[keys.First()].ToString());
                    if (myEntity.Attributes[keys.First()].ToString() == "Microsoft.Xrm.Sdk.AliasedValue")
                     {
                         trace.Trace(myEntity.Attributes[keys.First()].ToString());
                         count = Convert.ToInt32(((Microsoft.Xrm.Sdk.AliasedValue)myEntity.Attributes[keys.First()]).Value.ToString());
                         trace.Trace(count.ToString());
                     }

               }
                   trace.Trace(result.Entities.Count.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:

    http://msdn.microsoft.com/en-us/library/dn481588(v=crm.6).aspx

    http://msdn.microsoft.com/en-us/library/gg327941.aspx

    Thursday, March 6, 2014 6:55 AM