# How do Matching Rules and Duplicate Rules work together to prevent duplicate data entry?

**Topic:** Admin · **Difficulty:** Medium · **Tags:** Admin, Duplicate Management, Matching Rules

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-matching-rules-and-duplicate-rules-work-together-to-prevent-duplicate-data-entry

## Answer

**Basics:** Duplicates harm reporting and customer experience. Duplicate Management separates detection (matching rule) from action (duplicate rule), so the same matching logic can drive different behavior for different users.

**How it works:**
1. Matching Rule: Setup > Duplicate Management > Matching Rules; pick object, define field comparisons (for example Account Name fuzzy AND Billing Street fuzzy), choose Exact or Fuzzy per field, and activate. Standard Account, Contact and Lead rules exist.
2. Duplicate Rule: Setup > Duplicate Management > Duplicate Rules; choose object, record-level security (enforce or bypass sharing), actions for Create and Edit (Allow with alert/report, or Block), and attach one or more matching rules; set conditions (for example only when the user's profile is X).
3. When a record is saved, Salesforce evaluates the matching rule against existing records and applies the duplicate rule action.
4. Allowed duplicates are logged as Duplicate Record Items/Sets, available for reporting.

**Advanced / deep dive:** Fuzzy matching uses algorithms (for names, addresses, phone numbers) and covers common variants such as Bob/Robert; it is not available for every field type. Rules can compare across objects (for example Contact to Lead) via cross-object duplicate rules (Lead and Contact/Account). Sharing mode matters: "Enforce sharing rules" only compares against records the user can see, so hidden duplicates can still be created; "Bypass sharing rules" compares against everything. Through the API and Apex, a duplicate rule set to Allow does not block; with Block, the DML fails with `DUPLICATES_DETECTED` unless `Database.DMLOptions.DuplicateRuleHeader.allowSave = true`. Apex can proactively check with `Datacloud.FindDuplicates.findDuplicates(records)`. Duplicate rules apply on record creation and edit, but do not retroactively clean existing data; use the Merge tools, Reports on duplicate sets, or a dedupe app. Only a small number of active rules per object are allowed; verify current limits.

**Code / config example:**
```apex
// Allow save despite a duplicate warning, and inspect the result
Database.DMLOptions dml = new Database.DMLOptions();
dml.duplicateRuleHeader.allowSave = true;
dml.duplicateRuleHeader.runAsCurrentUser = true;
List<Database.SaveResult> res = Database.insert(newLeads, dml);
```

**Hidden gotchas & must-remember points:**
- Matching rules alone do nothing; they must be referenced by a duplicate rule.
- Blank fields do not match unless configured; blank-to-blank match is a setting.
- Block applies to the UI and API; Allow with alert only warns in the UI.
- Sharing mode can hide duplicates from the check.
- Existing duplicates are not detected until the records are edited or you run a report/job.
- Integrations without error handling for DUPLICATES_DETECTED will fail silently or loudly.

**Best practice:** Use Allow with alert for sales users, Block for integration-critical objects only after cleansing data, and review duplicate reports regularly.

---
Source: ForceChallenges · last updated 2026-11-02
