Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Suggested answer

Prevent duplicates record creation off contact via plugins

(0) ShareShare
ReportReport
Posted on by 140

Hey yall I have this plugin to prevent the user from creating contact dupes but when i try  to register the assembly using the dll I get this error could you guys help I am not sure what this error means

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;

namespace DuplicateDetection
{
    public class DetectDuplicates : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            IOrganizationServiceFactory servicefactory =
             (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service =
            servicefactory.CreateOrganizationService(context.UserId);

            #region working code
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                if (entity.LogicalName != "contact")
                {
                    return;
                }
                if (context.Depth > 1)
                {
                    return;
                }
                try
                {
                    if (entity.Attributes.Contains("emailaddress1"))
                    {
                        string email = entity.GetAttributeValue("emailaddress1").ToString();
                        //handle the above line of code accordingly based on dataType(String, EntityReference, Datetime etc.)
                        QueryExpression contactQuery = new QueryExpression("contact");
                        contactQuery.ColumnSet = new ColumnSet("emailaddress1");
                        contactQuery.Criteria.AddCondition("emailaddress1", ConditionOperator.Equal, email);
                        EntityCollection contactColl = service.RetrieveMultiple(contactQuery);
                        if (contactColl.Entities.Count > 0)
                        {
                            throw new Exception("Duplicates found !!!");
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }
            #endregion
        }
    }
}

  • Suggested answer
    Nya Profile Picture
    29,060 on at

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 51 Most Valuable Professional

#2
Ramesh Kumar Profile Picture

Ramesh Kumar 42

#3
David Shaw_UK Profile Picture

David Shaw_UK 27

Featured topics

Product updates

Dynamics 365 release plans