[SalesForce] Apex Managed Sharing Rule to share master object after insert of detail object

I am developing a team model like Opportunity Teams for a custom object (e.g. CustomObject). I have developed the necessary objects and fields. I have also developed a Visualforce page to create the CustomObjectTeam records in bulk. CustomObject and CustomObjectTeam have a master-detail relationship, where CustomObjectTeam is the detail. I know that 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 and the relationship’s sharing setting.

Using an after insert trigger on CustomObjectTeam, I need to ensure that a User was added and insert a CustomObject__Share record. Using Apex Managed Sharing Rules, what it the proper way to write a trigger to share a CustomObject record with the user indicated (i.e. Team_Member__c user lookup) on a CustomObjectTeam record?

Example code is appreciated. Below is my start at the code.

// On Insert of the CustomObjectTeam record, ensure that a User was added and insert a CustomObject__Share record
trigger CustomObjectApexSharing on CustomObjectTeam__c (after insert) {

// We only execute the trigger after a CustomObjectTeam record has been inserted 
// because we need the Id of the CustomObjectTeam record to already exist.
if(trigger.isInsert){

// CustomObject__Share is the "Share" table that was created when the Organization
// Wide Default sharing setting was set to "Private". CustomObjectTeam does not have a "Share" table.
// 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 and the relationship’s sharing setting.
// Allocate storage for a list of CustomObject__Share records.
List<CustomObject__Share> coShares  = new List<CustomObject__Share>();

// For each of the CustomObjectTeam records being inserted, do the following:
for(CustomObjectTeam__c co : trigger.new){

    // Create a new CustomObject__Share record to be inserted in to the CustomObject__Share table.
    CustomObject__Share teamMemberShare = new CustomObject__Share();

    // Populate the CustomObject__Share record with the ID of the record to be shared.
    teamMemberShare.ParentId = co.Id;

    // Then, set the ID of user or group being granted access. In this case,
    // we’re setting the Id of the Team Member that was specified by 
    // the CustomObject Administrator in the Team_Member__c lookup field  
    // on the CustomObjectTeam record. 
    teamMemberShare.UserOrGroupId = co.Team_Member__c;

    // Specify that the Team Member should have read, not edit, access for 
    // this particular CustomObject record.
    teamMemberShare.AccessLevel = 'read';

    // Specify that the reason the Team Member can read the record is 
    // because he/she is the part of the Team for the CustomObject.
    // (Team_Member_Access__c is the Apex Sharing Reason defined earlier.)
    teamMemberShare.RowCause = Schema.CustomObject__Share.RowCause.Team_Member_Access__c;

    // Add the new Share record to the list of new Share records.
    coShares.add(teamMemberShare);
}

// Insert all of the newly created Share records and capture save result 
Database.SaveResult[] coShareInsertResult = Database.insert(coShares,false);

// Error handling code omitted for readability.
}

}

Best Answer

I think this line:

teamMemberShare.ParentId = co.Id;

actually be

teamMemberShare.ParentId = co.Custom_Object__c;

Since the sharing for CustomObjectTeam is dictated by its Parent Custom_Object__c

Related Topic