Common duplicate detection rule

dup

In MS CRM we have duplicate detection rule to detect duplicate entity records, but it will give you a simple popup (ref below screen), but it not stops you to create duplicate.

But many times we needs a hard stop for duplicate, we can achieve this by writing simple plugin, that will be reusable for any entity. Here is the sample of it

Note: I assuming that name is unique here. but it can modified with some other or more attributes

namespace softnovation.Plugin
{
 public class DuplicateRetrict : BasePlugin
 {
 IOrganizationService service;
 public override void OnExecute(PluginExecution exeObject)
 {
 //'Get service reference
 service = exeObject.orgService;
 exeObject.tracingService.Trace("Duplicate Retrict started..");

 // The InputParameters collection contains all the data passed in the message request.
 if (exeObject.pluginContext.InputParameters.Contains("Target") && exeObject.pluginContext.InputParameters["Target"] is Entity)
 {
 // Obtain the target entity from the input parameters.
 Entity entity = (Entity)exeObject.pluginContext.InputParameters["Target"];

 exeObject.tracingService.Trace("DuplicateRetrict: Check dulicate '" + entity.LogicalName + "' record.");

 string entityName = GetEntityDisplayName(entity.LogicalName);

 //check on create
 if (exeObject.pluginContext.MessageName == "Create" || exeObject.pluginContext.MessageName == "Update")
 {
 //Dont allow to create record with same name
 if (IsDataExist(entity))
 AlertMessage(entity, entityName);
 } 
 else
 return;
 }
 }

 /// <summary>
 /// Message for user for duplicate data 
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="entityName"></param>
 private static void AlertMessage(Entity entity, string entityName)
 {
 if (entity.LogicalName == "soft_vijay")
 throw new InvalidPluginExecutionException
 ("The '" + entityName + "' for the respective Product or Product-Module combination is already available. If problem persists, please contact System Administrator.");
 else
 {
 throw new InvalidPluginExecutionException
 ("The '" + entityName + "' for the respective combination is already available. If problem persists, please contact System Administrator.");
 }
 }
 

 /// <summary>
 /// check duplicate data
 /// </summary>
 /// <param name="service"></param>
 /// <param name="SOR"></param>
 /// <returns></returns>
 public bool IsDataExist(Entity entity)
 { 
 QueryExpression query = new QueryExpression(entity.LogicalName); 
 query.ColumnSet = new ColumnSet(true);
 
 query.Criteria.AddCondition("soft_name", ConditionOperator.Equal, entity.Attributes["soft_name"].ToString());
 
 EntityCollection DataCollection = service.RetrieveMultiple(query);

 if (DataCollection.Entities.Count > 0)
 return true;
 else
 return false;
 }

 /// <summary>
 /// get entity name
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public string GetEntityDisplayName(string entityLogicalName)
 {

 RetrieveEntityRequest request = new RetrieveEntityRequest { EntityFilters = EntityFilters.Attributes, LogicalName = entityLogicalName };

 RetrieveEntityResponse response = (RetrieveEntityResponse)service.Execute(request);

 return response.EntityMetadata.DisplayName.LocalizedLabels[0].Label.ToString();

 }

 }
}


//reusable common plugin part
namespace Softnovation.Plugin
{
 public abstract class BasePlugin : IPlugin
 {
 public void Execute(IServiceProvider serviceProvider)
 {
 PluginExecution obj = new PluginExecution();
 obj.pluginContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
 IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
 obj.orgService = serviceFactory.CreateOrganizationService(obj.pluginContext.UserId);
 obj.tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
 OnExecute(obj);
 }
 public abstract void OnExecute(PluginExecution exeObject);
 }
 public class PluginExecution
 {
 public IOrganizationService orgService { get; set; }
 public ITracingService tracingService { get; set; }
 public IPluginExecutionContext pluginContext { get; set; }
 }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s