Sub topic 7
Duplicate rules: connecting matching rules to user actions
This chapter assumes you know the Salesforce data model (objects, fields, records) and can navigate Setup as an administrator. You should already know what a matching rule is from the earlier chapters. The Apex snippet near the end needs basic Apex.
What a duplicate rule does
A matching rule answers one question: "Are these two records alike?" It does not decide what happens next. A duplicate rule uses one or more matching rules and decides what Salesforce does when a match is found.
The two work as a pair:
| Piece | Question it answers | Where it lives |
|---|---|---|
| Matching rule | Which records count as the same? | Setup, Matching Rules |
| Duplicate rule | What happens when a save finds a match? | Setup, Duplicate Rules |
A duplicate rule runs when a user or an integration creates or edits a record. It takes the record being saved, asks each of its matching rules to look for similar records, and then applies the action you configured. If no match is found, the save proceeds as usual.
Anatomy of a duplicate rule
Open Setup, search for Duplicate Rules, and create a new rule. The form has these parts.
- Object. Each duplicate rule belongs to one object, such as Account, Contact, or Lead. This is the object whose records are being saved.
- Rule name and description. Use names that say what the rule protects, for example "Contact: same email or name and account". Admins read these names in reports and error messages.
- Record-Level Security. This decides whether the rule compares only against records the user can see. It has its own chapter, so only the headline here: with "Enforce sharing rules", a user cannot be warned about a record they cannot see. With "Bypass sharing rules", the rule searches every record.
- Actions. Separate settings for On Create and On Edit. Each is either Allow or Block, with options for alert and report.
- Alert text. The message users see when a rule alerts or blocks. Write it so a user knows what to do next.
- Matching Rules. The matching rules the duplicate rule uses. For the same object, choose from the standard or custom matching rules. To compare against another object, choose a matching rule for that object and map the fields.
- Conditions. Optional filters that limit when the rule runs, for example only for certain profiles or record types.
The matching rule does the comparison. The duplicate rule only decides who gets compared, when, and what happens after.
Create and edit are separate decisions
Many admins set both to the same action, but the settings are independent. This is useful because the two situations differ.
- On Create, the user has just typed in a new record. Blocking here stops the duplicate from ever existing.
- On Edit, the record already exists. A block on edit can trap users: they open an old record to fix a phone number, and the save fails because the record matches another one already in the system.
A common compromise is Block on create and Allow with report on edit. Duplicates are prevented going forward, and existing ones are collected in duplicate record sets for later cleanup instead of getting in the way of daily work.
Seeing the rule at work in the UI
When a rule with Allow and Alert finds a match, the user sees your alert text and a list of the matching records. They can open a match to review it, or save anyway. When a rule blocks, the user sees the alert text as an error and the save fails. They can fix the data or work with the existing record instead.
Report is separate from alert. With Report selected, Salesforce records the match in a duplicate record set even if the user sees nothing. You can use reports on those sets later to find and merge duplicates.
Rule conditions and what they change
Conditions let one object have several rules for different situations. Suppose sales reps create Leads by hand, while a web form creates others through an integration. You might write one rule that blocks for reps and another that only reports for the integration user's profile. Conditions on the current record, such as profile or lead source, decide which rule applies.
If you use conditions, be aware that records that do not satisfy them are never compared by that rule. A missing alert is not always a matching problem. Sometimes the condition simply excluded the record.
Before the rule does anything
A duplicate rule only runs when it is active. A new rule is created inactive, and you must activate it. Two points matter here.
- The matching rules it references must also be active. An active duplicate rule with an inactive matching rule finds no matches.
- An object can have a limited number of active duplicate rules, and each duplicate rule can use a limited number of matching rules. Check current limits in Salesforce documentation when you plan more than one rule per object.
Activation, editing, and ordering have their own chapter, so we keep the details there.
When code saves the record
Duplicate rules apply to Apex and API saves too, but the behavior differs from the UI. There is no screen for the user to click "Save anyway". How the save behaves depends on the rule's action.
- A rule set to Block fails the save. With
Database.insert(record, false), the failure is returned in theSaveResultas aDatabase.DuplicateError. With a plaininsertstatement, aDmlExceptionis thrown. - A rule set to Allow without an alert lets the save through. A rule set to Allow with alert returns a duplicate error from code unless the caller sets
DuplicateRuleHeader.allowSaveto true.
This Apex shows how to find out which rule stopped a save:
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());
System.debug('Message: ' + dupResult.getErrorMessage());
}
}
}getDuplicateRule() returns the developer name of the rule that fired. That helps when several rules apply to the same object. The chapter on Apex covers the full DML options.
Common mistakes
- Expecting the duplicate rule to define "duplicate". The matching rule defines it. If the wrong records match, change the matching rule, not the duplicate rule.
- Leaving the rule inactive. Saving a duplicate rule does not activate it.
- Writing a vague alert message. "Duplicate found" tells the user nothing. Say what to check: "A contact with this email already exists. Open the match below before you create a new one."
- Choosing Block everywhere. Blocked users find workarounds, such as adding a space to a name, which creates harder-to-detect duplicates. Reserve Block for cases where a match is almost certainly a true duplicate.
- Forgetting integrations. Data loads and APIs hit the same rules. A block can make a load fail row by row, so plan for it.
Exercise
A duplicate rule for Contacts is active, and its matching rule is set up correctly. Users report that when they edit old Contacts to correct a typo, the save fails with the duplicate error. The business wants to keep blocking duplicates on new records. What change would you make, and why?
Show answer
Change the On Edit action from Block to Allow, and select Report (optionally Alert too). Leave On Create as Block. The create and edit actions are independent settings. Old records that already match another record then stay editable, and their matches are collected in duplicate record sets for a later merge.
Exercise
A developer's Apex calls Database.insert(acct, false) and gets a failed SaveResult containing a Database.DuplicateError, although the duplicate rule is set to Allow with Alert. What does the developer need to do for the insert to succeed, and where is that option set?
Show answer
Set DuplicateRuleHeader.allowSave to true on a Database.DMLOptions object and pass it to Database.insert. This tells Salesforce the caller acknowledges the alert, so the save goes through.