---
schema_version: "1"
id: 32ad6ef64c7d7a4dacda036010bb4309ba967cb7d634abf1fb8a0e7eb61a13e7
title: Create a Trigger to Maintain Lead Visibility
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/ind/264-0-0/admin_referral_management_sharing_trigger.md
canonical_url: https://help.salesforce.com/s/articleView?id=ind.admin_referral_management_sharing_trigger.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 5fcee65a18b8e1b722c0fb1961df89c201e71c5dd9d195e7257a51752b709a9d
description: 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.
products:
  - Health
release: "264"
---
# 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.

### Required Editions

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition">
        <colgroup>
          <col>
        </colgroup>
        <tbody>
          <tr class="">
            <td class="slds-cell-wrap" style="vertical-align:top;">
              <p>Available in: Lightning Experience</p>
              <p style="margin-bottom:0;">Available in: <strong>Enterprise</strong> and <strong>Unlimited</strong> Editions with Health
                Cloud</p>
            </td>

          </tr>
        </tbody>
      </table>

![Important](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-ind-health-264-0-0-production-enus/90a9f828-79ab-49cd-ac8c-86e58ef7478e/images/icon_important.png)

Important 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.

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.
    
    ```java
    trigger ReferralShare on Lead (after update) {
        for(Lead lead : Trigger.new){
            try{
                Lead oldLead = Trigger.oldMap.get(lead.Id);
                // find the Group that the Lead creator is part of
                Group grp = [SELECT Id FROM Group WHERE Type='RoleAndSubordinates' 
                                AND RelatedId in (select UserRoleId from User where Id= :lead.CreatedById)];
                // Lead trigger sharing example - if the owner changed and the owner is different than the creator, 
                // share the Lead with the Group of the user that created the Lead
                if (lead.CreatedById!=lead.OwnerId && lead.OwnerId!=oldLead.OwnerId){
                    // create a new LeadShare record for the creator's Group
                    LeadShare leadShare = new LeadShare(LeadId=lead.Id, UserOrGroupId=grp.Id, LeadAccessLevel='Read', RowCause='Manual');
                    Database.SaveResult save = Database.insert(leadShare,false);
                    if (save.isSuccess()){
                        System.debug(LoggingLevel.ERROR, 'SAVE RESULT SUCCESS');
                    }
                    else {
                        for(Database.Error err : save.getErrors()){
                            System.debug(LoggingLevel.ERROR, 'SAVE RESULT ERROR: ' + err.getMessage());
                        }
                    }
                }
                // Opportunity sharing example - on Lead conversion, add an Opportunity sharing record for the Group of
                // the user that created the Lead
                if (lead.IsConverted){
                    // create a new OpportunityShare record for the creator's Group
                    OpportunityShare opptyShare = new OpportunityShare(OpportunityId=lead.ConvertedOpportunityId, UserOrGroupId=grp.Id, 
                                                                       OpportunityAccessLevel='Read', RowCause='Manual');
                    Database.SaveResult save = Database.insert(opptyShare,false);
                    if (save.isSuccess()){
                        System.debug(LoggingLevel.ERROR, 'SAVE RESULT SUCCESS');
                    }
                    else {
                        for(Database.Error err : save.getErrors()){
                            System.debug(LoggingLevel.ERROR, 'SAVE RESULT ERROR: ' + err.getMessage());
                        }
                    }
                }
            }
            catch(Exception ex){
                System.debug(LoggingLevel.ERROR, 'EXCEPTION: ' + ex.getMessage());
            }
        }  
    }
    ```