Sub topic 16
Testing and troubleshooting rules
This chapter assumes you can already build a custom matching rule and a duplicate rule, and that you know the difference between exact and fuzzy matching. You also need basic Apex for the last section. The goal is to answer two questions with evidence: why was this record flagged, and why was this record not flagged?
A testing mindset
A duplicate rule fails in one of three places. The matching rule may not consider the records alike. The duplicate rule may not run for the save in question. Or the rule ran and found a match, but the action did not do what you expected. Test each layer separately. Do not start by changing the matching equation.
| Layer | Question to ask | Where to look |
|---|---|---|
| Matching rule | Would these two records match at all? | Matching rule status, fields, method, blank-field handling |
| Duplicate rule | Did the rule run for this save? | Active flag, conditions, record-level security setting, create or edit selection |
| Action | What happened when it matched? | Allow, Block, alert, report, and the duplicate record set |
Build a test set first
Before you trust a rule, create a small set of records that you know should and should not match. Use a sandbox, not production, because every test that saves a record can create data and duplicate record sets.
A useful set has these cases:
- An exact copy of an existing record. This should always match, so a failure here points to activation or scope.
- A near match, such as
Jon SmithandJonathan Smithwith the same email. This tests fuzzy logic. - A record that differs in one required field, such as the same name with a different email domain. This tests whether the equation is too loose or too strict.
- A record with the compared field blank. This tests blank-field handling.
- A record owned by a user who cannot see the original. This tests the sharing setting.
Write down the expected result for each case before you save it. Without a prediction you cannot tell a pass from a surprise.
Common reasons a record was not flagged
The rules are not active
A duplicate rule and the matching rule it uses must both be active. After you activate a matching rule, Salesforce has to build its index, and its status shows as activating until that finishes. Records saved during that window may not match. Check the status on the matching rule detail page before you run any test.
The save did not trigger the rule
A duplicate rule runs on create, on edit, or both, depending on how you set it. If you selected only create, editing a record into a duplicate passes silently. Rules also stop applying when conditions are set. For example, a rule limited to records where the billing country is the United States will not run for a record with a different country.
The user cannot see the other record
A duplicate rule can enforce sharing rules or ignore them. When it enforces them, it compares the new record only against records the saving user can see. A user in one territory may not be warned about a duplicate owned by another team. If tests run as an administrator pass but a real user gets no alert, this is the first thing to check. Run the same test as the real user. The "Login as" feature, or a test user with the same profile and role, will show it.
The equation is stricter than you thought
Matching equations combine field conditions with AND and OR. An AND over name and email means both must match. Review each condition and ask whether your test data satisfies it.
Blank fields are a frequent cause. If two records both have a blank email, they do not count as matching on email unless the rule says otherwise. A rule that depends on a field that is often empty finds fewer duplicates than expected.
Fuzzy matching is not as fuzzy as you expect
Fuzzy methods handle common variants such as nicknames and spelling differences for supported fields, but they do not apply to every field. Some fields can only be matched exactly. Confirm that the method you chose is available for the field, and that your test pair is the kind of variation the method handles. A fuzzy match is not a general similarity score.
Common reasons a record was flagged unexpectedly
- The standard rule is still active. A custom rule you built may sit next to the standard one. Both can fire, so a user sees an alert you did not design.
- The equation is too loose. An OR condition on a common value, such as a company name like
Acme, matches many unrelated records. - Cross-object rules compare more than you realize. A Lead rule that compares against Contacts will alert on every Contact with a similar name.
- Integrations and imports trigger rules. Any save through the UI, API, or data tools can run the rule, depending on the action and the
allowSavesetting.
Reading what Salesforce tells you
When a rule fires, the alert or error shows the rule name and the matching records. Use that name to find the exact duplicate rule that acted. If several rules are active for an object, they are evaluated in order, so the name tells you which one it was.
For a rule set to report, look at the duplicate record sets. These queries show what has been recorded.
SELECT Id, DuplicateRuleId, RecordCount
FROM DuplicateRecordSet
ORDER BY CreatedDate DESC
LIMIT 20An empty result after tests that should have matched often means the action was Allow without a report, or the rule did not run. A set with unexpected members points to a loose equation.
Testing a rule without saving
You can ask the rules a question without inserting a record. Datacloud.FindDuplicates.findDuplicates runs the active duplicate rules for the object 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 });
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()) {
System.debug('Possible duplicate: ' + matchRecord.getRecord());
}
}
}
}Run this in Anonymous Apex from the Developer Console and read the debug log. It lets you try many variations quickly, and it leaves no test data behind. It uses the rules that are active, so it also tells you whether a rule is active at all.
Diagnosing blocked saves in code
When a save is blocked, the failure appears as a Database.DuplicateError. If you use the partial-save form of DML, you can inspect it.
Account acct = new Account(Name = 'Acme Corporation', BillingCity = 'Chicago');
Database.SaveResult saveResult = Database.insert(acct, false);
if (!saveResult.isSuccess()) {
for (Database.Error err : saveResult.getErrors()) {
if (err instanceof Database.DuplicateError) {
Database.DuplicateError dupError = (Database.DuplicateError) err;
Datacloud.DuplicateResult dupResult = dupError.getDuplicateResult();
System.debug('Blocked by rule: ' + dupResult.getDuplicateRule());
}
}
}If your own code calls Database.insert(acct) without the false argument, the error throws as an exception instead. That is a common source of unexpected failures in triggers and integrations after a Block rule goes live.
A repeatable troubleshooting routine
- Reproduce with a specific pair of records and note the user, object, and action (create or edit).
- Confirm both rules are active and that the matching rule has finished activating.
- Run
findDuplicateswith the candidate values. If it returns the match, the matching rule works and the problem is in the duplicate rule, scope, or action. - If it returns nothing, review the equation, blank fields, and method for each field.
- Repeat the test as the real user to rule out sharing differences.
- Check the duplicate record sets and reports to see whether the match was recorded.
- Change one thing at a time, then retest the full test set, including the cases that previously passed.
Exercise
Administrators report that a new duplicate rule alerts on a duplicate Contact, but sales users in another region are never alerted for the same pair. findDuplicates run by an administrator returns the match. What is the most likely cause, and how would you confirm it?
Show answer
The duplicate rule probably enforces sharing rules, so it only compares against records the saving user can see. Sales users in the other region cannot see the original record, so no match is found for them. Confirm it by running the same save as one of those users (or a test user with the same profile and role) and by checking the rule's record-level security setting.
Exercise
A rule's matching equation requires Email to match exactly. Two records have identical names and both have a blank Email, yet no duplicate is flagged. Why, and what are two ways to address it?
Show answer
Blank fields do not count as matching on that condition unless the rule is set to treat them that way, so the Email condition fails. You can change the equation so name and another populated field are enough (for example, using OR with a different condition), or adjust the blank-field setting on the field condition so blanks count as a match, after checking that this will not create too many false positives.