Sub topic 11
Activating, editing and ordering rules
This chapter covers how matching rules and duplicate rules change state over time. You will see why a rule sometimes won't activate, what happens to existing data when you edit a rule, and how rule order affects what users see. It assumes you can already build a matching rule and a duplicate rule, as covered in earlier chapters. It also assumes you can open Setup as an administrator.
Two kinds of rules, two lifecycles
Matching rules and duplicate rules depend on each other, but they behave differently when you activate or edit them.
| Row | Matching rule | Duplicate rule |
|---|---|---|
| What activation does | Builds an index of existing records so comparisons are fast | Starts applying actions on create and edit |
| Speed | Not instant. It can take a while on large objects | Immediate |
| Edit while active | No. Deactivate first | Yes |
| Depends on | Nothing | An active matching rule |
A duplicate rule only evaluates records through a matching rule. Salesforce therefore enforces an order of operations. Activate the matching rule first, then the duplicate rule. When you shut things down, go the other way.
Matching rule statuses
A matching rule moves through several statuses. Only Active means the rule can be used.
While a rule is Activating, Salesforce is evaluating your existing records for the rule. On a large object this can take a long time, and you will get an email when it finishes. You can't use the rule in a duplicate rule until it is Active. If activation fails, open the rule and check the message, then correct the problem and try again. Typical causes are a field condition that is no longer valid or a rule that conflicts with another setting.
A matching rule's status can matter for other reasons too. A duplicate rule that points at a matching rule that is not Active will fail to activate. Editing a matching rule that duplicate rules use is blocked until those duplicate rules are deactivated.
Activating in the right order
Follow this sequence when you introduce new duplicate prevention:
- Create the matching rule and activate it. Wait until its status is Active.
- Create the duplicate rule and select that matching rule.
- Activate the duplicate rule.
- Test with records that you know should and should not match.
Standard matching rules are provided by Salesforce. You can't edit or delete them, so if the standard behavior is not enough, create a custom matching rule instead.
Editing a matching rule safely
You can't change the equation of an active matching rule. The usual procedure is:
- Find every duplicate rule that uses the matching rule.
- Deactivate those duplicate rules.
- Deactivate the matching rule and wait for it to reach Inactive.
- Edit the matching rule and save it.
- Activate the matching rule and wait for Active.
- Reactivate the duplicate rules.
Between steps 2 and 6, nothing prevents duplicates on that object. If the object takes live traffic, such as web-to-lead forms or integrations, plan for that gap. Schedule the change at a quiet time, or tell the integration owners first. Reindexing a large object makes the gap longer.
Editing a duplicate rule
Duplicate rules are easier. You can change the action (allow, block, alert, report), the conditions and the matching rule selection on an active rule, and the change applies to the next save. Existing records and existing duplicate record sets are not rewritten. Tightening a rule, for example moving from Alert to Block, only affects future saves. Records already saved as duplicates stay as they are until someone merges or deletes them.
Some practical consequences:
- If you switch a rule from Allow to Block, integrations that were saving duplicates will start failing. Check error handling in those integrations first.
- If you change the matching rule that a duplicate rule uses, test again. The new rule may match a very different set of records.
- To delete a rule, deactivate it first. Deactivate and delete the duplicate rule before the matching rule that it uses.
Ordering duplicate rules
An object can have several active duplicate rules. You can have up to five active duplicate rules on an object. The rules appear in a list on the object's duplicate rule page, and you set their order with the Reorder control there.
Order matters when more than one rule could fire on the same save. Say one rule blocks saves on exact email matches, and another alerts on fuzzy name matches. The order decides which rule is evaluated first, and so which message the user sees first. Put the most restrictive and most specific rules first. Then verify what happens when both rules match. Test this in a sandbox rather than assuming. Behavior with a mix of Block and Alert actions is easier to confirm by trying it than by reasoning from the rule definitions.
Keep the rule set small. Each active rule adds work to every save on the object, and several overlapping rules make troubleshooting harder. If two rules differ only by a condition, consider whether one rule with a better matching equation would do.
Checking the current state with SOQL
You can read the duplicate rules in an org with a query. This is useful for audits, especially after a deployment.
SELECT DeveloperName, SobjectType, IsActive, SortOrder
FROM DuplicateRule
ORDER BY SobjectType, SortOrderCompare the result with what you expect. A rule that is missing, inactive or in the wrong position is a common reason that duplicates slip through after a release.
Moving rules between orgs
Matching rules and duplicate rules are metadata, so you can deploy them from a sandbox to production. Plan for the activation behavior described above. A matching rule that arrives active still has to build its index in the target org. A duplicate rule that depends on it can't be active until that finishes. If you deploy both together, check afterward that both ended up Active, and check the order of rules on each object. Don't assume that the sandbox order carried over.
Common mistakes
- Trying to edit an active matching rule. The edit option is unavailable. Deactivate it, along with the duplicate rules that use it.
- Activating a duplicate rule too early. If the matching rule is still Activating, the duplicate rule can't use it. Wait for Active.
- Assuming a new rule cleans old data. It doesn't. It only checks records saved afterward.
- Leaving rules inactive after maintenance. Add a check to your change checklist, or run the query above.
- Ordering by habit. Put the blocking, specific rules first and test the combinations.
Exercise
Your org has a duplicate rule on Contact that blocks saves, and it uses a custom matching rule. You need to add a new field condition to the matching rule. List the steps in the right order, and say what risk the change creates.
Show answer
Deactivate the duplicate rule, then deactivate the matching rule and wait for it to become Inactive. Edit the matching rule to add the field condition, save it, and activate it. Wait until its status is Active, because activation builds the index and can take time. Then reactivate the duplicate rule and test with records that should and should not match. The risk is the period while the rules are off. Users and integrations can save duplicate Contacts without any check, and those records are not caught afterward. You would need a duplicate job or report to find them.
Exercise
You change an active duplicate rule from Alert to Block. What happens to the duplicate records that already exist?
Show answer
Nothing happens to them. A duplicate rule acts on creates and edits that happen after the change. Existing duplicates stay in place until someone merges or deletes them. New saves that match are now blocked, so integrations that relied on saving through alerts may start to fail.