Sub topic 16
Testing and troubleshooting rules
This chapter assumes you have built at least one matching rule and one duplicate rule, and that you can run anonymous Apex from the Developer Console. The goal is to find out why a record was flagged as a duplicate, or why it was not.
Test with realistic records
Create a known pair of records before you change any rule. For a Contact rule that matches on email, save [email protected]. Then try to save a second Contact with the same email, and again with a variant such as Jon Smyth. Test with an ordinary user, not only as an administrator. The results can differ because of sharing settings, which the checklist below covers.
You can also test from code, without saving anything. This anonymous Apex runs the active duplicate rules against an unsaved record:
Contact candidate = new Contact(
FirstName = 'Jon',
LastName = 'Smith',
Email = '[email protected]'
);
List<Datacloud.FindDuplicatesResult> results =
Datacloud.FindDuplicates.findDuplicates(new List<Contact>{ candidate });
Integer matches = 0;
for (Datacloud.FindDuplicatesResult findResult : results) {
for (Datacloud.DuplicateResult dupResult : findResult.getDuplicateResults()) {
System.debug('Rule: ' + dupResult.getDuplicateRule());
for (Datacloud.MatchResult matchResult : dupResult.getMatchResults()) {
for (Datacloud.MatchRecord matchRecord : matchResult.getMatchRecords()) {
matches++;
System.debug('Match: ' + matchRecord.getRecord());
}
}
}
}
System.debug('Total matches: ' + matches);An empty result means no active rule found a match for this user. It does not tell you which condition failed, so work through the checklist.
Troubleshooting checklist
Common causes, in the order to check them:
- Status. Both the matching rule and the duplicate rule must be active. A matching rule shows Activating while it indexes existing data, and it does not match reliably until it becomes Active.
- Operations and conditions. The duplicate rule must be set to run on create, edit, or both. Its conditions can also exclude the record, for example by profile.
- Sharing. If the rule enforces sharing rules, it compares only against records the current user can see. A test as an administrator may find a match that a sales user never sees.
- Blank fields. An exact match on a blank field does not count as a match unless the matching rule has Match Blank Fields selected.
- Exact or fuzzy.
Smythwill not matchSmithon an exact condition. Check which method each field uses.
When a save is blocked unexpectedly
Use Database.insert(record, false) and inspect the errors. A Database.DuplicateError carries the rule that fired through getDuplicateResult().getDuplicateRule(). You can also query DuplicateRecordSet to see which sets the rule has created.
Exercise
A sales user creates a Lead that clearly duplicates an existing Lead, but no alert appears. The same test as an administrator shows the alert. What is the most likely cause, and where do you check it?
Show answer
The duplicate rule probably enforces sharing rules, so it compares only against records the user can see. The sales user cannot see the existing Lead. Check the rule's record-level security setting (enforce or bypass sharing rules) and the user's access to that Lead.