Wednesday, February 13, 2013

Sharing Records using Trigger:

Sharing Records using Trigger:
This example shows how to share records after inserting depends on user you selected before creating the record.The trigger looks like this
trigger Hiring_Manager_Position_Share on Position__c (after insert) {
    if(trigger.isInsert){
        List<Position__Share> posShare = new List<Position__Share>();
        for(Position__c pos : trigger.new){
            Position__Share hiringManagerShare = new Position__Share();
            hiringManagerShare.ParentId = pos.Id;
            hiringManagerShare.UserOrGroupId = pos.Hiring_Manager__c;
            hiringManagerShare.AccessLevel = 'edit';
            hiringManagerShare.RowCause = Schema.Position__Share.RowCause.Hiring_Manager__c;
            posShare.add(hiringManagerShare);
        }
        Database.SaveResult[] posShareInsertResult = Database.insert(posShare, false);  
    }
}



No comments:

Post a Comment