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 CRM (Archived)

Duplicate Contact? (C#/SDK question)

(0) ShareShare
ReportReport
Posted on by

I want to accomplish this feat in programming in C# for CRM.

  1. Check if the contact exists, if it does not, then create the contact. 
  2. The conflict I'm facing is if the contact exists, only the last name is a required field which makes checking the data challenging and there may be more than 1 contact that might be erroneously entered.

Here's my code, I can only figure out how to create a GUID using the last name but Not on more than one field. Can someone shed some light on this and provide your thoughts from those who has more experience in this field?

How can I check on more than just the last name? Perhaps 'lastname' , 'firstname' , and 'Company'?

Thanks in advance!

string firstName= "John";
string lastName = "Doe";

 var ContactValue = GetEntityCollection(_service, "contact", "lastname", lastName, new ColumnSet("contactid", "lastname")); //does it return anything
                                Guid contactpersonid = (Guid)ContactValue[0].Id;
                                salesorder["neu_orderedbyid"] = new EntityReference("contact", new Guid(contactpersonid.ToString()));

*This post is locked for comments

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

    bump

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Please provide code for GetEntityCollection method.

  • Suggested answer
    ThomasN Profile Picture
    3,190 on at

    Hi Tekwise, Thank you for reaching out.

    Why not use the existing duplicate detection rules in CRM? You can access those through the SDK and do something with the results like display error or ask for more information.

    Let me know if this is something that might work? I am eager to help.

  • Community Member Profile Picture
    on at

    [quote user="Andrii Butenko"]

    Hello,

    Please provide code for GetEntityCollection method.

    [/quote]

     private static EntityCollection GetEntityCollection(IOrganizationService service, string entityName, string attributeName, string attributeValue, ColumnSet cols)
            {
                QueryExpression query = new QueryExpression
                {
                    EntityName = entityName,
                    ColumnSet = cols,
                    Criteria = new FilterExpression
                    {
                        Conditions =
                                    {
                                    new ConditionExpression
                                    {
                                    AttributeName = attributeName,
                                    Operator = ConditionOperator.Equal,
                                    Values = { attributeValue }
                                    }
                                    }
                    }
                };
                return service.RetrieveMultiple(query);
            }


  • Community Member Profile Picture
    on at

    [quote user="ThomasN"]

    Hi Tekwise, Thank you for reaching out.

    Why not use the existing duplicate detection rules in CRM? You can access those through the SDK and do something with the results like display error or ask for more information.

    Let me know if this is something that might work? I am eager to help.

    [/quote]

    Hi Tom - how can I get started? Can you point me in the right direction? Thanks

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    You can use following code for searching my multiple fields:

    private static EntityCollection GetEntityCollection(IOrganizationService service, string entityName, string[] attributes, object[] attributeValues, ColumnSet cols)
            {
    		QueryByAttribute query = new QueryByAttribute(entityName)
    		{
    			ColumnsSet = cols
    		};
    
    		for(var i = 0; i < attributes.length; i++)
    		{
    			query.AddAttributeValue(attributes[i], attributeValues[i]);
    		}
    
    		return service.RetrieveMultiple(query);
            }


    Here is usage sample based on your code:

    string firstName= "John";
    string lastName = "Doe";
     
     var ContactValue = GetEntityCollection(_service, "contact", new string[] { "firstname", "lastname" }, new object[] { firstName, lastName }, new ColumnSet("contactid", "lastname")); //does it return anything


  • Verified answer
    ThomasN Profile Picture
    3,190 on at

    Hi Tekwise,

    First you will need to setup Duplicate Detection Rules in Settings > Data Management > Duplicate Detection Rules. Those rules are an entity that you can retrieve records from that would indicate duplicates.

    You can then use "Retrieve" Method to pull back the duplicates found during creation (Add) of that record. Using the CHM in the SDK was big help working through this. Below is the link to the TechNet article on this as well.

    https://msdn.microsoft.com/en-us/library/hh210213(v=crm.7).aspx

    May not fit your scenario Andrii provided a great sample as well.

    Let me know if this works.

  • Community Member Profile Picture
    on at

    I tried this but I get an error once it gets to this part. It cannot create a guid using the combination?

    "error message: Index was out of range. Must be non-negative and less than the size of the collection."

    var ContactValue = GetEntityMultipleCollections(_service, "contact", new string[] { "firstname", "lastname" }, new object[] { firstName, lastName },
    new ColumnSet("contactid", "lastname")); //does it return anything 

    Guid contactpersonid = (Guid)ContactValue[0].Id;

  • Community Member Profile Picture
    on at

    [quote user="ThomasN"]

    Hi Tekwise,

    First you will need to setup Duplicate Detection Rules in Settings > Data Management > Duplicate Detection Rules. Those rules are an entity that you can retrieve records from that would indicate duplicates.

    You can then use "Retrieve" Method to pull back the duplicates found during creation (Add) of that record. Using the CHM in the SDK was big help working through this. Below is the link to the TechNet article on this as well.

    https://msdn.microsoft.com/en-us/library/hh210213(v=crm.7).aspx

    May not fit your scenario Andrii provided a great sample as well.

    Let me know if this works.

    [/quote]

    Thanks - ill check this out!

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    That means that no contacts were returned based on your criteria.

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans