3.1 - Programmatic Sharing - Programmatic Apex Sharing
3.1 - Programmatic Sharing - Programmatic Apex Sharing
• Sharing Reasons
• Apex Managed Sharing allows developers to build sophisticated and dynamic sharing settings
• Available for objects with a default OWD of ‘Private’ or ‘Public Read Only’
• Only users with Modify All Data permission can add or change Apex managed sharing on a record
• A Share object exists for a Salesforce object if the OWD sharing settings for this object is
restrictive: Private or Public Read only
• If an Object is Public Read/Write, the Share object table has 0 records; there is no need to share as it
is already publicly Read/Write
• Objects on the detail side of a master-detail relationship do not have an associated sharing object.
The detail record’s access is determined by the master’s sharing object.
• Explicit Sharing:
• User managed sharing (manual sharing by the owner or a User with Full Access permission on the record)
• Implicit sharing
• Apex Managed Sharing is creating records in the Share object through Apex
• The Share object has 0 records until you make the object OWD restrictive
• There is no Share object until you make the object OWD restrictive
• SOQL Query
SELECT Id
FROM OpportunityShare
• SOQL Query
SELECT Id, OpportunityAccessLevel,
OpportunityId, RowCause,
UserOrGroupId
FROM OpportunityShare
• SOQL Query
• SOQL Query
SELECT Id, AccessLevel,
ParentId, RowCause,
UserOrGroupId
FROM Invoice__Share
• RowCause explains the reason the record is shared to the specified User or Group
• Standard Share objects like OpportunityShare will NOT have write access on RowCause field
RowCause Description
Owner The specified User is the record owner
ImplicitParent A child record related to this record is owned by the specified User
Team The specified User is a Team member (ex. Account Team)
Manual Sharing was manually granted to the specified User
TerritoryManual The Account record was manually assigned to a Territory
Territory A Territory assignment rule granted access for this Account to the specified Group
Apex sharing Only available for custom objects
reason
MyReasonName__c
Schema.CustomObject__Share.rowCause.SharingReason__c
Schema.Job__Share.rowCause.Recruiter__c
• Standard sharing functionality is not sufficient, and sharing logic is too complex to be
established declaratively.
• Sharing access is criteria-based but the evaluated field isn’t supported by declarative sharing
• these can be created by navigating to the ‘Apex Sharing Reasons’ related list of an object.
2. While creating Apex code for sharing a record, the following fields must be defined:
• UserOrGroupId is the ID of the user or public group to whom access is being granted.
• RowCause is used for specifying the reason why the user or group is being granted access.
Note that either a custom or ‘Manual’ RowCause can be used in Apex code to create records
in share tables. If left blank, it will be ‘Manual’
• Standard Object
OpportunityShare oppShare = new OpportunityShare();
oppShare.OpportunityAccessLevel = 'Read';
oppShare.OpportunityId = '0061U000002szd9QAA';
oppShare.UserOrGroupId = '0051U0000010dxpQAA';
oppShare.RowCause = Schema.OpportunityShare.RowCause.Manual; //optional – Manual is default
insert oppShare;
• Custom Object
Invoice__Share invShare
OpportunityShare oppShare
= new= new
Invoice__Share();
OpportunityShare();
oppShare.OpportunityAccessLevel
invShare.AccessLevel = 'Read'; = 'Read';
oppShare.OpportunityId
invShare.ParentId = 'a001U000000yS7yQAE';
= '0061U000002szd9QAA';
oppShare.UserOrGroupId=='0051U0000010dxpQAA';
invShare.UserOrGroupId '0051U0000010dxpQAA';
oppShare.RowCause==Schema.Invoice__Share.RowCause.Walid_Reason__c;
invShare.RowCause Schema.OpportunityShare.RowCause.Manual; //optional
//optional
– Manual is default
insert invShare;
oppShare;
• Example from
the Developer
Guide
• Test Class
• LINK
22
Apex Managed Sharing Considerations
• Only users with ‘Modify All Data’ permission can add, edit or delete apex managed
sharing records.
• A record can be shared multiple times with a user or group using different Apex sharing
reasons.
• When multiple entries in the share object apply for the logged-in user, the most-
permissive rule applies
• Apex managed sharing is maintained across record owner changes if the RowCase is not
‘Manual’
Demo
Programmatic
Apex Sharing
24
Summary - Programmatic Apex Sharing
Subject Description
Apex Sharing Allows developers to build sophisticated and dynamic sharing settings
Only users with Modify All Data permission can add or change Apex managed sharing on a record
Share Object Apex Managed Sharing is creating records in the Share object through Apex
Standard objects: AccountShare
Custom Object: Invoice__Share
Types of Sharing A share object includes records supporting:
Explicit Sharing (Apex Managed Sharing is here) / Implicit Sharing
Share Object ParentId: corresponds to the record being shared.
Columns UserOrGroupId: is the ID of the user or public group to whom access is being granted.
AccessLevel: can be either ‘Read’ or ‘Edit’.
RowCause: specifies the reason why the user or group is being granted access
Sharing Reason Can only be defined for Custom Objects, and not Standard Object
If the owner of record is changed, all Share records with the Manual reason are deleted
Deleting an Apex sharing reason will delete all sharing on the object that uses it
Example: OppShareRecord.RowCause = Schema.OpportunityShare.RowCause.Manual;