Sub topic 8
Criteria-based sharing rules
Criteria-based sharing rules open record access to a group of users based on the values in a record's fields. They do not depend on who owns the record. This chapter assumes you know org-wide defaults, the role hierarchy, public groups, and what a sharing rule can and cannot do. It also assumes you can read a basic SOQL query.
What a criteria-based rule does
A criteria-based rule says: "Any record of this object where these field conditions are true is shared with this target at this access level."
An owner-based rule starts from who owns the record. A criteria-based rule starts from what the record contains. Use it when the reason for sharing is a field value, such as a status, a region, or a record type.
A rule has four parts:
- The object it applies to.
- The criteria: one or more field conditions.
- The target: a public group, a role, a role and subordinates, or (for some objects) a queue or a Experience Cloud role group.
- The access level: Read Only or Read/Write.
Like every sharing rule, it can only grant more access. It never removes access that org-wide defaults, ownership, or the role hierarchy already give. It also cannot give access to an object the user's profile or permission sets do not allow. If a user has no Read permission on the object, the rule has no visible effect for that user.
Prerequisites in the org
Before you create a rule, check three things:
- The org-wide default for the object must be Private or Public Read Only. A rule that grants Read/Write is only useful if the default is more restrictive than Read/Write. If the default is Public Read/Write, there is nothing to open up.
- The target must exist. Create the public group first if you want to share with a set of users who do not map to one role.
- The fields you want to filter on must be of a supported type.
Building a rule in Setup
In Setup, search for Sharing Settings, find the object in the list, and click New in its Sharing Rules section. Then:
- Enter a label. The API name is generated from it.
- Choose the rule type "Based on criteria".
- Add up to ten criteria rows. Each row has a field, an operator, and a value.
- Choose who to share with, and select the group, role, or role and subordinates.
- Choose the access level.
- Save.
Saving starts a recalculation. Existing records that match are shared with the target, and the rule keeps applying as records are created or edited.
How multiple criteria combine
By default, all rows must be true. This is an AND across the rows. You can switch to custom filter logic, such as 1 AND (2 OR 3), when the requirement is more complex. Number each row and refer to the numbers in the expression.
Operators and value types
The operators available depend on the field type. Text and picklist fields support equals, not equal to, starts with, contains, and similar choices. Number and date fields support comparisons such as greater than.
Enter several values in one row by separating them with commas. The row is then true if the field matches any of them. That is how you cover "Overdue" and "Escalated" in one row.
Some field types cannot be used in criteria. Formula fields and long text areas are common examples. If you need a rule on a calculated value, you usually store the result in a regular field that a flow or trigger keeps up to date.
The rule in Metadata API form
Sharing rules are metadata, so you can deploy them between orgs. This criteria-based rule gives the Finance_Managers public group read access to invoices whose Status__c equals Overdue:
<?xml version="1.0" encoding="UTF-8"?>
<SharingRules xmlns="http://soap.sforce.com/2006/04/metadata">
<sharingCriteriaRules>
<fullName>Share_Overdue_Invoices</fullName>
<accessLevel>Read</accessLevel>
<label>Share Overdue Invoices</label>
<sharedTo>
<group>Finance_Managers</group>
</sharedTo>
<criteriaItems>
<field>Status__c</field>
<operation>equals</operation>
<value>Overdue</value>
</criteriaItems>
</sharingCriteriaRules>
</SharingRules>The element sharingCriteriaRules marks the rule as criteria-based. An owner-based rule uses sharingOwnerRules instead. The sharedTo element names the target. Here it is a group, but it can also be a role or a role and subordinates. In a deployment, the file is named after the object, for example Invoice__c.sharingRules.
What happens when a record changes
The rule is evaluated whenever a record is created or its field values change.
- A new invoice saved with
Status__c = Overdueis shared withFinance_Managersimmediately. - An invoice that changes from Open to Overdue gains the share.
- An invoice that changes from Overdue to Paid loses the share, because it no longer matches.
The last case is the one that surprises people. Access granted by a criteria-based rule is not permanent. It tracks the data. If users report that a record "disappeared" after an edit, check whether a rule's criteria stopped matching.
Inspecting the result
Each match produces a row in the object's share table. For a custom object the table is named <Object>__Share. A rule-created row has a row cause of Sharing Rule, which appears in the API as RowCause = 'Rule'. Query the share rows for one invoice:
SELECT Id, ParentId, UserOrGroupId, AccessLevel, RowCause
FROM Invoice__Share
WHERE ParentId = '<invoice record Id>'For an overdue invoice you should see a row whose UserOrGroupId is the Finance_Managers group, with AccessLevel of Read and RowCause of Rule. The owner appears as well, with the row cause Owner. If the rule row is missing, either the record does not match the criteria or the rule has not finished recalculating.
Standard objects work differently. Account, for example, uses AccountShare, and the object name has a different pattern, but the same columns apply.
Common limits and edge cases
- Rule count. Each object has a limit on criteria-based rules, and the limit can be lower than for owner-based rules. Check the current limit for your edition in the Salesforce documentation before designing a rule per region or per customer.
- Criteria limit. A rule can have at most ten filter rows.
- Blank values. To match records where a field is empty, use an equals or not equal to condition with an empty value. Test this on a few records, since blank handling differs by field type.
- Record types and ownership. You can filter on record type, and you can also filter on the owner's fields where the object allows it. Keep rules readable. Many rows are hard to maintain.
- Not all objects support them. Criteria-based rules are available for accounts, contacts, opportunities, cases, leads, campaigns, custom objects, and some other standard objects. Check the object's sharing settings page.
- Cost of change. Editing a rule's criteria, target, or access level triggers recalculation for the object. On large objects this can take a while, and the work can affect performance. Make changes in off-peak hours when you can.
Common errors
- Sharing with the wrong target type. Choosing a role when you meant "role and subordinates" leaves out everyone below that role. If the manager sees the record but the team below does not, check this first.
- Expecting a rule to lock records down. A rule only adds access. To restrict, change the org-wide default or reduce what profiles allow.
- Using a field that changes often. A field that flips constantly makes shares appear and disappear. Prefer stable fields when you can.
- Forgetting object permissions. The target must have Read on the object, or the shared record is invisible to them.
- Using a criteria rule where a team or manual share is better. If the reason for sharing is "these particular people work on this record," a rule that matches field values is the wrong tool. The record may need an account team or a manual share instead.
When to choose a criteria-based rule
| Situation | Better choice |
|---|---|
| Everyone who owns records in a role needs to share them | Owner-based rule |
| Records with a given field value need to reach a group | Criteria-based rule |
| One person needs one record | Manual sharing |
| Named people work on one account | Account team |
Exercises
Exercise
An invoice has Status_c set to Overdue, and members of FinanceManagers can read it through a criteria-based rule. A user changes the status to Paid, which the rule does not match. What happens to the Finance_Managers access, and where can you confirm it?
Show answer
The access is removed, because the record no longer matches the criteria. Confirm it by running a query on Invoice_Share for that invoice's ParentId. The row for the FinanceManagers group with RowCause "Rule" will be gone, and only rows such as the owner row remain.
Exercise
The org-wide default for Invoice__c is Public Read/Write. You create a criteria-based rule that gives a group Read Only access to overdue invoices. Does this restrict or change anything for the group? Why?
Show answer
No. A sharing rule can only grant additional access, and the default already gives everyone Read/Write. To make the rule meaningful, the default must be Private or Public Read Only.