Duplicate detection in Microsoft Dynamics CRM Using a generic Plugin

Hi all,

Lot of times I used to write different plugins for duplicate detection,but got a thought of writing a generic plugin by passing the the parameters to check duplicates from Un Secure Config in Parameters.

Read the parameters passed from Un Secure Configurations:

I used a generic List for reading the parameters

List<string> parameters=new List<string>();

public CheckDuplicate(string unSecureConfig)
{
foreach (var item in unSecureConfig.Split(‘|’))
{
parameters.Add(item);
}
}

Design Query Expression:

Generic Query Expression is designed in this way

private QueryExpression getQueryExpression(Entity entity, string entityLogicalName, List<string> parameters)
{
QueryExpression query = new QueryExpression();
query.EntityName = entityLogicalName;
query.ColumnSet = new ColumnSet(true);
FilterExpression ft = new FilterExpression();
ft.FilterOperator = LogicalOperator.And;
foreach (var item in parameters)
{
if(entity.Contains(item))
{
if((entity.Attributes[item].GetType().Equals(typeof(Microsoft.Xrm.Sdk.OptionSetValue))) && ((OptionSetValue)entity.Attributes[item]).Value!=null)
{
int attributeValue=((OptionSetValue)entity.Attributes[item]).Value;
ft.AddCondition(new ConditionExpression(item, ConditionOperator.Equal, attributeValue));
}
else if((entity.Attributes[item].GetType().Equals(typeof(Microsoft.Xrm.Sdk.EntityReference))) && ((EntityReference)entity.Attributes[item]).Id!=null)
{
Guid attributeValue = ((EntityReference)entity.Attributes[item]).Id;
ft.AddCondition(new ConditionExpression(item, ConditionOperator.Equal, attributeValue));
}
else if((entity.Attributes[item].GetType().Equals(typeof(Microsoft.Xrm.Sdk.Money))) && ((Money)entity.Attributes[item]).Value!=null)
{
decimal attributeValue = ((Money)entity.Attributes[item]).Value;
ft.AddCondition(new ConditionExpression(item, ConditionOperator.Equal, attributeValue));
}
else if(entity.Attributes[item].ToString()!=null)
{
string attributeValue = entity.Attributes[item].ToString();
ft.AddCondition(new ConditionExpression(item, ConditionOperator.Equal, attributeValue));
}
}
}
query.Criteria = ft;
return query;

}
}

 

Use this Query Expression to retrieve duplicate records present in the system

Complete Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using System.Runtime.Serialization;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;

namespace duplicateDetection
{
public class CheckDuplicate:IPlugin
{
List<string> parameters = new List<string>();
public CheckDuplicate(string unSecureConfig)
{
foreach (var item in unSecureConfig.Split(‘|’))
{
parameters.Add(item);
}
}
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if(context.InputParameters.Contains(“Target”) && context.InputParameters[“Target”] is Entity)
{
Entity entity = (Entity)context.InputParameters[“Target”];
string entityLogicalName = entity.LogicalName;
QueryExpression query = getQueryExpression(entity, entityLogicalName, parameters);
EntityCollection duplicates = service.RetrieveMultiple(query);
if(duplicates.Entities.Count>0)
{
throw new InvalidPluginExecutionException(“This record cannot be created since there are duplicates available in the system”);
}
}
}

private QueryExpression getQueryExpression(Entity entity, string entityLogicalName, List<string> parameters)
{
QueryExpression query = new QueryExpression();
query.EntityName = entityLogicalName;
query.ColumnSet = new ColumnSet(true);
FilterExpression ft = new FilterExpression();
ft.FilterOperator = LogicalOperator.And;
foreach (var item in parameters)
{
if(entity.Contains(item))
{
if((entity.Attributes[item].GetType().Equals(typeof(Microsoft.Xrm.Sdk.OptionSetValue))) && ((OptionSetValue)entity.Attributes[item]).Value!=null)
{
int attributeValue=((OptionSetValue)entity.Attributes[item]).Value;
ft.AddCondition(new ConditionExpression(item, ConditionOperator.Equal, attributeValue));
}
else if((entity.Attributes[item].GetType().Equals(typeof(Microsoft.Xrm.Sdk.EntityReference))) && ((EntityReference)entity.Attributes[item]).Id!=null)
{
Guid attributeValue = ((EntityReference)entity.Attributes[item]).Id;
ft.AddCondition(new ConditionExpression(item, ConditionOperator.Equal, attributeValue));
}
else if((entity.Attributes[item].GetType().Equals(typeof(Microsoft.Xrm.Sdk.Money))) && ((Money)entity.Attributes[item]).Value!=null)
{
decimal attributeValue = ((Money)entity.Attributes[item]).Value;
ft.AddCondition(new ConditionExpression(item, ConditionOperator.Equal, attributeValue));
}
else if(entity.Attributes[item].ToString()!=null)
{
string attributeValue = entity.Attributes[item].ToString();
ft.AddCondition(new ConditionExpression(item, ConditionOperator.Equal, attributeValue));
}
}
}
query.Criteria = ft;
return query;

}
}
}

Register the plugin in Pre-Operation of create and pass the parameters from Un secure configuration

Hope this helps 🙂

 

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