C# Code · Dynamics CRM Duplicate Detection

Dynamics 365 CRM Apply OOB Duplicate Detection while creating/Updating record programatically

While creating/updating record in CRM Dynamics from C# code which could be from Plugins, Console Application, Azure function and so on using Iorganization service, We may want to apply OOB Duplicate Detection rules which is published from CRM UI.

So to achieve this we will have to use CreateRequest and UpdateRequest from C#. I was able to Create/Update records with _service.Create(EntityObject) and Web API POST without any exception even if OOB Duplicate Detection was enabled.

Pass the duplicate detection optional parameter SuppressDuplicateDetection by adding a value to the Parameters property of the CreateRequest and UpdateRequest message requests. The SuppressDuplicateDetection parameter value determines whether the Create or Update operation can be completed:

  • true – Create or update the record, if a duplicate is found.
  • false – Do not create or update the record, if a duplicate is found.

Note

If the duplicate detection optional parameter is set to false and a duplicate is found, an exception is thrown and the record is not created or updated.

The following example shows how to pass the duplicate detection option as a part of the CreateRequest and UpdateRequest message requests:


// Create and account record with the named Proseware, Inc. and already existing Account Number.
    Account account = new Account 
    {
        Name = "Proseware, Inc.",
        AccountNumber = "ACC005"
    };

    // Create operation by suppressing duplicate detection
    CreateRequest reqCreate = new CreateRequest();
    reqCreate.Target = account;
    reqCreate.Parameters.Add("SuppressDuplicateDetection", true); // Change to false to activate the duplicate detection.
    CreateResponse createResponse = (CreateResponse)_service.Execute(reqCreate);
    _dupAccountId = createResponse.id;
    Console.Write("Account: {0} {1} created with SuppressDuplicateDetection to true, ", 
        account.Name, account.AccountNumber);

    // Retrieve the account containing with its few attributes.
    ColumnSet cols = new ColumnSet(
        new String[] { "name", "accountnumber"});

    Account retrievedAccount = (Account)_service.Retrieve("account", _dupAccountId, cols);
    Console.Write("retrieved, ");

    // Update the existing account with new account number.
    retrievedAccount.AccountNumber = "ACC006";                   

    // Update operation – update record, if a duplicate is not found.
    UpdateRequest reqUpdate = new UpdateRequest();
    reqUpdate.Target = retrievedAccount;
    reqUpdate["SuppressDuplicateDetection"] = false; // Duplicate detection is activated.

    // Update the account record.
    UpdateResponse updateResponse = (UpdateResponse)_service.Execute(reqUpdate);
    Console.WriteLine("and updated.");

 

Hope this Helps!

 

2 thoughts on “Dynamics 365 CRM Apply OOB Duplicate Detection while creating/Updating record programatically

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s