function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kivacailinannekivacailinanne 

Apex Sharing: Creating the same share multiple times

Does it matter if you create duplicate Apex Sharing rows for the same Record and User?  When this is done, no errors are thrown and the duplicate share seems to succeed.

 

For example, if we take the provided example code, below, does it matter if this is executed many times?   Does it actually create duplicate rows? 

 

If it does create duplicate rows, can the duplication be stopped by changing the insert to an upsert? 

 

If that's not enough, how can one query to see if the share exists before re-creating it?

 

 // Create new sharing object for the custom object Job.
Job__Share jobShr = new Job__Share();

// Set the ID of record being shared.
jobShr.ParentId = recordId;

// Set the ID of user or group being granted access.
jobShr.UserOrGroupId = userOrGroupId;

// Set the access level.
jobShr.AccessLevel = 'Read';

// Set rowCause to 'manual' for manual sharing.
// This line can be omitted as 'manual' is the default value for sharing objects.
jobShr.RowCause = Schema.Job__Share.RowCause.Manual;

// Insert the sharing record and capture the save result.
// The false parameter allows for partial processing if multiple records passed
// into the operation.
Database.SaveResult sr = Database.insert(jobShr,false);

 

Best Answer chosen by Admin (Salesforce Developers) 
werewolfwerewolf
The API will let you create duplicate shares but it will silently eat the dupes.