Sub topic 14
Finding duplicates in Apex with Datacloud.FindDuplicates
This chapter assumes you know how matching rules and duplicate rules work, and that you can read basic Apex and SOQL. You will learn how to ask Salesforce whether an unsaved record has duplicates, and how to read the answer.
What the method does
Datacloud.FindDuplicates.findDuplicates takes a list of records that have not been saved. It runs the active duplicate rules for each record's object and returns the matching records. It does not insert anything, so you can use it for a pre-check before a DML statement, a custom "check for duplicates" button, or an integration that needs to decide whether to create or update.
Two points matter before you write code:
- Only active duplicate rules run. If no duplicate rule is active for the object, the result is empty.
- The results follow the rules' matching rules, not a query you write. To change what counts as a match, change the matching rule.
Worked example
Contact candidate = new Contact(
FirstName = 'Jon',
LastName = 'Smith',
Email = '[email protected]'
);
List<Datacloud.FindDuplicatesResult> results =
Datacloud.FindDuplicates.findDuplicates(new List<Contact>{ candidate });
for (Datacloud.FindDuplicatesResult findResult : results) {
for (Datacloud.DuplicateResult dupResult : findResult.getDuplicateResults()) {
for (Datacloud.MatchResult matchResult : dupResult.getMatchResults()) {
for (Datacloud.MatchRecord matchRecord : matchResult.getMatchRecords()) {
System.debug('Possible duplicate: ' + matchRecord.getRecord());
}
}
}
}Reading the result
The result is nested four levels deep, which is why the code has four loops.
| Level | Type | Meaning |
|---|---|---|
| 1 | `FindDuplicatesResult` | One entry for each record you passed in, in the same order. |
| 2 | `DuplicateResult` | One entry for each duplicate rule that ran for that record. |
| 3 | `MatchResult` | The outcome of one matching rule used by that duplicate rule. |
| 4 | `MatchRecord` | One existing record that matched. |
Call matchRecord.getRecord() to get the matched SObject. The record contains only the fields the matching rule needs, so query it again if you need other fields.
Common errors
- Expecting a match with no active rule. The method relies on duplicate rules. Activate one for the object first.
- Ignoring rule conditions. A duplicate rule with conditions or sharing settings can skip a record. Test with a user whose access matches production.
- Calling it inside a loop. Pass a list of records in one call. Per-record calls use up governor limits faster.
- Assuming the action matters. The rule's action (allow, block, alert) does not stop this method. It returns matches whatever the action is. Your code decides what to do next.
Exercise
You pass three unsaved Contacts to findDuplicates, and the object has one active duplicate rule that uses one matching rule. How many FindDuplicatesResult entries come back, and how many DuplicateResult entries does each one hold?
Show answer
Three FindDuplicatesResult entries come back, one for each input record. Each holds one DuplicateResult, because only one duplicate rule ran for it.