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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

Duplicate deduction on contact

(0) ShareShare
ReportReport
Posted on by 55

Hello everyone,

I have requirement to write plugin that check duplicate contact on creation of some custom record .

scenario is when some x record created with name ,email id and contact based on this value need to check existing contact, if present then check related lead for contact and update, if no contact then create contact and lead based on x record value .

Can any one help on this how please.

Thank you in advance !

I have the same question (0)
  • Community Member Profile Picture
    on at

    Hi Beginner Developer,

    Do you mind using Power Automate? What did you do and where are you stuck?

  • Beginner Developer Profile Picture
    55 on at

    Hi Steve ,

    I need to use plugin .

    Thank you

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Beginner Developer,

    I'm not sure where you are stuck. However, this is my test code to check existing contact:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    
    namespace BasicPlugin
    {
        public class FollowupPlugin : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                // Obtain the tracing service
                ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                // Obtain the execution context from the service provider.  
                IPluginExecutionContext context = (IPluginExecutionContext)
                    serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                // The InputParameters collection contains all the data passed in the message request.  
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parameters.  
                    Entity entity = (Entity)context.InputParameters["Target"];
    
                    // Obtain the organization service reference which you will need for  
                    // web service calls.  
                    IOrganizationServiceFactory serviceFactory =
                        (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                    try
                    {
                        if (entity.LogicalName == "cr32a_person")
                        {
                            string email = entity.GetAttributeValue("cr32a_email");
                            string firstName = entity.GetAttributeValue("cr32a_firstName");
                            string lastName = entity.GetAttributeValue("cr32a_lastname");
    
                            var contactQuery = new QueryExpression
                            {
                                EntityName = "contact",
                                ColumnSet = new ColumnSet("contactid"),
                                Criteria = new FilterExpression
                                {
                                    Conditions =
                                    {
                                        new ConditionExpression
                                        {
                                            AttributeName = "emailaddress1",
                                            Operator = ConditionOperator.Equal,
                                            Values = { email }
                                        },
                                        new ConditionExpression
                                        {
                                            AttributeName = "firstname",
                                            Operator = ConditionOperator.Equal,
                                            Values = { firstName }
                                        },
                                        new ConditionExpression
                                        {
                                            AttributeName = "lastname",
                                            Operator = ConditionOperator.Equal,
                                            Values = { lastName }
                                        }
                                    }
                                }
                            };
    
                            var contacts = service.RetrieveMultiple(contactQuery).Entities;
                            if (null != contacts) { 
                                if (contacts.Count > 0)
                                {
                                    //update lead
                                }else if(contacts.Count == 0)
                                {
                                    //create contact and lead
                                }
                            }
                        }
                    }
    
                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
                    }
    
                    catch (Exception ex)
                    {
                        tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
                        throw;
                    }
                }
            }
        }
    }

    You could have a try. If you tell me where you are stuck, I would provide further help.

  • Beginner Developer Profile Picture
    55 on at

    Thank You Steve!

    This was really helpful ,i need to check even existing lead with the contact details if contact found .

    this works great !!

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Beginner Developer,

    Please try this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    
    namespace CheckContact
    {
        public class checkContact : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
    
                // Obtain the tracing service
                ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                // Obtain the execution context from the service provider.  
                IPluginExecutionContext context = (IPluginExecutionContext)
                    serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                // The InputParameters collection contains all the data passed in the message request.  
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parameters.  
                    Entity entity = (Entity)context.InputParameters["Target"];
    
                    // Obtain the organization service reference which you will need for  
                    // web service calls.  
                    IOrganizationServiceFactory serviceFactory =
                        (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                    try
                    {
                        if (entity.LogicalName == "cr32a_person")
                        {
                            string email = entity.GetAttributeValue("cr32a_email");
                            string firstName = entity.GetAttributeValue("cr32a_firstname");
                            string lastName = entity.GetAttributeValue("cr32a_lastname");
    
                            var contactQuery = new QueryExpression
                            {
                                EntityName = "contact",
                                ColumnSet = new ColumnSet(true),
                                Criteria = new FilterExpression
                                {
                                    Conditions =
                                    {
                                        new ConditionExpression
                                        {
                                            AttributeName = "emailaddress1",
                                            Operator = ConditionOperator.Equal,
                                            Values = { email }
                                        }
                                        ,
                                        new ConditionExpression
                                        {
                                            AttributeName = "firstname",
                                            Operator = ConditionOperator.Equal,
                                            Values = { firstName }
                                        },
                                        new ConditionExpression
                                        {
                                            AttributeName = "lastname",
                                            Operator = ConditionOperator.Equal,
                                            Values = { lastName }
                                        }
                                    }
                                }
                            };
    
                            var contacts = service.RetrieveMultiple(contactQuery).Entities;
                            if (null != contacts)
                            {
                                if (contacts.Count > 0)
                                {
                                    foreach (var contact in contacts)
                                    {
                                        var leadQuery = new QueryExpression
                                        {
                                            EntityName = "lead",
                                            ColumnSet = new ColumnSet(true),
                                            Criteria = new FilterExpression
                                            {
                                                Conditions =
                                                {
                                                    new ConditionExpression
                                                    {
                                                        AttributeName = "parentcontactid",
                                                        Operator = ConditionOperator.Equal,
                                                        Values = { contact.GetAttributeValue("contactid") }
                                                    }
                                                }
                                            }
                                            
                                        };
                                        var leads = service.RetrieveMultiple(leadQuery).Entities;
                                        Console.WriteLine(leads.Count);
                                    }
                                }
                                else if (contacts.Count == 0)
                                {
                                    //create contact and lead
                                }
                            }
                        }
                    }
                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
                    }
    
                    catch (Exception ex)
                    {
                        tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
                        throw;
                    }
                }
            }
        }
    }
    

  • Suggested answer
    Beginner Developer Profile Picture
    55 on at

    Thank you so much Steve!!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
#ManoVerse Profile Picture

#ManoVerse 101

#2
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 62 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans