You are here:
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.
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.
- From Setup, in the Quick Find box, enter Change Data Capture, and then select Change Data Capture.
- In Available Entities, select Cadence Step Tracker (ActionCadenceStepTracker), and then click the > arrow.
- Save your changes.
Enable Change Data Capture for the ActionCadenceStepTrackerChangeEvent Object
Using the Developer Console, create a trigger on the ActionCadenceStepTracker change event.
- Click the gear icon, and then select Developer Console.
- In the Developer Console, select File | New | Apex Trigger.
- In the Name field, enter the name of your trigger. We use UpdateLeadOnStepCompletionTrigger.
- From the dropdown, select ActionCadenceStepTracker.
-
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.
- From Setup, in the Quick Find box, enter Debug Logs, and then select Debug Logs.
- Click New.
- For Traced Entity Type, select Automated Process.
- Select the time period for log collection. We suggest an hour or less.
- For Debug Level, click New Debug Level. Enter CustomDebugLevel for the name, and then accept the other default selections.
- Save your changes.
- In Sales Engagement, find your cadence that has the First Touch step. Set this step to Completed.
- Verify that the lead’s status is updated to Contacted.
- In Debug Logs, click View next to the automated process log to see the System.debug messages from your new trigger.

