Sub topic 8
Criteria-based sharing rules
Criteria-based sharing rules grant access to records by field value instead of by owner. This chapter assumes you know org-wide defaults, the role hierarchy, public groups, and the general purpose of sharing rules. It also assumes you can read basic Metadata API XML.
What a criteria-based rule does
A criteria-based rule shares every record that matches one or more field conditions. Examples are "Status equals Overdue" or "Region equals EMEA". The record owner and the owner's role do not matter. When a record's field values start to match, the rule opens access. When they stop matching, the rule removes that access.
A rule has four parts:
- The object being shared.
- The criteria that select records.
- The target that receives access: a public group, a role, a role and its subordinates, or a role with internal subordinates.
- The access level: Read Only or Read/Write.
Like all sharing rules, a criteria-based rule can only open access. It cannot go below the org-wide default.
Worked example: overdue invoices
Finance managers need read access to every overdue invoice. The org-wide default for Invoice__c is Private. Sales reps own the invoices.
In Setup, open Sharing Settings and find the sharing rules list for Invoice. Create a new rule with these values:
| Setting | Value |
|---|---|
| Label | Share Overdue Invoices |
| Rule type | Based on criteria |
| Criteria | Status__c equals Overdue |
| Share with | Public group: Finance_Managers |
| Access level | Read Only |
The same rule in Metadata API form looks like this:
<?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>Save the rule and let the recalculation finish. Members of Finance_Managers can then open any invoice whose Status__c is Overdue.
How the rule behaves over time
The rule reacts to data changes. The diagram shows the life of one invoice.
When the status changes, Salesforce adds or removes the share row for the record. You can see it by querying the share table. The row cause is a sharing rule, not Owner or Manual.
SELECT Id, ParentId, UserOrGroupId, AccessLevel, RowCause
FROM Invoice__Share
WHERE ParentId = '<invoice record Id>'Common errors
- Choosing the wrong target. A rule shared to a role does not reach users who are only in a public group, and the reverse is also true. Check which one you selected.
- Expecting the rule to restrict. If the org-wide default is already Public Read Only, a Read Only rule adds nothing. Only Read/Write would change anything.
- Misspelled or case-mismatched values. The criteria compare exact values, so
Overdueandoverdueare not interchangeable for every field type. Prefer picklists. - Too many rules. Every rule adds recalculation work. Several narrow rules on one field are often better combined into one rule with a multi-value condition, where the operation allows it.
- Forgetting the delay. Large data volumes can make recalculation slow, so a new rule may not take effect immediately.
Exercise
The org-wide default for Invoice_c is Private. A criteria-based rule shares records where Statusc equals Overdue with FinanceManagers as Read Only. An invoice owned by a sales rep changes from Overdue to Paid. What happens to the Finance_Managers access, and how can you confirm it?
Show answer
The rule no longer matches the record, so Salesforce removes the sharing rule access. FinanceManagers lose access unless another mechanism still grants it, such as a different rule, manual sharing, or the role hierarchy. To confirm, query InvoiceShare for the invoice's ParentId and check that no row for FinanceManagers with a sharing rule row cause remains.