Salesforce Dictionary - Free Salesforce GlossarySalesforce Dictionary
Full Apex-Managed Sharing entry
How-to guide

How to implement Apex Managed Sharing safely

Apex Managed Sharing is the most complex part of Salesforce sharing. Take the implementation in three deliberate steps: create the Apex Sharing Reason, write the granting code, then write the recalculation code that keeps shares accurate as data changes.

By Dipojjal Chakrabarti · Founder & Editor, Salesforce DictionaryLast updated May 19, 2026

Apex Managed Sharing is the most complex part of Salesforce sharing. Take the implementation in three deliberate steps: create the Apex Sharing Reason, write the granting code, then write the recalculation code that keeps shares accurate as data changes.

  1. Create an Apex Sharing Reason

    Setup, Object Manager, open the custom object, then Apex Sharing Reasons. Create a reason with a clear name (e.g. Specialty_Engineer_Access). The reason becomes the RowCause on every __Share row your code creates.

  2. Write a trigger or service class that creates shares

    On the right trigger event (insert, update of the driving field), collect the eligible record-user pairs, instantiate __Share rows with the right AccessLevel and RowCause, and insert them in bulk. Run with FOR UPDATE locking or sharing.share() inside SOQL when concurrent edits are likely.

  3. Handle revocation on data change

    When the data driving the share changes (specialty reassignment, stage rollback), delete the existing share rows with the Apex Sharing Reason and re-insert the new set. Test that no orphan share rows survive a change.

  4. Implement Apex Sharing Recalculation

    Create a class that implements the sharing recalculation interface and register it on the object. The platform calls it during recalculation events, including manual triggers from Sharing Settings.

  5. Audit and monitor

    Build a SOQL query that compares expected shares to actual __Share rows for a sample of records. Schedule it weekly. Drift is the most common failure mode of long-lived Apex Managed Sharing implementations.

__Share objectrequired

The auto-generated sharing object for the target object.

ParentIdrequired

The record being shared.

UserOrGroupIdrequired

The recipient user, public group, role, or queue.

AccessLevelrequired

Edit, Read, or All.

RowCauserequired

Manual on standard objects, or the Apex Sharing Reason on custom objects.

Apex Sharing Reasonrequired

Admin-defined RowCause for custom objects; required for safe recalculation.

Gotchas
  • Standard objects do not support custom Apex Sharing Reasons. Manual is the only practical RowCause, which complicates safe recalculation.
  • Forgetting to revoke shares when the driving data changes leaves stale access in place; this is the most common audit finding.
  • Apex Managed Sharing share rows count against DML and storage. Mass grant patterns can blow row limits at scale.
  • Recalculation triggered from Sharing Settings runs synchronously and can time out on large orgs. Plan to test recalculation against full-volume sandboxes before relying on the button.

See the full Apex-Managed Sharing entry

Apex-Managed Sharing includes the definition, worked example, deep dive, related terms, and a quiz.