Skip to main content

Notifications

Announcements

No record found.

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 !

  • Suggested answer
    RE: Duplicate deduction on contact

    Thank you so much Steve!!

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Duplicate deduction on contact

    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;
                    }
                }
            }
        }
    }
    

  • RE: Duplicate deduction on contact

    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
    Community Member Microsoft Employee on at
    RE: Duplicate deduction on contact

    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.

  • RE: Duplicate deduction on contact

    Hi Steve ,

    I need to use plugin .

    Thank you

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Duplicate deduction on contact

    Hi Beginner Developer,

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

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,661 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,379 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans