---
schema_version: "1"
id: 9a271b87ae40f413bf6249cead7ef0f6b19dbd0079f73ed5f6a1210162825416
title: Automatically Set a Lead’s Status When a Step Is Completed
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/sales/264-0-0/hvs_cadences_auto_set_lead_value.md
canonical_url: https://help.salesforce.com/s/articleView?id=sales.hvs_cadences_auto_set_lead_value.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: bc7e8faac756afab3a83d84d2c199242127d6d517120852236aa1f9d1718d0ae
description: 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.
products:
  - Sales Cloud
  - Cross Cloud
  - Service Cloud Voice
release: "264"
---
# 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

<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;">Available in: Lightning Experience</td></tr><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available with <a href="https://www.salesforce.com/sales/engagement-platform/" target="_blank">Sales Engagement</a>, which is included with Sales in <strong>Performance</strong>, <strong>Einstein 1</strong>, and <strong>Unlimited</strong> Editions, and available for an extra cost in <strong>Professional</strong> and <strong>Enterprise</strong> Editions. Sales Engagement is also available for an extra cost in Service and Lightning Platform.</td></tr></tbody></table>

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](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-sales-sales_productivity-264-0-0-production-enus/b2f6c36a-fa61-4a3c-9752-032ce1818a69/images/icon_note.png)

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.
    
    ```java
    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.

#### See Also

- [Test Change Event Triggers](https://developer.salesforce.com/docs/atlas.en-us.change_data_capture.meta/change_data_capture/cdc_trigger_testing.htm)