Sub topic 7
Sharing Model and Record Visibility
Permission sets decide which objects and fields the integration user can touch. The sharing model decides which records of those objects it can see. This chapter assumes you know organization-wide defaults (OWD), roles, and the basic SOQL you used in earlier chapters. Both layers must allow access before a query returns data.
Two layers of access
Object and field permissions answer "can this user work with Accounts at all?" Sharing answers "which Account rows?" A user with Read on Account and a Private OWD sees only the records it owns, plus records shared with it in some other way.
| Layer | Question it answers | Where you set it |
|---|---|---|
| Object permission | Can the user read, create, edit, or delete this object? | Permission set |
| Field-level security | Which fields can the user see or change? | Permission set |
| Sharing | Which records can the user see? | OWD, roles, sharing rules, manual shares |
How a record becomes visible
A record is visible to the integration user if any of these is true:
- The user owns it.
- The OWD for the object is Public Read Only or Public Read/Write.
- The user sits above the owner in the role hierarchy, and the object uses the hierarchy for access.
- A sharing rule shares the record with a role, group, or the user.
- The record is shared manually, or through a team or an Apex managed share.
- The user has View All or Modify All on the object, or View All Data or Modify All Data.
Items 1 to 5 grant access to specific records. Item 6 bypasses sharing completely. That is why the permission set in the earlier chapter set viewAllRecords and modifyAllRecords to false.
Choose the narrowest sharing option
Start by finding out which records the integration needs. Then pick the narrowest way to give it that access.
- Integration owns the records. If it only creates and updates its own records, you need no extra sharing.
- Records of a known subset. Use a criteria-based sharing rule, for example Accounts where
Industry = 'Energy', shared with a public group that contains only the integration user. - Records from many owners. Put the integration user in a role that is placed deliberately in the hierarchy. Remember that roles grant access to everything below them for objects that use the hierarchy.
- Everything. Use View All on that one object. Treat it as a documented exception and not as the default.
Avoid changing the OWD to make one integration work. That change opens the object to every internal user.
Check what the user actually sees
Query the same object as an administrator and as the integration user, then compare the counts. In the sandbox, run this with the integration login from the previous chapter:
sf data query \
--target-org integration-sandbox \
--query "SELECT COUNT() FROM Account"Run the same query as an administrator. A smaller count for the integration user is expected when sharing works. Identical counts on a Private object suggest the user has View All, Modify All, or a broad role or rule.
You can also check why a given record is visible. Query the share table as an administrator. The following example lists the access rows for one Account:
SELECT UserOrGroupId, AccountAccessLevel, RowCause
FROM AccountShare
WHERE AccountId = '001XXXXXXXXXXXXXXX'RowCause tells you the reason, such as Owner, Rule, or Manual. A cause you did not plan for points to a rule or share to remove.
Common errors
- Empty results with no error. The user has object Read but cannot see the records. Add a sharing rule. Do not add View All as a quick fix.
- A "not found" response for a known record ID. Record-level access is missing, so Salesforce treats the record as non-existent for this user.
- Update fails on a visible record. Visibility is not edit access. Check that the sharing rule grants Read/Write, and that the permission set has Edit.
- Apex integrations behave differently. Code declared
without sharingignores these rules. Keep integration entry pointswith sharingunless there is a documented reason.
Exercise
The integration user has Read on Opportunity, View All is off, and the Opportunity OWD is Private. A query returns zero rows, although many Opportunities exist. Name two least-privilege ways to fix this, and one fix you should avoid.
Show answer
Fix with a criteria-based sharing rule that shares the needed Opportunities with a public group containing only the integration user, or have the integration own the records it needs. Avoid granting View All Data or View All on Opportunity, and avoid changing the OWD to Public Read Only, because both give access to far more records than the integration needs.