web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
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,064 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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

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

#1
Travis South Profile Picture

Travis South 54

#2
Syed Aqib Raza Profile Picture

Syed Aqib Raza 42

#3
ParthPatel249 Profile Picture

ParthPatel249 33

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans