Create a Trigger to Maintain Lead Visibility

Add a trigger that grants sharing access to the person who created the lead even when they’re no longer the lead’s owner. Granting sharing access enables the lead’s creator to track the referral after it’s converted to an opportunity.

Available in: Lightning Experience

Available in: Enterprise and Unlimited Editions with Health Cloud


Instead of using Lead and Opportunity objects to manage referrals, use the Clinical Service Request object and automation tools built for the FHIR R4-aligned Clinical Data Model. To benefit from new and enhanced referral management features in the future, you must use objects in the Clinical Data Model.

Important

To access the LeadShare and OpportunityShare objects, sharing for the Lead and Opportunity objects must be set to Private or Public Read Only.

  1. From Setup, select Customize and then click Lead.
  2. Click Triggers and then click New.
  3. To define your trigger, enter Apex code similar to this sample code.
    1trigger ReferralShare on Lead (after update) {
    2    for(Lead lead : Trigger.new){
    3        try{
    4            Lead oldLead = Trigger.oldMap.get(lead.Id);
    5            // find the Group that the Lead creator is part of
    6            Group grp = [SELECT Id FROM Group WHERE Type='RoleAndSubordinates' 
    7                            AND RelatedId in (select UserRoleId from User where Id= :lead.CreatedById)];
    8            // Lead trigger sharing example - if the owner changed and the owner is different than the creator, 
    9            // share the Lead with the Group of the user that created the Lead
    10            if (lead.CreatedById!=lead.OwnerId && lead.OwnerId!=oldLead.OwnerId){
    11                // create a new LeadShare record for the creator's Group
    12                LeadShare leadShare = new LeadShare(LeadId=lead.Id, UserOrGroupId=grp.Id, LeadAccessLevel='Read', RowCause='Manual');
    13                Database.SaveResult save = Database.insert(leadShare,false);
    14                if (save.isSuccess()){
    15                    System.debug(LoggingLevel.ERROR, 'SAVE RESULT SUCCESS');
    16                }
    17                else {
    18                    for(Database.Error err : save.getErrors()){
    19                        System.debug(LoggingLevel.ERROR, 'SAVE RESULT ERROR: ' + err.getMessage());
    20                    }
    21                }
    22            }
    23            // Opportunity sharing example - on Lead conversion, add an Opportunity sharing record for the Group of
    24            // the user that created the Lead
    25            if (lead.IsConverted){
    26                // create a new OpportunityShare record for the creator's Group
    27                OpportunityShare opptyShare = new OpportunityShare(OpportunityId=lead.ConvertedOpportunityId, UserOrGroupId=grp.Id, 
    28                                                                   OpportunityAccessLevel='Read', RowCause='Manual');
    29                Database.SaveResult save = Database.insert(opptyShare,false);
    30                if (save.isSuccess()){
    31                    System.debug(LoggingLevel.ERROR, 'SAVE RESULT SUCCESS');
    32                }
    33                else {
    34                    for(Database.Error err : save.getErrors()){
    35                        System.debug(LoggingLevel.ERROR, 'SAVE RESULT ERROR: ' + err.getMessage());
    36                    }
    37                }
    38            }
    39        }
    40        catch(Exception ex){
    41            System.debug(LoggingLevel.ERROR, 'EXCEPTION: ' + ex.getMessage());
    42        }
    43    }  
    44}