Sub topic 17
Design best practices for duplicate prevention
This chapter pulls the earlier material together into design decisions. It assumes you can already create a matching rule and a duplicate rule in Setup, and that you know the difference between exact and fuzzy matching. The Apex sections assume basic Apex and SOQL.
Start from the business problem
Salesforce Duplicate Management has two parts. Matching rules decide whether two records look alike. Duplicate rules decide what happens when they do. Good design starts with the question "which duplicates cost us money, and who creates them?" rather than "which fields can we compare?"
Answer these before you open Setup:
- Which objects have a duplicate problem (usually Lead, Contact and Account)?
- Where do the duplicates come from: manual entry, web-to-lead, imports, integrations or lead conversion?
- Who should see the warning, and what should they be able to do about it?
- What does a false positive cost compared with a missed duplicate?
The last question drives most other choices. Blocking a legitimate record can stop a sales rep from logging a call. Missing a duplicate means two reps work the same account.
Use the standard rule first
Standard matching rules are tuned and maintained by Salesforce. Start with them and replace them only when you can name a specific gap, for example:
- Your business identifies people by a custom field such as a customer number.
- Standard rules are too loose and flag too many records.
- You need to match on a field the standard rule ignores.
A custom matching rule is something you must test and maintain. Every extra field condition is one more thing that can cause false positives or missed matches.
Match on strong, clean identifiers
Choose fields that identify a record and rarely change. Email, phone, and name plus company are common. Avoid fields that many different records share, such as country or a generic status.
| Field type | Typical method | Watch out for |
|---|---|---|
| Exact | Shared mailboxes such as info@ or sales@ | |
| Last name | Fuzzy | Common surnames produce many weak matches |
| Company or account name | Fuzzy | Legal suffixes and abbreviations |
| Customer number | Exact | Blank values on older records |
| Phone | Exact | Formatting differences and shared switchboards |
Two practical rules follow from this table:
- Use exact matching for identifiers and fuzzy matching for names. Fuzzy matching on an ID field adds noise and gains nothing.
- Require a strong field in every equation. A rule that matches on name alone will flag many people who are not duplicates.
Decide how blank fields behave
Blank values are a frequent source of surprises. If both records have an empty value in a field, the rule must decide whether that counts as a match. If blanks count as matches, two records with no email address can look identical on that field. For identifier fields, treat blanks as non-matches so that missing data never creates a match on its own. Check the blank-field setting on every condition when you review a custom rule.
Match the action to the risk
Pick the least severe action that still protects the data.
| Situation | Suggested action |
|---|---|
| Manual entry by experienced users, matches may be legitimate | Allow with an alert |
| Strong identifier match (exact email or customer number) | Block |
| Integration or import that cannot show a dialog | Allow, and report |
| You are still tuning a new rule | Allow with report, then tighten later |
Treat "Report" as a measuring tool. When you introduce a rule, run it as Allow with report first. Review the duplicate record sets it creates, then adjust the matching rule before you switch to Block. Blocking on day one usually generates support tickets and workarounds.
Plan for every entry point
A duplicate rule only helps if the record is created through a path the rule covers. Review each source:
- User interface: Alert and Block both work and give clear feedback.
- Imports and integrations: A block returns an error for each failed row. Decide whether the integration should retry, log, or flag the failure for review.
- Lead conversion: Conversion can create or update Contacts and Accounts, so rules on those objects matter here. Cross-object rules from Lead to Contact help catch existing customers before conversion.
- Apex and API: Code receives duplicate errors as
Database.DuplicateErrorand can choose how to react.
Handle duplicates in code deliberately
Code that writes records should check for duplicates on purpose rather than rely on a failure. To look for matches before saving, use Datacloud.FindDuplicates.findDuplicates. To react to a block, inspect the save result for a duplicate error.
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());
}
}
}Use the bypass option sparingly. Setting allowSave lets a save through when the rule's action is Allow with an alert. It does not override a Block rule.
Account acct = new Account(Name = 'Acme Corporation', BillingCity = 'Chicago');
Database.DMLOptions dmlOptions = new Database.DMLOptions();
dmlOptions.DuplicateRuleHeader.allowSave = true;
Database.SaveResult saveResult = Database.insert(acct, dmlOptions);
System.debug('Saved despite alerts: ' + saveResult.isSuccess());Clean up what already exists
Prevention rules do not remove duplicates that were created before the rules were active. Plan a cleanup pass:
- Run the rule in report mode so that it creates duplicate record sets.
- Query the sets and review their size.
- Merge records in small, reviewed batches.
SELECT Id, DuplicateRuleId, RecordCount
FROM DuplicateRecordSet
ORDER BY CreatedDate DESC
LIMIT 20Large sets are worth inspecting first. A set with many members often points to a weak matching condition, such as a shared email address, not to many real duplicates.
Test before you activate
Build a small test data set with three groups: true duplicates, near misses (similar names, different people) and records with blank fields. Run each group through the rule using the UI or findDuplicates, and confirm the results. Keep the test records so you can repeat the check after you change the rule.
When you change a matching rule, remember that rules must be deactivated before editing and reactivated afterward. Schedule that change outside busy hours, because protection is off while the rule is inactive.
Keep the rule set small
Overlapping rules are hard to reason about. Prefer one duplicate rule per object and use conditions or cross-object matching to extend it. Document each rule's purpose, owner and action. Review the duplicate record sets on a schedule, because rising counts show that either the rule has drifted or a new data source needs attention.
Summary checklist
- Begin with standard matching rules and add custom ones only for a named gap.
- Use exact matching for identifiers and fuzzy matching for names.
- Treat blank values as non-matches on identifier fields.
- Roll out with Allow and report, then move to Block once results are clean.
- Cover every entry point, including imports, lead conversion and Apex.
- Use bypass options only where a decision has been made.
- Test with true duplicates, near misses and blanks.
Exercise
Your team wants to block any new Contact whose last name fuzzy-matches an existing Contact. Explain why this design is risky and describe a safer rollout.
Show answer
Last name alone is a weak identifier. Many different people share common surnames, so fuzzy matching on it will produce many false positives, and a Block action would stop legitimate saves. A safer design requires a strong field in the matching equation, such as an exact email match, or combines fuzzy last name with first name and company. Roll the rule out as Allow with report first, review the duplicate record sets to check the false-positive rate, and move to Block only for the strong-identifier rule once the results are clean.
Exercise
An integration loads Accounts through the API and a Block rule makes some rows fail. A developer proposes setting allowSave = true in the integration code to fix it. Will this work, and is it a good idea?
Show answer
It will not work for a Block rule, because allowSave only lets a save proceed when the rule's action is Allow with an alert. Even where it applies, it would stop all duplicate protection for the integration. A better approach is to log the Database.DuplicateError details, route the failed rows to a review queue, and merge or update the existing record instead of inserting a new one.