Sub topic 13
Inspecting access with share tables and UserRecordAccess
This chapter assumes you know basic SOQL, understand org-wide defaults (OWD), the role hierarchy, and the difference between object permissions and record access. Here you will check access with queries instead of guessing from the settings.
Two ways to ask about access
Salesforce gives you two views of record access.
- Share tables list the rows that grant access. They answer "why does anyone have access?"
- UserRecordAccess computes the final result for one user and one record. It answers "can this person do this?"
Use share tables to explain access. Use UserRecordAccess to confirm it.
Share tables
Each object that uses sharing has a share table. For Account it is AccountShare. For a custom object it is MyObject__Share. The table stores one row for each user or group that has been given access to a record.
SELECT Id, AccountId, UserOrGroupId, AccountAccessLevel, RowCause
FROM AccountShare
WHERE AccountId = '001XXXXXXXXXXXXXXX'The important columns are:
| Column | Meaning |
|---|---|
| `AccountId` | The record that is shared. The column name varies by object. |
| `UserOrGroupId` | The user or group that receives access. |
| `AccountAccessLevel` | The level granted: Read, Edit or All. |
| `RowCause` | The reason the row exists, such as Owner, Team, Manual or a sharing rule. |
RowCause is the useful column when you troubleshoot. An Owner row means access comes from ownership. A Manual row means someone used the Sharing button. A Team row means the user is on the account team.
Also, if the OWD for an object is Public Read/Write, share rows are not created for most users, because the default already grants access. Share tables are most informative for Private and Public Read Only objects.
UserRecordAccess
UserRecordAccess takes a user and a record, then reports the combined result of OWD, ownership, hierarchy, sharing rules, teams, manual shares and permissions such as View All.
SELECT RecordId, HasReadAccess, HasEditAccess, HasDeleteAccess
FROM UserRecordAccess
WHERE UserId = '005XXXXXXXXXXXXXXX'
AND RecordId = '001XXXXXXXXXXXXXXX'Each query must filter on one UserId and a RecordId (or a small list of record IDs). You cannot query it without these filters.
Common errors
- Querying a share table without a filter. Filter on the parent record ID (for example
AccountId) or you may see far more rows than you expect. - Treating an empty share table as "no access". The hierarchy, OWD and View All can grant access without a row.
- Using the wrong ID.
UserIdmust be a user ID that starts with005, not a role or group ID. - Reading
HasReadAccessas full permission. It covers record access only.
A short workflow
- Run
UserRecordAccessfor the user and record to see the outcome. - If access is missing, query the share table to see who does have access and why.
- Compare the owner's role with the user's role to check the hierarchy.
- Review the OWD and the user's object permissions.
Exercise
A manager can read a subordinate's Account, but AccountShare has no row for the manager on that record. Is this a problem? What would you check to confirm where the access comes from?
Show answer
It is not a problem. The access most likely comes from the role hierarchy, which does not appear as a share row for each record. To confirm, run UserRecordAccess for the manager and the Account to see HasReadAccess. Then check that the manager's role is above the owner's role and that Grant Access Using Hierarchies is enabled for the object.