1

We have a custom object that has a lookup to account (similar to Contact looking up to Account). We want the user who owns an account to see the records looking up to that account automatically without any user/admin intervention. The functionality would be similar to sharing setting "Controlled by parent". However, custom objects do not have this option. We want to replicate this option for custom object.

Other solution I have looked at is using criteria-based or owner-based sharing. However, that does not allow dynamic sharing (i.e., share with owner of the account) and is limited to sharing with previously described roles and groups, etc.

We want to make sure that the users can see the record as the new records are created with zero manual work. (e.g., Apex Trigger, Apex Class). This solution needs to be scalable with hundreds of thousands of records so manual sharing will not work and groups, roles, permissions sets may not be ideal.

  • 1
    The other option is to make it master detail with account . – Mohith Shrivastava May 30 '16 at 21:50
  • Thanks Muhith. I found this link on the comparison very helpful: youtube.com/watch?v=fUD4MzgA0gk – Jorjani May 31 '16 at 15:17
  • As suggested by others you either need a master derail relationship or apex managed sharing. I have successfully used visual workflow to automate sharing a number of times. If the volume of records is high I would def go w apex, but if not visual workflow can do it easily. The bulkification on flows is not as robust as it needs to be which is the main drawback. – gorav Jun 1 '16 at 1:01
1

"Controlled by parent" option is only available in Master-Detail relationship.

You can use Apex to share records. Every custom object has its Share object created by default; __Share. You only get the Share object created if OWD is Private/Read Only.

So if custom object name is Job, it will look like this:

List<Job__Share> jobShareList = new List<Job__Share>();
for(Job__c job : Trigger.new){
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.Can be Edit, Read
jobShr.AccessLevel = 'Edit';
jobShr.RowCause = Schema.Job__Share.RowCause.Manual;
jobShareList.add(jobShr);
}

Database.SaveResult sr = Database.insert(jobShareList);

This will handle the records in bulk.

3

The solution that you're looking for would be Apex Managed Sharing. The most robust method of deploying it for the number of records you're talking about would be using triggers that which write records that need to be shared to a custom object. On insert of the records to the custom object, another trigger fires which creates an @future call to initiate a queuable class. The queueable queries the custom object for records that need to be shared. Once shared, it deletes those records.

If there's an error, it marks the record for requeuing (depends on the severity) and calls another instance of the queueable class to query records and try to process the record again along with new ones that have been added. If it still can't process the sharing on the record, ideally, it sends and email to an admin for manual intervention, using an error handler class and marks the record as "do not process" until the admin intevenes to fix any problems he or she finds.

Variations on this pattern are generally described in Dan Appleman's Advanced Apex Programming along with sample code. I've used the pattern along with my own data error handler class and can say that it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.