Sub topic 2
Matching rules: deciding what counts as a duplicate
This chapter assumes you know the basic Salesforce data model (objects, fields, records) and can navigate Setup as an administrator. The Apex snippet near the end is optional and needs basic Apex.
By the end, you should be able to explain what a matching rule is, what it does and does not do, and how it fits with duplicate rules. Later chapters cover standard rules, exact and fuzzy methods, and custom rules in detail.
What a matching rule is
A matching rule is a definition of similarity for one object. It answers one question: given two records of this object, are they alike enough to be duplicates?
A matching rule does not act on anything. It does not block a save, show a warning, or merge records. It only returns a verdict. A different component, the duplicate rule, takes that verdict and decides what happens to the user.
This split is the central idea of Duplicate Management:
| Component | Job | Example |
|---|---|---|
| Matching rule | Decides whether two records match | Same last name and a similar first name and the same email |
| Duplicate rule | Decides what to do when a match is found | Alert the user on create, block on edit |
Keeping the two apart lets you reuse one definition of "duplicate" under different behaviors. For example, a sales team may only get an alert, while an integration user is blocked, and both can rely on the same matching rule.
How a matching rule is evaluated
When a user saves a record, and a duplicate rule applies to it, the process looks like this:
- The duplicate rule checks its own conditions, such as who is saving the record.
- The duplicate rule calls its matching rule or rules.
- The matching rule compares the incoming record with existing records of the target object.
- Each existing record that satisfies the matching rule is returned as a match.
- The duplicate rule applies its action to those matches.
Two details are easy to miss.
- The matching rule is evaluated against the data that is already saved, and against the incoming values. It does not compare unsaved records in the same batch with each other. If you insert two identical new records in one DML call, neither is compared with the other, and both can be saved.
- A matching rule compares records of the object it is defined for. A matching rule for Contact compares Contacts. Matching one object against another, such as Leads against Contacts, is configured on the duplicate rule and covered in the cross-object chapter.
What a matching rule contains
Every matching rule has the same parts, whether it is standard or custom.
- Object. The object whose records it compares.
- Matching criteria. The fields to compare and how to compare them (exact or fuzzy). The criteria are combined into a matching equation, such as
(Last Name AND First Name) OR Email. - Status. Whether the rule is active and can be used.
Salesforce supplies standard matching rules for common objects such as Account, Contact, and Lead. You can also build custom matching rules when the standard ones do not fit your data.
Rule status
A matching rule can only be used by a duplicate rule when it is active. Activation is not instant, because Salesforce builds an index of existing records for the rule. While that happens the status shows as Activating. If you deactivate a rule, the status moves through Deactivating to Inactive.
Because indexing takes time on large objects, plan activation outside peak hours, and do not assume a rule works the moment you click Activate. Check that the status is Active before you test.
What a matching rule does not do
Misunderstandings here cause most early configuration errors.
- It does not run on its own. A matching rule with no duplicate rule pointing at it never fires for users. It sits idle.
- It does not clean existing data. A new matching rule does not flag duplicates already in your org. Duplicate rules act when a record is created or edited. To find existing duplicates, you run reports or use other tools, covered in later chapters.
- It does not see what the user cannot see, unless told to. Whether the comparison includes records the saving user has no access to is decided by the duplicate rule's sharing setting, not the matching rule.
- It is not the same as a validation rule. A validation rule checks one record against fixed logic. A matching rule compares a record against the rest of the data.
Where the verdict shows up
The matching rule's output is visible in several places, which helps when you troubleshoot.
- In the user interface, the duplicate rule shows the matching records in an alert or error message.
- In duplicate record sets, which store groups of records that matched.
- In Apex, through the result classes in the
Datacloudnamespace. EachMatchResultidentifies the matching rule that produced it, and eachMatchRecordcarries a match confidence value.
This anonymous Apex runs the active duplicate rules against an unsaved Contact and prints which matching rule produced each match. Run it in the Developer Console's Execute Anonymous window.
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()) {
System.debug('Matching rule: ' + matchResult.getRule());
for (Datacloud.MatchRecord matchRecord : matchResult.getMatchRecords()) {
System.debug('Confidence: ' + matchRecord.getMatchConfidence()
+ ' Record: ' + matchRecord.getRecord());
}
}
}
}If nothing prints, either no active duplicate rule applies to Contact or no existing record matched. Both are useful facts when you are diagnosing a rule.
Common errors
| Symptom | Likely cause |
|---|---|
| Duplicates are not detected at all | The matching rule is not active, or no active duplicate rule references it |
| A rule worked in a sandbox but not in production | The rule was not activated in production, or indexing is still in progress |
| Existing duplicates are not flagged | Matching rules act on create and edit, not on stored data |
| Two identical records inserted together both saved | Records in the same batch are not compared with each other |
| You cannot edit the rule | It is active, so deactivate it before changing it |
Choosing a starting point
Before you create anything, decide which question you are answering.
- What object are you protecting against duplicates?
- What does "the same" mean for your business: the same person, the same company, or the same household?
- Which fields are reliable enough to compare?
If a standard matching rule already expresses your answer, use it. Build a custom rule only when the standard criteria miss duplicates you care about or flag records you consider distinct. The next chapters show how to make that choice.
Exercise
A Contact matching rule is active, but users can still create obvious duplicate Contacts without any warning. The rule's criteria look correct. What are two things you should check, and why?
Show answer
First, check that an active duplicate rule for Contact references this matching rule. A matching rule only returns a verdict, and without a duplicate rule nothing acts on it. Second, check the duplicate rule's conditions and action. A condition might exclude the user's profile, or the action might not be configured to alert or block. Also confirm the matching rule status is Active and not Activating, since indexing may not be finished.
Exercise
You insert two new Accounts with identical names in a single DML statement, and both are saved even though a duplicate rule with a name-based matching rule is active. Why?
Show answer
The matching rule compares each incoming record with records that are already saved, not with other unsaved records in the same batch. Neither new Account matched an existing record, so both were saved.