When to use Apex managed sharing
Key Takeaway
Choose Apex managed sharing only when sharing rules and manual sharing don't provide the access control the customer needs, such as access for a user named on a custom object record that must survive an owner change.
Apex managed sharing lets developers grant record access with code when the point-and-click sharing tools can't express the rule. The code writes share records on a custom object. Each share uses an Apex sharing reason, so the access stays in place even when the record owner changes.
Where it fits in record access
Record access is layered, and each layer does a different job:
- Organization-wide defaults set the baseline access for each object.
- The role hierarchy gives users access to records owned by or shared with their subordinates.
- Sharing rules add exceptions based on record owner or field values.
- Manual sharing lets record owners share individual records.
- Apex managed sharing is the option to use when sharing rules and manual sharing still don't provide the required access control.
You can grant sharing through the Salesforce user interface or programmatically. Apex managed sharing is the programmatic route. Developers write it in Apex or call it through SOAP API to share custom object records based on the application's own logic.