Automatically Set a Lead’s Status When a Step Is Completed

Use an Apex trigger to subscribe to Change Data Capture events from cadences. Update a lead’s status to Contacted when a sales rep completes the First Touch step in the lead’s cadence.

Required Editions

Available in: Lightning Experience
Available with Sales Engagement, which is included with Sales in Performance, Einstein 1, and Unlimited Editions, and available for an extra cost in Professional and Enterprise Editions. Sales Engagement is also available for an extra cost in Service and Lightning Platform.

Start by enabling change data capture for the ActionCadenceStepTrackerChangeEvent object. Then, create a change event trigger. Finally, change a step's state and see how a lead's status updates automatically. To use this example, you need a cadence with a step called First Touch. The cadence must have at least one lead added as a target.

Note
Note Remember that Apex triggers for change events are asynchronous. After a sales rep marks a step as completed, a short delay can occur before the related lead’s status is updated.

Enable Change Data Capture for the ActionCadenceStepTrackerChangeEvent Object

Before you can receive change notifications for cadence steps, you must enable the ActionCadenceStepTrackerChangeEvent object for notifications.

  1. From Setup, in the Quick Find box, enter Change Data Capture, and then select Change Data Capture.
  2. In Available Entities, select Cadence Step Tracker (ActionCadenceStepTracker), and then click the > arrow.
  3. Save your changes.

Enable Change Data Capture for the ActionCadenceStepTrackerChangeEvent Object

Using the Developer Console, create a trigger on the ActionCadenceStepTracker change event.

  1. Click the gear icon, and then select Developer Console.
  2. In the Developer Console, select File | New | Apex Trigger.
  3. In the Name field, enter the name of your trigger. We use UpdateLeadOnStepCompletionTrigger.
  4. From the dropdown, select ActionCadenceStepTracker.
  5. Replace the default content with this code.
    trigger UpdateLeadOnStepCompletionTrigger on ActionCadenceStepTrackerChangeEvent (after insert)
        for(ActionCadenceStepTrackerChangeEvent event : Trigger.New) {
            EventBus.ChangeEventHeader header = event.ChangeEventHeader;
            List<String> recordIds = header.getRecordIds();
            
            System.debug('Received change event for ' + 
                         header.entityName +
                         ' for the ' + 
                         header.changeType + 
                         ' operation.'
                        );
            
            if(recordIds.size() == 0) {
                continue;
            }
            
            // Get ActionCadenceStepTracker records for completed steps where the target is a lead
            List<ActionCadenceStepTracker> stepTrackers = [SELECT Id, ActionCadenceStepId, ActionCadenceName, TargetId, StepType, StepTitle
                                                           FROM ActionCadenceStepTracker
                                                           WHERE     Id IN :recordIds 
                                                                 AND State = 'Completed'
                                                                 AND Target.Type = 'Lead'
                                                                 AND StepTitle LIKE 'First Touch%'];
    
            if(stepTrackers != null && stepTrackers.size() > 0) {
                System.debug('Found ' + stepTrackers.size() + ' step tracker events.');
                
                List<Lead> leads = new List<Lead>();
                for(ActionCadenceStepTracker stepTracker : stepTrackers) {
                    System.debug('Adding the lead ID  ' + stepTracker.TargetId + ' to Lead list.');
                    Lead lead = new Lead(Id=stepTracker.TargetId, Status='Contacted');
                    leads.add(lead);
                }
                            
                update leads;
            } else {
                System.debug('Did not find any completed First Touch step trackers that are related to leads');
            }
        }
    }

This change event trigger iterates through each received change event message in Trigger.New. For each event, we get the ID of the ActionCadenceStepTracker that triggered the event. Then, we use the ID of the step tracker to query for the ActionCadenceStepTracker record. We check to verify that the related step’s name is First Touch, the state is Completed, and the target of the cadence is a lead.

If the cadence step meets that criteria, the state of the related lead is updated to Contacted.

Verify the Trigger

Set the log level to capture debug messages from Apex triggers, then verify that your new trigger updates a lead’s status.

  1. From Setup, in the Quick Find box, enter Debug Logs, and then select Debug Logs.
  2. Click New.
  3. For Traced Entity Type, select Automated Process.
  4. Select the time period for log collection. We suggest an hour or less.
  5. For Debug Level, click New Debug Level. Enter CustomDebugLevel for the name, and then accept the other default selections.
  6. Save your changes.
  7. In Sales Engagement, find your cadence that has the First Touch step. Set this step to Completed.
  8. Verify that the lead’s status is updated to Contacted.
  9. In Debug Logs, click View next to the automated process log to see the System.debug messages from your new trigger.