Sub topic 13
Share objects, access levels, and row causes
This chapter assumes you know how org-wide defaults, ownership, the role hierarchy, sharing rules, manual sharing, and teams grant access, and that you can read basic SOQL. Here you look at where those grants are stored and how to read them.
Where sharing is stored
Salesforce does not keep a single list of "who can see what." Each object that uses sharing has a companion share object, which is a table of rows that grant access to individual records. When a mechanism grants access beyond the owner, the platform writes a row there.
The share object name depends on the object type:
| Object type | Share object name | Example |
|---|---|---|
| Standard object | `<Object>Share` | `AccountShare`, `OpportunityShare`, `CaseShare` |
| Custom object | `<Object>__Share` | `Invoice__Share` |
A share object exists only when the object's org-wide default is more restrictive than Public Read/Write. If an object is Public Read/Write, everyone can already edit every record, so there is nothing to store. Controlled by Parent objects, such as Contact when it follows its Account, also have no share object of their own.
Each row in a share object has four fields you will use constantly:
| Field | Meaning |
|---|---|
| `ParentId` | The record being shared. Standard share objects use `AccountId`, `OpportunityId`, or `CaseId` instead. |
| `UserOrGroupId` | The user, public group, queue, or role-based group that receives access. |
| `AccessLevel` | `Read` or `Edit`. Some objects also allow `All`. |
| `RowCause` | The reason the row exists. |
The owner has a share row too. Salesforce writes it with the row cause Owner when the record is created, and you cannot edit or delete it.
Access levels
The access level is the most permissive thing a row can give. It does not override object permissions. A user with Read on a record and no Edit permission on the object stays read-only, and the reverse is also true. A user needs both the object permission and a share row (or ownership) that reaches the record.
Readlets the user view the record.Editlets the user view and change the record.Allmeans full access, including delete and sharing the record with others. Only the Owner row and similar system-controlled rows have it. You cannot assign it in your own rows.
Standard share objects add per-object fields. AccountShare has AccountAccessLevel, OpportunityAccessLevel, and CaseAccessLevel, because an account share can also decide the user's access to the account's child opportunities and cases.
Row causes
The row cause tells you which mechanism created a row. This is the key to troubleshooting, because two rows with the same user and access level can come from very different places.
| Row cause | Created by | Removed when |
|---|---|---|
| Owner | Record creation or ownership change | The owner changes |
| Manual (`Manual`) | The Sharing button, or Apex using the Manual cause | A user removes it, or the owner changes |
| Rule | An owner-based or criteria-based sharing rule | The rule is deleted, or the record no longer matches |
| Team | An account, opportunity, or case team membership | The team member is removed |
| Territory | Territory assignment, if Territory Management is enabled | The assignment changes |
| Implicit | Child or parent relationships, such as access to an account through its opportunities | The relationship ends |
| Custom Apex reason | Apex code using a reason you define | Apex code removes it, and it survives owner change |
Two details matter in practice. First, manual shares are removed when the record owner changes, so a manual share is not a durable design. Second, row causes you define for Apex sharing (Apex sharing reasons) are kept when ownership changes, which is why developers use them for access that must persist.
Role hierarchy access is a special case. Access that flows up the hierarchy is calculated by the platform and is not stored as a row in the share object. A manager can therefore see records through the hierarchy without appearing in the share table.
How the pieces fit together
Querying a share object
Share objects are queryable with SOQL. This query lists every row for one custom object record and shows why each exists:
SELECT Id, ParentId, UserOrGroupId, AccessLevel, RowCause
FROM Invoice__Share
WHERE ParentId = '<invoice record Id>'A typical result might contain an Owner row, a Rule row for a public group, and a Manual row for one named user. The UserOrGroupId can be a user ID (prefix 005) or a group ID (prefix 00G). If it is a group, query the Group and GroupMember objects to see who is inside.
Team membership has its own queryable object. This query shows who is on an account team and what level of access each member has:
SELECT Id, AccountId, UserId, TeamMemberRole, AccountAccessLevel
FROM AccountTeamMember
WHERE AccountId = '<account record Id>'Team members also produce rows in AccountShare with the Team row cause, so you can cross-check the two. AccountTeamMember tells you the role the person was given, and AccountShare tells you what the platform stored.
Query rules and limits
- You must filter on
Id,ParentId(or the object-specific parent field), orUserOrGroupId. An unfiltered query on a share object is rejected. - Filtering on
RowCausealone is not enough, so combine it with a parent filter. - Share rows exist only for records the running user is allowed to see sharing for. Admins with View All Data see everything.
Creating a share row with Apex
You can create share rows in code. Doing so with the Manual cause is the code equivalent of clicking Sharing on a record:
Invoice__Share share = new Invoice__Share();
share.ParentId = invoiceId;
share.UserOrGroupId = userId;
share.AccessLevel = 'Read';
share.RowCause = Schema.Invoice__Share.RowCause.Manual;
Database.SaveResult result = Database.insert(share, false);
if (!result.isSuccess()) {
System.debug(result.getErrors()[0].getMessage());
}The second argument to Database.insert is false, which means partial success is allowed. A failure is returned in the result instead of thrown as an exception, so you must check isSuccess(). Common failures are an access level lower than what the user already has, a user who already owns the record, and a running user without permission to share the record. Salesforce rejects or ignores a row that grants less than existing access, because the user's effective access is already higher.
Remember that this row is removed on owner change. Chapter material on Apex sharing covers custom row causes for access that has to last.
Sharing rules in metadata
Rule rows come from sharing rule definitions. This criteria-based rule gives the Finance_Managers group read access to overdue invoices:
<?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>After this rule is deployed and recalculation finishes, each matching invoice has a row in Invoice__Share with the Rule row cause for that group. When an invoice's Status__c changes to Paid, recalculation removes the row. You cannot delete a Rule row directly. You change the rule or the record.
Common mistakes
- Querying a share object that does not exist. If the org-wide default is Public Read/Write,
Invoice__Shareis not available and the query fails to compile. - Expecting hierarchy access in the table. A manager's access through the role hierarchy will not show as a row.
- Using Manual shares for lasting access. Ownership changes silently remove them.
- Trying to insert Rule or Team rows. Only the platform writes these causes. Your code can write Manual or a custom Apex reason.
- Reading a row as the whole answer. A user may have access through several routes, and one missing row proves nothing if ownership or hierarchy grants access.
Exercise
A sales manager can open an invoice record, but a query on Invoice__Share for that record returns no row for the manager. The manager is not the owner. Name two ways the manager could still have access, and say what you would check for each.
Show answer
The manager may have access through the role hierarchy, because the manager sits above the owner in the hierarchy and that access is calculated by the platform, not stored as a row. Check the role of the owner and of the manager. The manager may also have a permission such as View All or Modify All on the Invoice object, or View All Data, from a profile or permission set. Check those permissions. A group share is another route: the row would list a group ID in UserOrGroupId, so query GroupMember to see if the manager is in that group.
Exercise
You insert an Invoice__Share row with RowCause Manual, and later the invoice's owner is reassigned. What happens to the row, and what would you use if the access had to persist?
Show answer
Salesforce removes the Manual row when the owner changes. To keep the access, define a custom Apex sharing reason on the object and insert rows with that reason, since rows that use custom reasons survive an ownership change.