Sub topic 5
Building a custom matching rule
This chapter assumes you know how a matching rule and a duplicate rule relate, and the difference between exact and fuzzy matching. You should also be able to find Duplicate Management in Setup. Here you build a custom matching rule for Contacts, activate it, and check that it behaves as designed.
When a custom rule is worth building
Standard matching rules cover common cases such as name plus email or name plus company. They cannot be edited. Build a custom rule when one of these applies:
- Your business identifies a record by a field the standard rule ignores, such as an external ID or a custom field.
- The standard rule is too loose and flags records your users consider distinct.
- The standard rule is too strict and misses duplicates in your data.
- You need different match logic for different record types or segments, each with its own rule.
A custom matching rule only describes what counts as a match. It does nothing to a save until an active duplicate rule uses it.
The working model
A custom matching rule has four parts:
- Object. The rule applies to one object, and you cannot change it after creation.
- Matching criteria. Each row names a field and a matching method (exact or fuzzy). Fuzzy is available only for certain fields, such as names and addresses.
- Matching equation. This combines the rows with AND and OR. The next chapter covers the equation and blank fields in detail. This chapter uses simple equations.
- Status. A new rule is inactive. Activation compiles the rule and indexes the existing records so that matching is fast.
Build the rule step by step
This example creates a rule for Contacts. It treats two contacts as duplicates in either of two cases:
- Their email addresses are identical.
- Their last names are alike, their first names are alike, and their mailing postal codes are identical.
- In Setup, enter Matching Rules in Quick Find and select it.
- Click New Rule.
- Choose Contact as the object.
- Enter the name Contact Email or Name and Zip. Salesforce generates the API name from it. Add a description saying why the rule exists, because other admins will read it later.
- Under Matching Criteria, add these rows:
| Row | Field | Matching method |
|---|---|---|
| 1 | Exact | |
| 2 | Last Name | Fuzzy: Last Name |
| 3 | First Name | Fuzzy: First Name |
| 4 | Mailing Zip/Postal Code | Exact |
- Select Add Filter Logic and enter the equation
1 OR (2 AND 3 AND 4). - Click Save. The rule is inactive.
- Open the rule and click Activate. The status moves to Activating and then Active. The time depends on how many Contact records exist.
Row numbers in the equation refer to the order of the criteria rows, so changing the order after you write the equation changes its meaning. Write the equation last and check it against the table.
Attach the rule to a duplicate rule
An active matching rule changes nothing by itself. Create or edit a duplicate rule on Contact and select Contact Email or Name and Zip as its matching rule. Choose actions for create and edit (alert, block, or allow, with optional reporting). The duplicate rule chapters cover those choices. Until you do this step, no user sees an alert and no record is flagged.
A matching rule can be referenced by more than one duplicate rule. For example, one rule can alert for users in a sales profile, and another can block for an integration user.
Rules for editing and limits
- You can edit a custom matching rule only while it is inactive. To change an active rule, deactivate it first.
- Deactivating a matching rule requires deactivating every duplicate rule that references it. Plan changes for a quiet period, because duplicates can be created while enforcement is off.
- Editing a rule and reactivating it re-indexes existing records, so expect another Activating period.
- You can delete a matching rule only when it is inactive and no duplicate rule uses it.
- Salesforce limits the number of active matching rules per object, the number of criteria rows per rule, and the number of fuzzy fields. The limits change between releases, so check the current documentation before designing a rule with many rows.
- Not every field type is supported. If the field you need is not in the picker, you cannot use it directly. A common workaround is a formula or a trigger-populated text field. A formula field cannot be used in a matching rule, so a text field kept current by a flow or trigger is the usual choice.
Choosing exact or fuzzy per row
Use exact matching for fields that hold identifiers: email, external ID, phone in a normalized format. Use fuzzy matching for fields with human spelling variation, such as names and company names. Fuzzy matching on an identifier risks false positives, and exact matching on a name misses "Jon" against "Jonathan".
Anchoring a fuzzy name match to an exact field, such as postal code in the example above, reduces false positives. Names alone are weak evidence. Two people named "Maria Garcia" are common.
Verify the rule from Apex
Test the rule before users depend on it. After the duplicate rule is active, Datacloud.FindDuplicates runs it against an unsaved record. The result names the matching rule that produced each match, so you can confirm that your custom rule fired and not a standard one.
Contact candidate = new Contact(
FirstName = 'Jon',
LastName = 'Smith',
Email = '[email protected]',
MailingPostalCode = '60601'
);
List<Datacloud.FindDuplicatesResult> results =
Datacloud.FindDuplicates.findDuplicates(new List<Contact>{ candidate });
for (Datacloud.FindDuplicatesResult findResult : results) {
for (Datacloud.DuplicateResult dupResult : findResult.getDuplicateResults()) {
System.debug('Duplicate rule: ' + dupResult.getDuplicateRule());
for (Datacloud.MatchResult matchResult : dupResult.getMatchResults()) {
System.debug('Matching rule: ' + matchResult.getRule()
+ ', matches: ' + matchResult.getSize());
}
}
}Run it in Anonymous Apex with the debug log open. If the debug lines never appear, no active duplicate rule on Contact applies to the record.
Common errors
- The rule is active but nothing happens. No duplicate rule references it, or the duplicate rule is inactive. Check the duplicate rule first.
- The equation matches too much. An OR branch with a single weak row, such as only a fuzzy last name, matches many unrelated records. Combine weak rows with AND.
- Known duplicates are not found. Check whether the fields are blank on one record, which the next chapter explains. Also check that the fuzzy method suits the field, and that the rule has finished activating.
- The equation uses the wrong rows. The numbers refer to row order. Re-read the equation after any change to the table.
- Existing duplicates are not flagged. Matching rules apply when records are created or edited, not retroactively. Existing data needs a separate cleanup, which the later chapters on duplicate record sets and merging cover.
- Edit button missing. The rule is active. Deactivate it first, or you are looking at a standard rule, which is never editable.
Exercise
You built a custom Contact matching rule with the equation (1 AND 2) OR 3. A teammate adds a new criteria row at the top of the table, which becomes row 1, and does not change the equation. What problem results, and what is the safest process for the change?
Show answer
The numbers in the equation refer to row positions, so every row shifted down by one. The equation now combines the wrong fields: the new field is row 1, and the old row 3 is no longer the OR branch. The rule may match too much or too little. The safe process is to make the change in a sandbox, with the rule deactivated (which means deactivating the duplicate rules that use it), rewrite the equation to match the new row order, test it with Datacloud.FindDuplicates on sample records, and then activate and deploy to production in a quiet period.
Exercise
A matching rule on Lead is Active, but users see no duplicate alerts when they enter a lead that has the same email as an existing lead. Name two things to check.
Show answer
First, check that an active duplicate rule on Lead references this matching rule, because a matching rule alone never alerts users. Second, check that the duplicate rule's action for create and edit is set to alert or block and not to allow without an alert, and that the matching rule uses an exact match on Email with no equation error.