Skip to content

Update lead Owner, when a lead is assigned to a Sales Engagement cadence (cadence starts).

Last updated on April 5, 2024

When working with Sales Engagement cadences, you may encounter a scenario where you need to update a field on a lead when it is assigned to a cadence (i.e., when the cadence starts). However, you might discover that the ActionCadenceTracker object, which is created when a target is added to a cadence, does not support Salesforce automation such as record-triggered flows and Apex triggers.

This article provides step-by-step instructions to help you set up the necessary automation using Change Data Capture as mentioned in this Salesforce article: Cadence Automation Tools.

STEP 1: Enable Change Data Capture for “ActionCadenceTracker” object

Go to Salesforce Setup > Integrations > Change Data Capture. Now move the Cadence Tracker object from the available entities to the selected entities and click the Save button. As shown in the attached screenshot.

STEP 2: Create an auto-launched Flow with a “Change Cadence Target Assignee” action

Create an auto-launched flow with the following label and API name.

  • Flow Label: Change Cadence Target Assignee
  • Flow API Name: Change_Cadence_Target_Assignee
  • Type: Autolaunched Flow

Now create two text variables user_id and target_id and mark them as Available for input, as shown in the attached screenshots. Then put the “Change Cadence Target Assignee” action in the flow from the available Sales Engagement Invocable Actions and map the variables as shown in the third screenshot.

STEP 3: Create an apex trigger on the ActionCadenceTrackerChangeEvent object

Create an apex trigger on the ActionCadenceTrackerChangeEvent object to run on the after insert operation. This trigger listens for new events on the ActionCadenceTrackerChangeEvent record, and when a Lead is targeted by a specific cadence (‘my test cadence’), it triggers a Flow (created in step 2) to update the Lead owner with the cadence assignee.

trigger UpdateCadenceAssigneeOnLead on ActionCadenceTrackerChangeEvent (after insert) {
    
    set<Id> CadenceIds = new set<Id>();
    set<Id> LeadIds = new set<Id>();
    
    for(ActionCadenceTrackerChangeEvent event : Trigger.New) {
        EventBus.ChangeEventHeader header = event.ChangeEventHeader;
        
        System.debug('Received change event for ' + 
                     header.entityName +
                     ' for the ' + 
                     header.changeType + 
                     ' operation.'
                    );
        if(header.changeType.equalsIgnoreCase('create') && String.ValueOf(event.TargetId)?.StartsWith('00Q')){
            system.debug('event::'+event);
            
            system.debug('ActionCadenceId::'+event.ActionCadenceId);
            system.debug('TargetId::'+event.TargetId);
            
            CadenceIds.add(event.ActionCadenceId);
            LeadIds.add(event.TargetId);
            
        }
    }
    
    Map<Id, ActionCadence> ActionCadenceMap = new Map<Id, ActionCadence>([SELECT Id, Name, Type, State FROM ActionCadence WHERE ID IN :CadenceIds]);
    Map<Id, Lead> LeadsMap = new Map<Id, Lead>([SELECT Id, OwnerId, Owner.Username FROM Lead WHERE ID IN :LeadIds]);
    
    for(ActionCadenceTrackerChangeEvent event : Trigger.New) {
        EventBus.ChangeEventHeader header = event.ChangeEventHeader;
        
        System.debug('Received change event for ' + 
                     header.entityName +
                     ' for the ' + 
                     header.changeType + 
                     ' operation.'
                    );
        if(header.changeType.equalsIgnoreCase('create') && String.ValueOf(event.TargetId)?.StartsWith('00Q')){
            if(ActionCadenceMap.get(event.ActionCadenceId) != NULL && ActionCadenceMap.get(event.ActionCadenceId).Name.equalsIgnoreCase('my test cadence')){
                if(LeadsMap.get(event.TargetId) != NULL && LeadsMap.get(event.TargetId)?.OwnerId != NULL &&  String.valueOf(LeadsMap.get(event.TargetId)?.OwnerId)?.StartsWith('005') ){
                    //Instance of the Flow
                    
                    Map<String, Object> myMap = new Map<String, Object>();
                    myMap.put('target_id', event.TargetId);
                    myMap.put('user_id', LeadsMap.get(event.TargetId)?.OwnerId);
                    Flow.Interview.Change_Cadence_Target_Assignee myFlow = new Flow.Interview.Change_Cadence_Target_Assignee(myMap);
                    myFlow.start();                    
                }
            }
        }
    }    
}

💪