編輯

共用方式為


Detect duplicate data using the SDK for .NET

The Microsoft Dataverse SDK for .NET allows you to detect duplicate rows to maintain integrity of data. For detailed information about detecting duplicate data using code, see Detect duplicate data using code.

Note

Make sure there are appropriate duplicate detection rules in place. Dataverse includes default duplicate detection rules for accounts, contacts, and leads, but not for other types of rows. If you want the system to detect duplicates for other row types, you’ll need to create a new rule.
- For information on how to create a duplicate detection rule using the UI, see Set up duplicate detection rules to keep your data clean.
- For information on creating duplicate detection rules using code, see Duplicate rule tables

Use RetrieveDuplicatesRequest message to detect duplicates before you create or update row

You can programmatically check whether a table row is a duplicate or will be a duplicate before creating or updating it by using the RetrieveDuplicatesRequest class.

var account = new Account();
account.Name = "Sample Account";

var request = new RetrieveDuplicatesRequest()
{
    BusinessEntity = account,
    MatchingEntityName = account.LogicalName,
    PagingInfo = new PagingInfo() { PageNumber = 1, Count = 50 }
};

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

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

Use SuppressDuplicateDetection parameter to throw errors when you create or update row

If you want to have the platform throw an error when a new row you create is determined to be a duplicate row, or you update an existing row so that duplicate detection rules will be evaluated, you must use the CreateRequest or UpdateRequest classes with the IOrganizationService.Execute method and apply the SuppressDuplicateDetection parameter set to false.

The following code will throw an InvalidOperationException exception with the message A record was not created or updated because a duplicate of the current record already exists. when the following are true:

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

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(ex.Detail.Message);
        default:
            throw ex;
    }
}

See also

Detect duplicate data using the Web API