Sub topic 17
Design best practices for duplicate prevention
This chapter assumes you know how matching rules and duplicate rules work together, and that you have seen the actions (allow, block, alert, report), duplicate record sets, merging, and the Apex duplicate APIs from earlier chapters. Here you combine them into a design you can defend.
Start with the business case, not the feature
Decide what a duplicate costs you before you pick an action. A duplicate Account can split revenue reporting. A duplicate Lead can make two reps call the same person. The cost sets how strict the rule should be.
Prefer standard matching rules first
Standard matching rules are maintained by Salesforce and cover common cases. Build a custom matching rule only when a standard rule misses a field you depend on, such as a customer number. Every custom rule is something you must test and maintain.
Match the action to the risk
| Situation | Suggested action |
|---|---|
| High-confidence match, such as an exact external ID | Block |
| Likely match, such as a fuzzy name and email | Alert |
| Data loaded by integrations or imports | Allow, with report |
| Unsure how noisy a new rule is | Allow, with report first |
Blocking frustrates users when the match is wrong, so start with Allow and Report. Review the results, then tighten the rule.
Keep matching equations precise
- Use exact matching on identifiers. Use fuzzy matching on names and addresses.
- Decide how blank fields behave. A blank-matches-blank setting can create false positives.
- Require two or three strong fields to agree, instead of one weak field.
Plan for integrations and Apex
Integrations and triggers hit duplicate rules too. Decide per integration whether it should be blocked, or whether it should set allowSave and let you review the results later. Do not bypass rules by default. Handle Database.DuplicateError so a blocked save produces a clear message or a retry path.
Database.DMLOptions dmlOptions = new Database.DMLOptions();
dmlOptions.DuplicateRuleHeader.allowSave = true;
Database.SaveResult result = Database.insert(
new Account(Name = 'Acme Corporation', BillingCity = 'Chicago'),
dmlOptions
);
System.debug('Saved: ' + result.isSuccess());Close the loop with review and merging
Rules prevent new duplicates. They do not clean up old ones. Review duplicate record sets on a schedule and merge what you find, so the data improves over time.
SELECT Id, DuplicateRuleId, RecordCount
FROM DuplicateRecordSet
ORDER BY CreatedDate DESC
LIMIT 20Test and maintain
- Test with realistic data: typos, nicknames, and missing fields.
- Deploy changes to a sandbox first.
- Check ordering and conditions when several rules apply to one object.
- Revisit rules when your data sources change.
Exercise
You add a fuzzy custom matching rule on Contact name only, and users complain that valid contacts are blocked. What two changes would you make, and in what order?
Show answer
First, change the action from Block to Allow with report (or Alert) so work continues while you investigate. Then tighten the matching equation by requiring more fields, such as name plus email, and review the duplicate record sets to confirm the false positives drop before blocking again.