Newer Version Available

This content describes an older version of this product. View Latest

FindDuplicates Class

Performs rule-based searches for duplicate records. The input is an array of sObjects. Each sObject represents a record you want to find duplicates of. The output identifies the detected duplicates for each input sObject based on active duplicate rules for the given object.

Namespace

Datacloud

FindDuplicates Methods

The following are methods for FindDuplicates.

findDuplicates(sObjects)

Identifies duplicates for sObjects provided and returns a list of FindDuplicatesResult objects.

Usage

Use FindDuplicates to apply active duplicate rules associated with an object to records represented by input sObjects.

FindDuplicates uses the duplicate rules for the object that has the same type as the input sObjects.

This method doesn’t return custom fields by default. This method identifies duplicate records according to activated standard and custom matching rules. Standard matching rules don’t include custom fields in their matching criteria. You can configure custom matching rules that do include custom fields for matching criteria, and then assign the custom matching rule to a duplicate rule. However, configuring these rules isn’t a part of the Datacloud API.

Input
  • All sObjects in the input array must be of the same object type, and that type must correspond to an object type that supports duplicate rules.
  • The input array is limited to 50 elements. If you exceed this limit, an exception is thrown with the following message: Configuration error: The number of records to check is greater than the permitted batch size.
Output
  • The output of FindDuplicates is an array of objects with the same number of elements as the input array, and in the same order. The output objects encapsulate record IDs for duplicate records. The output objects also contain values from the duplicate records.
  • Each element contains an array of DuplicateResult objects, which each represent a duplicate rule that FindDuplicates applied. Within each DuplicateResult object is an array of MatchResult objects, which each represent a matching rule that the duplicate rule applied. If FindDuplicates doesn’t find any duplicates for that matching rule, then the MatchResult.getMatchRecords() array is empty. Otherwise, the MatchResult.getMatchRecords() array contains MatchRecord elements, which each represent a duplicate record.
  • If no duplicate rule is active for the object type in the input array, a System.HandledException exception is thrown with this message: No active duplicate rules are defined for the {ObjectName} object type.

Example

1//Create the sObject to check for duplicates.
2Account acct = new Account();
3acct.Name = 'Test Account 123';
4acct.BillingStreet = '123 Test Street';
5acct.BillingCity = 'San Francisco';
6acct.BillingState = 'CA';
7acct.BillingCountry = 'US'; 
8 
9List<Account> acctList = new List<Account>();
10acctList.add(acct); 
11 
12// Call the findDuplicates method, which returns one FindDuplicatesResult for each sObject in the input list.
13List<Datacloud.FindDuplicatesResult> results = Datacloud.FindDuplicates.findDuplicates(acctList);
14 
15//Get the result for the first record (index 0).
16Datacloud.FindDuplicatesResult acctResult = results[0];
17 
18// Check that findDuplicates() was successfully executed for this account.
19if (!acctResult.isSuccess()) {
20  List<Database.Error> errs = acctResult.getErrors();
21  for (Database.Error err : errs) {
22      System.debug(err.getMessage());
23  }
24} else {
25 
26  Boolean duplicatesFound = false;
27  Boolean matchError = false;
28 
29  // Iterate through the duplicate rules that were evaluated.
30  for (Datacloud.DuplicateResult dupResult : acctResult.getDuplicateResults()) {
31    // Iterate through the matching rules that were evaluated for each duplicate rule.
32    for (Datacloud.MatchResult matchResult : dupResult.getMatchResults()) {
33      // Check that getMatchResults() was successfully executed for this matching rule.
34      if (!matchResult.isSuccess()) {
35        List<Database.Error> errs = matchResult.getErrors();
36        for (Database.Error err : errs) {
37          System.debug(err.getMessage());
38        }
39        matchError = true;
40      } else {
41        // Check if duplicates are found according to the matching rule.
42        if (!matchResult.getMatchRecords().isEmpty()) {
43          System.debug('Duplicate record(s) found with matching rule: ' + matchResult.getRule());
44          duplicatesFound = true;
45          // Get information about the duplicates.
46          for (Datacloud.MatchRecord matchRecord : matchResult.getMatchRecords()) {
47            System.debug('Duplicate record: ' + matchRecord.getRecord());
48          }
49        }
50      }
51    }
52  }
53 
54  // Insert the record only if no duplicates were found and no errors occurred.
55  if (!duplicatesFound && !matchError) {
56    insert(acct);
57    System.debug('Account inserted.');
58  }
59  
60}

Signature

public static List<Datacloud.FindDuplicatesResult> findDuplicates(List<SObject> sObjects)

Parameters

sObjects
Type: List<SObject>
An array of sObjects for which you want to find duplicates.

Return Value

Type: List<FindDuplicatesResult>