Sub topic 2
Matching rules: deciding what counts as a duplicate
This chapter assumes you know the Salesforce data model (objects, fields, records) and can navigate Setup as an administrator. You should also know the difference between Accounts, Contacts and Leads.
What a matching rule does
A matching rule is a set of comparisons that decides whether two records are alike. It does not act on anything. It only answers the question "do these two records match?" A duplicate rule, covered in a later chapter, asks that question when a user saves a record and then decides what happens next.
Each matching rule belongs to one object, and it compares a record against existing records of that object. Salesforce supplies standard matching rules for Accounts, Contacts and Leads. You can also build custom matching rules.
How a comparison runs
When a duplicate rule fires, it passes the new or edited record to its matching rule. The matching rule compares chosen fields on that record with the same fields on existing records. If the comparisons satisfy the rule's logic, the existing record is returned as a match.
The parts of a matching rule
A matching rule is built from three things:
- Field comparisons. Each comparison names a field, such as Email or Last Name, and a matching method for it. The method is either exact or fuzzy. The next chapters cover both.
- A matching equation. This combines the comparisons with AND and OR. For example, "Last Name matches AND (Email matches OR Phone matches)".
- Blank-field handling. You choose whether two blank values count as a match. Treating blanks as matches produces many false positives.
Matching rules and duplicate rules
The two rule types have separate jobs.
| Rule type | Question it answers | Example output |
|---|---|---|
| Matching rule | Are these two records alike? | A list of matching records |
| Duplicate rule | What should happen when they are? | An alert, a block, or a report entry |
One matching rule can serve several duplicate rules. A duplicate rule must reference a matching rule, and it cannot be activated without one.
Why this separation matters
Because the logic lives in the matching rule, you can tune what counts as a duplicate without changing how users are warned. For example, you can loosen a name comparison and leave the duplicate rule's alert untouched. The reverse also holds. You can change an alert to a block without touching the matching logic.
A quick way to see the result
You can run a matching rule from Apex through Datacloud.FindDuplicates, which runs the active duplicate rules for an unsaved record and returns the matches.
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());
}
}
}
}Nothing is inserted. The call only reports which existing records the rules consider duplicates.
Exercise
A team wants to change a duplicate rule from "alert" to "block" without changing which records count as duplicates. Which rule do they edit, and why?
Show answer
They edit the duplicate rule. The action (alert or block) belongs to the duplicate rule. The matching rule only decides which records match, so it stays unchanged.
Exercise
Why can treating blank fields as matches cause problems?
Show answer
Two records with blank values in the same field would count as matching on that field. Many unrelated records would then be flagged as duplicates, which creates false positives.