CRM Crate

How to identify duplicate rows using the SDK for .NET in Power Apps?

We will learn to identify duplicate rows using the Web APIs in Power Apps / Dynamics 365. Before we start, make sure to subscribe to CRM Crate so that you can stay up to date in the field of Power Platform.

How to identify duplicate rows using the SDK for .NET in Power Apps?

To uphold the integrity of your data, setting up guidelines to minimize the occurrence of duplicate records or rows within the system is advisable. While model-driven apps and customer engagement apps like Dynamics 365 Sales and Dynamics 365 Customer Service come with default duplicate detection rules for accounts and contacts, other record types lack such defaults. To enable duplicate detection for additional record types, it’s necessary to create new duplicate detection rules.
You can create new duplicate detection rules in your environment (Settings > Data management > Duplicate detection rules) as given below.

Below will understand to identify duplicate table rows using SDK for .NET in Power Apps.


Using RetrieveDuplicatesRequest for detecting duplicate rows in Dataverse

We will leverage the RetrieveDuplicatesRequest message to identify duplicate data before creating or updating rows in Dataverse. You have the ability to programmatically verify whether a table row is already a duplicate or will become one prior to its creation or update. This can be achieved by utilizing the RetrieveDuplicatesRequest class as given below.

RetrieveDuplicatesRequest Class

Contains the data that is needed to detect and retrieve duplicates for a specified record.
var account = new Account();
account.Name = "Prashant Tirlotkar";

var request = new RetrieveDuplicatesRequest()
{
    BusinessEntity = account, //Schema name of your table
    MatchingEntityName = account.LogicalName, //Schema name of your field
    PagingInfo = new PagingInfo() { PageNumber = 1, Count = 50 } //Paging information for displaying number of duplicate rows 
};

var response = (RetrieveDuplicatesResponse)svc.Execute(request);

if (response.DuplicateCollection.Entities.Count >= 1)
{
    Console.WriteLine("{0} CRM Crate -  Duplicate rows found = .", response.DuplicateCollection.Entities.Count);
}

Using SuppressDuplicateDetection parameter for throwing on detection of duplicate rows in Dataverse

If you want the Dynamics 365 / Power Apps to raise an error when a newly created row is identified as a duplicate, or when updating an existing row triggers the evaluation of duplicate detection rules, you must utilize the CreateRequest or UpdateRequest classes along with the IOrganizationService.Execute method. Ensure that the SuppressDuplicateDetection parameter is set to false in this process.

The below given code will throw an error for the following conditions:

  1. There is an existing account with the name “Prashant Tirlotkar”.
  2. Duplicate Detection is enabled for the environment when a row is created or updated.
  3. The account table has duplicate detection enabled.
  4. A Duplicate Detection Rule is published that checks whether the account name value is an exact match for an existing row
var account = new Account();
account.Name = "Prashant Tirlotkar";

var request = new CreateRequest();
request.Target = account;
request.Parameters.Add("SuppressDuplicateDetection", false);

try
{
    svc.Execute(request);
}
catch (FaultException<OrganizationServiceFault> ex)
{
    switch (ex.Detail.ErrorCode)
    {
        case -2147220685:
            throw new InvalidOperationException("Duplicate Detection Message - " + ex.Detail.Message);
        default:
            throw ex;
    }
}

Using SuppressDuplicateDetection parameter for bypassing or ignoring the duplicate detection rule in Power Apps

By default, duplicate detection is suppressed when you are creating or updating a record using Web API. Additionally you can use the SuppressDuplicateDetection parameter to bypass / ignore the duplicate detection rule in CreateRequest & UpdateRequest. The following .NET code demonstrates bypassing of the duplicate detection while creating a new row in Dataverse.

Entity target = new Entity("account");
target["name"] = "Prashant Tirlotkar";
CreateRequest req = new CreateRequest();
req.Target = target;
req["SuppressDuplicateDetection"] = true; // This setting will suppress or bypass the duplicate detection.
CreateResponse response = (CreateResponse)_service.Execute(req); 

5 1 vote
Article Rating
Subscribe
Notify of
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Create a Virtual Table in Power Apps using OData v4 Data Provider [Complete Guide] - CRM Crate
3 months ago

[…] driven by the OData v4 Data Provider, offer real-time access, eliminating the need for data duplication and enhancing cost efficiency. While providing a dynamic and centralized approach to data […]

error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.