Sub topic 13
Inspecting Access with Share Tables and UserRecordAccess
This chapter assumes you know the basics of org-wide defaults (OWD), record ownership, the role hierarchy, and simple SOQL. It shows how to check what the sharing model actually did, using Setup and queries, instead of guessing.
Two questions, two tools
When someone says "this user can't see the record," there are two different questions behind it.
- Who has access to this record, and why? The share table answers this.
- Can this specific user access this record, and at what level? The
UserRecordAccessobject answers this.
The two tools differ in what they include. A share table lists only the rows that grant access through sharing. UserRecordAccess reports the final result after every mechanism is applied, including object permissions, View All and Modify All, ownership, and the role hierarchy.
| Tool | Answers | Includes hierarchy and permission overrides? |
|---|---|---|
| Share table (for example `AccountShare`) | Which users and groups have a share row, and the reason | Partly. Owner and sharing-based rows appear. Some access is implicit and has no row |
| `UserRecordAccess` | What this user can do with this record | Yes. It reports the final result |
| Sharing button on a record | Who has access, in the UI | Shows a summary for a single record |
How share tables work
Salesforce stores record-level access grants in a share table for each object that supports sharing.
- Standard objects use a table named
ObjectNameShare, such asAccountShare,OpportunityShareandCaseShare. - Custom objects use
CustomObject__Share. This table exists only when the object's OWD is Private or Public Read Only. If the OWD is Public Read/Write, no share table exists, because there is nothing to open. - Contact is a special case. Contact access often follows the parent account, and
ContactShareexists only when the Contact OWD is not Controlled by Parent.
Each share row has these key fields:
| Field | Meaning |
|---|---|
| `ParentId` or `AccountId`, `OpportunityId` and so on | The record being shared. Standard objects use an object-specific name. Custom objects use `ParentId` |
| `UserOrGroupId` | The user, public group, role or role-and-subordinates group that gets access |
| `AccessLevel` or `AccountAccessLevel` | The level granted: Read, Edit or All |
| `RowCause` | Why the row exists |
Reading RowCause
RowCause is the most useful field when you troubleshoot. Common values include:
- Owner: the user owns the record. The row is created automatically.
- Team: the user is on an account team or opportunity team.
- Manual: someone used the Sharing button, or Apex inserted the row.
- Rule: a sharing rule created the row.
- ImplicitChild and ImplicitParent: access derived from related records, such as access to an account from owning one of its child contacts or opportunities.
- Territory: a territory assignment created the row.
- Custom Apex sharing reasons: defined on custom objects for programmatic sharing.
Query a share table
This query lists the share rows for one account.
SELECT Id, AccountId, UserOrGroupId, AccountAccessLevel, RowCause
FROM AccountShare
WHERE AccountId = '001XXXXXXXXXXXXXXX'Replace the placeholder with a real account ID. You can run it in the Developer Console Query Editor or in Salesforce Inspector.
What share tables do not show
The share table is not the whole story. Be careful with these gaps:
- Role hierarchy access has no per-record row for each manager. The hierarchy is evaluated through group membership. A manager can see a subordinate's record even when no
AccountSharerow names that manager. You may see a row for the owner's role group, but you will not see a row per manager. - Object permissions are not rows. A user with View All on Account has access to every account, and the share table does not list them.
- Rows can be recalculated. Changes to the role hierarchy, ownership or sharing rules cause Salesforce to recalculate access, and rows appear or disappear. A query shows the state at that moment.
- Visibility of the share table itself is limited. Users need access to the underlying object, and some share rows require sufficient permissions to read.
The practical rule is that a row proves access exists, but a missing row does not prove access is absent.
UserRecordAccess: the final answer
UserRecordAccess is a read-only object that calculates access for a user and record at query time. It takes the whole model into account, including OWD, role hierarchy, sharing rules, teams, manual shares, and the View All and Modify All permissions.
SELECT RecordId, HasReadAccess, HasEditAccess, HasDeleteAccess
FROM UserRecordAccess
WHERE UserId = '005XXXXXXXXXXXXXXX'
AND RecordId = '001XXXXXXXXXXXXXXX'The result has one row per record, with these Boolean fields:
HasReadAccessHasEditAccessHasDeleteAccessHasTransferAccessHasAllAccessMaxAccessLevel, which holds the highest level the user has: None, Read, Edit, Transfer, Delete or All
Rules for querying it
UserRecordAccess has restrictions that cause common errors.
- The
WHEREclause must filter on a singleUserId. You cannot ask about many users in one query. - You must filter on
RecordId, and you can pass up to 200 record IDs withIN. - You cannot use it in a subquery or join it to other objects. To combine results, run two queries and match the IDs in code.
- It does not support
ORDER BY,GROUP BYorLIMITin the way a normal object does. Keep the query simple. - Records of objects that do not support sharing return unexpected results. Test with a standard sharing-enabled object first.
To check several records for one user, use IN:
SELECT RecordId, HasReadAccess, HasEditAccess
FROM UserRecordAccess
WHERE UserId = '005XXXXXXXXXXXXXXX'
AND RecordId IN ('001XXXXXXXXXXXXXXX', '001YYYYYYYYYYYYYYY')Worked example
A regional manager, Priya, reports that she cannot edit an account owned by Marco, who sits in a role below her. The Account OWD is Public Read Only, and Grant Access Using Hierarchies is on.
- Query
UserRecordAccessfor Priya and the account. IfHasReadAccessis true andHasEditAccessis false, the result matches the OWD. Public Read Only gives read to everyone. Edit comes from the hierarchy only when Priya's role is above the owner's role. - Check whether Priya's role is actually above Marco's role. A reporting line in the user record's Manager field is not the role hierarchy.
- Query
AccountSharefor the account. If you see anOwnerrow for Marco and no row that names Priya or her role, the hierarchy is the only possible path to edit. Read it in Setup under Roles to confirm the position. - If Priya's role is above Marco's, check that Grant Access Using Hierarchies is on for the object. On a standard object, the hierarchy always applies. On a custom object, it applies only when the setting is checked.
- If the hierarchy does not apply, grant access with a sharing rule, an account team, or a manual share, depending on how permanent the need is.
The same query pattern works for any user and record, which makes UserRecordAccess the first check in most investigations.
Inspecting access in Setup
You do not always need a query.
- Sharing button on the record: Shows the users, groups, roles and teams with access, along with the access level and the reason. The button appears only when the OWD is not Public Read/Write and you have access to manage sharing.
- Sharing Settings in Setup: Shows the OWD of each object, the Grant Access Using Hierarchies setting, and the sharing rules for each object.
- Roles in Setup: Shows the hierarchy tree and the users assigned to each role.
- Login as a user: Lets an admin see exactly what the user sees. Use it with care and with the user's permission, where your org requires it.
Creating a share row to test a fix
You can insert a manual share row to confirm that an access gap is the cause. This is the same row the Sharing button creates.
Account acc = [SELECT Id FROM Account LIMIT 1];
AccountShare share = new AccountShare();
share.AccountId = acc.Id;
share.UserOrGroupId = UserInfo.getUserId();
share.AccountAccessLevel = 'Read';
share.OpportunityAccessLevel = 'None';
share.RowCause = Schema.AccountShare.RowCause.Manual;
insert share;Two cautions apply. Manual shares are removed when the record owner changes, so they are a weak long-term fix. Also, if the user is the record owner, the insert fails, because the owner already has full access.
Common mistakes
- Treating an empty share query as proof that nobody else has access.
- Querying
UserRecordAccesswith several user IDs, which raises an error. - Confusing the Manager field on the user record with the role hierarchy.
- Forgetting that object permissions are checked first. If the user lacks Read on Account,
UserRecordAccessand the share table cannot help, and no sharing row will fix it. - Testing only as an administrator. An administrator with Modify All Data sees everything and hides the problem.
Exercise
A support lead reports that she cannot edit a case. You query CaseShare for that case and find a row for the case owner with RowCause Owner, and no row for the lead. Does this prove the lead has no access? What do you run next, and how do you read the result?
Show answer
No. The lead could still have access through the role hierarchy, a View All or Modify All permission, or Modify All Data, and none of these create a per-user share row. Next, run a UserRecordAccess query with the lead's UserId and the case's RecordId. If HasEditAccess is false, the sharing model is not granting edit, so check her role position against the owner's role, the Grant Access Using Hierarchies setting if the object is custom, and the object permission. If HasEditAccess is true, sharing is not the cause, so look at page layouts, validation rules, record types or locked records instead.
Exercise
Why can you not run SELECT UserId, RecordId, HasReadAccess FROM UserRecordAccess WHERE RecordId = '001XXXXXXXXXXXXXXX' to find everyone who can read an account? What should you use instead?
Show answer
UserRecordAccess requires a filter on a single UserId, so it cannot list all users for one record. To see who has access to a record, query the share table (for example AccountShare) or use the Sharing button, and then remember that implicit access such as the role hierarchy and View All will not appear as rows. To confirm a particular user, run UserRecordAccess for that one user.