Org Skills

Sharing design — layers, Apex sharing, external users

Supporting material for salesforce-data-model. Agents load it on demand; it ships inside the skill folder.

RawSource

Target: Spring '26. Sources: Sharing Rules, Restriction Rules, Apex Sharing, Experience Cloud licences.

This file is the sharing design reference. Enforcing access in code (with sharing, WITH USER_MODE, Security.stripInaccessible, Shield, Event Monitoring) belongs to salesforce-security.

1. The layers, in the order you design them

Layer What it does Design note
Org-Wide Defaults The baseline per object: Private, Public Read Only, Public Read/Write, Controlled by Parent Start Private for anything with an owner. OWD can only be opened by the layers below — nothing except restriction/scoping rules narrows it
Role hierarchy Managers inherit access to records owned below them Custom objects can switch this off ("Grant Access Using Hierarchies"); standard objects cannot
Sharing rules Owner-based or criteria-based grants to roles, public groups, territories Declarative and automatically recalculated; the default answer for "team X also needs to see Y"
Manual share / Apex managed sharing A grant on one record (Object__Share with RowCause) For patterns rules cannot express. Apex shares with a custom RowCause survive owner changes; manual shares do not
Teams and territories Account, Opportunity and Case teams; Enterprise Territory Management Territory sharing is recalculated on model changes — expensive on LDV
Implicit sharing Account → child Contact/Case/Opportunity access, portal user → their account Not configurable; account for it before building custom sharing
Restriction rules / scoping rules Narrow below what sharing grants (restriction) or set a default filtered view (scoping) The only "subtractive" layer. Scoping rules are a view default, not a security control
Permission sets and groups Object CRUD, FLS, tabs, Apex/VF access, custom permissions Assign through permission set groups; keep profiles to login hours, defaults, licence — plus the org's mandatory FLS on the four admin profiles
Field-level security Per-field read/edit The API honours it; the UI is not the control

2. Sharing matrix — the deliverable

Object OWD (internal / external) Hierarchy Sharing rules Apex sharing External users Notes
Account Private / Private on Criteria: Region = EMEA → EMEA Sales (Read/Write) — Partner users via account-based sharing Owner = account executive
Order__c Private / Private on Owner-based: Sales → Service (Read Only) Order__Share RowCause Fulfilment_Partner__c for the shipping vendor, rebuilt nightly Customer Community via Sharing Set on Account Recalculation job: OrderShareRecalcBatch
Price_Book_Entry__c Public Read Only / Private n/a — — none Reference data

3. Apex managed sharing — the parts people forget

  • Create the share row with the child share object: Order__Share has ParentId, UserOrGroupId, AccessLevel (Read/Edit) and RowCause. A custom RowCause (defined as a sharing reason on the object) is what makes the share survive an owner change; Manual rows are deleted when ownership changes.
  • Inserting shares needs the code to run in a context with "Modify All" on the object, so it typically lives in a without sharing class with a comment saying why.
  • Write the recalculation at the same time as the grant: a Batch Apex class registered as an Apex sharing recalculation for the object, plus a scheduled run. Without it, a role change or a data load silently leaves people with the wrong access.
  • Test with System.runAs for a user who should see the record and one who should not; assert on a USER_MODE query, not on the share row.
  • On LDV objects, sharing recalculation is the expensive operation: batch OWD/role/sharing-rule changes into a window and expect hours, not minutes.

4. Experience Cloud — decide the licence before the model

Licence Sharing mechanism Use for
Customer Community Sharing Sets (grant by the user's Account/Contact relationship) and Share Groups High-volume B2C portals; users have no roles, so sharing rules and the role hierarchy do not apply to them
Customer Community Plus Roles, sharing rules, and reports B2B customer portals needing hierarchy or delegated admin
Partner Community Roles, sharing rules, leads and opportunities Channel partners working your pipeline
Employee/Internal Full internal sharing model Intranets

The consequence: a model designed around sharing rules does not work for Customer Community users — they need Sharing Sets keyed on a lookup back to their Account or Contact. Check this before drawing the ERD, not after the portal is built.

5. Automating assignment

  • Permission Set Groups with muting permission sets express "this job minus that one capability" — cleaner than cloned permission sets.
  • User Access Policies assign permission sets, groups, package licences and queues automatically when a user matches criteria (for example department and profile), and can be run against existing users. Use them instead of a record-triggered flow on User.
  • Custom permissions are the switch automation and code should check (FeatureManagement.checkPermission, $Permission in flows), not profile names.

6. Checklist for a sharing design

  • Every object has an explicit OWD for internal and external users
  • Each grant names the layer it uses and who owns it
  • Nothing relies on hidden UI elements or page layouts for access control
  • Apex shares use a custom RowCause and have a recalculation batch plus a schedule
  • Restriction rules used where visibility must be narrowed, with an owner and a review date
  • Experience Cloud licence and its sharing mechanism chosen before the model is fixed
  • System.runAs tests exist for one permitted and one denied user per sensitive object
  • Recalculation cost estimated for LDV objects and scheduled off-hours