Sub topic 7
Sharing Model and Record Visibility
Object and field permissions decide what kinds of data the integration user can touch. The sharing model decides which individual records it can see. This chapter assumes you have read the previous chapters on permission sets and object and field-level permissions. It also assumes you know the basics of roles, record ownership, and SOQL.
Two Questions, Two Layers
Every API request from the integration user passes two checks.
- Can the user work with this object and field? Profiles, permission sets, and field-level security answer this.
- Can the user see or change this particular record? Organization-wide defaults, the role hierarchy, sharing rules, and other sharing mechanisms answer this.
A user needs both. An integration user with Read on Account and a Private default for Account still sees only a small subset of accounts.
Failing the second check usually produces no error. A query returns fewer rows, or none. This is the most common cause of an integration that "works in the sandbox but returns empty results in production". It is also the cause of "the integration sees everything" when someone has granted View All Records to fix the first problem.
The diagram shows why View All Records is risky. It skips every sharing check for that object, so it works as an override and not as a sharing setting.
Organization-Wide Defaults
The organization-wide default (OWD) sets the baseline access for each object. It is the most restrictive level of access that any user can have. The other sharing tools only open access up from there.
| OWD setting | What a non-owner can do |
|---|---|
| Private | Nothing, unless another mechanism grants access |
| Public Read Only | View records, not edit |
| Public Read/Write | View and edit records |
| Controlled by Parent | Inherit access from the parent record |
You find these under Setup > Sharing Settings. Check the OWD for each object your integration uses before you decide how to grant record access.
Two consequences matter for an integration user:
- If the OWD is Public Read Only, the integration user can read every record with no extra sharing configuration. A read-only integration needs nothing more. Be aware that this means it reads all records of that object, so least privilege there depends on the object permission and field-level security.
- If the OWD is Private, the integration user sees only records it owns plus records shared with it. This is the case where you must design record access on purpose.
Changing an OWD affects every user in the org, not only the integration user. Don't loosen a default just to make an integration work.
Ways to Give the Integration User Record Access
When the OWD is Private, you have several options. They differ in how precisely they can limit access.
| Mechanism | What it does | Fit for an integration user |
|---|---|---|
| Role hierarchy | Users higher in the hierarchy see records owned by users below them | Weak. It gives access to everything owned by a whole branch |
| Criteria-based sharing rule | Shares records that match field criteria with a public group, role, or role and subordinates | Good for narrow, read-only access, such as accounts where `Type = 'Customer'` |
| Owner-based sharing rule | Shares records owned by members of a group or role | Good when the data you need is owned by a known set of users |
| Manual sharing | A user shares one record at a time | Poor. It does not scale and isn't repeatable |
| Account or opportunity teams | Adds a user to a record's team | Rarely used for integrations |
| Apex managed sharing | Code creates share records | Use only for complex rules |
| View All / Modify All | Bypasses sharing for the object | Avoid unless the integration truly needs every record |
Sharing rules can only widen access beyond the OWD, never narrow it. They can also grant only Read Only or Read/Write. They don't grant create or delete rights, which come from object permissions.
Use a Public Group for the Integration User
Target your sharing rules at a public group and not at the user directly. Create a group such as Integration_Users, add the integration user as a member, and share records with that group. A second integration can then be added to the group later, and the rule stays readable in the audit.
Create the group under Setup > Public Groups. Then create a criteria-based sharing rule under Setup > Sharing Settings. For example, share Accounts where Type equals Customer with the group Integration_Users, with Read Only access.
The Role Hierarchy Question
An integration user doesn't need a role. If you leave the Role field empty, the user:
- sees records it owns
- sees records shared with it through the mechanisms above
- does not inherit access to records owned by anyone below it
If you assign a role, the user sees everything owned by users in roles beneath it, whenever the object has Grant Access Using Hierarchies enabled. That setting is always on for standard objects. A role placed high in the hierarchy gives an API-only account wide read access without any visible sharing rule, which is hard to spot in a review.
The reverse also applies. Records the integration user creates are owned by it. If it has a role, users in roles above it see those records through the hierarchy. If it has no role, nobody sees them unless the OWD is public or a sharing rule applies. Plan for this in the write path.
Records the Integration User Creates
When the integration inserts a record, OwnerId defaults to the integration user. With a Private OWD, business users often can't see those records. You have three choices:
- Set
OwnerIdin the payload to a queue or to a business user. The integration user needs permission to do this, and a queue must support the object. - Create an owner-based sharing rule that shares records owned by the
Integration_Usersgroup with the sales or service group that needs them. - Rely on assignment rules, where the object supports them.
Pick one deliberately. If you skip it, you find out when the first business user asks where the records went.
Related Objects and Parent Access
For child objects in a master-detail relationship, the OWD is Controlled by Parent. Access to the child follows access to the parent. A user who can't see the parent Account can't see Contacts controlled by it.
Lookup relationships behave differently. The related object has its own OWD and its own sharing. The integration user can see an Opportunity and still see no related Account name, because lookup fields to a record the user can't see return null.
Check these relationships when you build the list of objects in your requirements. Missing access to a parent is a quiet cause of incomplete data.
Custom Apex and Sharing
If the integration calls a custom Apex REST class, the sharing keyword on the class matters. A class declared with sharing respects the running user's sharing. A class declared without sharing, or one with no keyword that is called from another context, can expose records the integration user could not otherwise see. Review any Apex class the integration user can call for this reason. Class access is a permission set setting, so you can limit it there.
@RestResource(urlMapping='/accounts/*')
global with sharing class AccountService {
@HttpGet
global static List<Account> getAccounts() {
return [SELECT Id, Name, Industry FROM Account LIMIT 100];
}
}With with sharing, this query returns only accounts the integration user can see. Removing the keyword changes that behavior. The default for a class without a keyword depends on how it is invoked, so state the keyword explicitly.
Verifying Record Access
Don't rely on reading the sharing settings alone. Check what the user can actually see. The UserRecordAccess object reports the effective access of a user on specific records, and it accounts for every sharing mechanism at once.
SELECT RecordId, HasReadAccess, HasEditAccess, HasDeleteAccess, MaxAccessLevel
FROM UserRecordAccess
WHERE UserId = '005XXXXXXXXXXXXXXX'
AND RecordId IN ('001XXXXXXXXXXXXXXX', '001YYYYYYYYYYYYYYY')The query requires a UserId filter and a RecordId filter, and it accepts a limited number of record IDs per query. Pick test records that should be visible and records that should not be, and confirm both results.
To see why a record is visible, query the share table for the object. Each object has one. For Account it is AccountShare:
SELECT UserOrGroupId, AccessLevel, RowCause
FROM AccountShare
WHERE AccountId = '001XXXXXXXXXXXXXXX'The RowCause field tells you the reason, such as Owner, Rule, Manual, or Team. Records visible because of a public OWD don't have a row for every user. A missing row therefore doesn't always mean "no access". Use UserRecordAccess for the final answer.
Finally, run the real query as the integration user and compare the count with an admin's.
sf data query \
--target-org integration-sandbox \
--query "SELECT COUNT() FROM Account"Run the same query as an admin in the same org. If the integration user's count equals the admin's total and you didn't intend that, look for View All Records, a high role, or a public OWD.
Common Mistakes
- Granting View All Records to fix empty results. This removes the sharing layer for the whole object. Add a targeted sharing rule instead.
- Putting the integration user in a senior role. It then reads whole branches of the hierarchy.
- Changing the OWD for one integration. The change applies to every user.
- Forgetting ownership of created records. Users report missing data because the records belong to the integration user.
- Testing only as an admin. Admins see everything, so a broken sharing design looks fine.
Exercise
An integration user has Read permission on Account, no role, and no View All Records. The Account OWD is Private. A query as this user returns zero accounts, although the org has thousands. What is the least-privilege fix if the integration should read only accounts with Type = 'Customer'?
Show answer
Add the integration user to a public group such as Integration_Users, then create a criteria-based sharing rule that shares Accounts where Type equals Customer with that group, at Read Only access. Don't grant View All Records, don't change the OWD, and don't assign a senior role. Verify afterward with a UserRecordAccess query for one Customer account and one non-Customer account.
Exercise
The integration user inserts Contact records, and sales staff say they can't see them. The Contact OWD is Private and the integration user has no role. Name two ways to fix this without giving the staff broader access to other contacts.
Show answer
Set OwnerId in the integration payload to the appropriate user or queue, or create an owner-based sharing rule that shares records owned by the Integration_Users group with the sales group. Either way, the visibility of other contacts stays unchanged.