---
schema_version: "1"
id: 51dedd9b40033b47418134c80bef5b7df0f1e520d3e9478bcf71b74fb6de6af6
title: Add Prospects to a Cadence Automatically with Process Builder and Apex
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/sales/264-0-0/hvs_cadences_auto_add_targets.md
canonical_url: https://help.salesforce.com/s/articleView?id=sales.hvs_cadences_auto_add_targets.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: c020b08c3360515e07fdd004569822464bcb6a81bfed92ef6b5b6151fac4a8f5
description: Save your sales reps from taking an extra step. To automatically add lead, contact, or person account records to a cadence when the records are created, create an Apex class and a process.
products:
  - Sales Cloud
  - Cross Cloud
  - Service Cloud Voice
release: "264"
---
# Add Prospects to a Cadence Automatically with Process Builder and Apex

Save your sales reps from taking an extra step. To automatically add lead, contact, or person account records to a cadence when the records are created, create an Apex class and a process.

### 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>

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small permission">
        <colgroup>
          <col style="width:50%">
          <col style="width:50%">
        </colgroup>
        <thead class="slds-text-title_caps ">
          <tr class="">
            <th scope="colgroup" class="slds-cell-wrap" colspan="2" style="text-align:center;vertical-align:top;" id="d102213e80">User
              Permissions Needed</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d102213e80 ">To create, edit, or view processes:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d102213e80 ">
              <p>Manage Flow</p>
              <p>AND</p>
              <p style="margin-bottom:0;">View All Data</p>
            </td>

          </tr>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d102213e80 ">To define Apex triggers:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d102213e80 ">Author Apex</td>

          </tr>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d102213e80 ">To configure remote settings:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d102213e80 ">
              <p>Customize Application</p>
              <p>OR</p>
              <p style="margin-bottom:0;">Modify All Data</p>
            </td>

          </tr>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d102213e80 ">To add prospects to cadences:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d102213e80 ">
              <p>Sales Engagement User</p>
              <p>OR</p>
              <p style="margin-bottom:0;">Sales Engagement User
                Included</p>
            </td>

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

![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 You can’t add Apex classes directly in production Salesforce orgs. Only Developer, trial, and sandbox orgs support direct Apex editing in Setup. To deploy Apex code in a production org, add it first to a sandbox org, test, and then promote it to the production org.

This example uses the `AssignTargetToSalesCadence` action. You can also build processes with the `RemoveTargetFromSalesCadence` action.

Set your process to start only upon record creation. Setting your process to run upon both record creation and edit creates duplicate targets within the cadence.

1. Create the Apex class.
    1. From the Setup menu, choose **Developer Console**.
    2. From the File menu, choose **New** | **Apex Class**.
    3. Enter EnrollTargetIntoSalesCadence for class name. Click **OK**.
    4. In the Developer Console, replace the code you see with the following Apex.
        
        ```java
        public class EnrollTargetIntoSalesCadence {
            
            @InvocableMethod(label='Enroll Target In Cadence' description='Uses REST Invocable Action to assign a Target to a Cadence')
            public static void enrollTarget(List<EnrollTargetRequest> targetsToEnroll) {
                System.debug('*** Targets to enroll *** ' + targetsToEnroll);
                if (targetsToEnroll != null) {
                    for (EnrollTargetRequest req : targetsToEnroll) {
                        System.debug('*** request *** ' + req);
                        System.debug('\ttarget: ' + req.targetId);
                        System.debug('\tsales cadence: ' + req.salesCadenceName);
                        sendRequest(req.targetId, req.salesCadenceName);
                    }
                } else {
                    System.debug('*** null targetsToEnroll receieved ***');
                }  
            }
            
            @future(callout = true)
            private static void sendRequest(String targetId, String salesCadenceNameOrId) {
                String sfdcURL = URL.getSalesforceBaseUrl().toExternalForm();
                String actionsRestURL = sfdcURL + '/services/data/v45.0/actions';
                String assignTargetRESTUrl = actionsRestURL + '/standard/assignTargetToSalesCadence';
        
                HttpRequest httpReq = new HttpRequest();
                httpReq.setMethod('POST');
                httpReq.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
                httpReq.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
                httpReq.setEndpoint(assignTargetRESTUrl);
                httpReq.setHeader('Content-Type', 'application/json');
                httpReq.setBody('{"inputs" : [{"salesCadenceNameOrId" : "' + salesCadenceNameOrId + '", "targetId" : "' + targetId + '"}]}');
                
                String response = '';
                try {
                    Http http = new Http();
                    HttpResponse httpResponse = http.send(httpReq);
                    if (httpResponse.getStatusCode() == 200) {
                        response = JSON.serializePretty(JSON.deserializeUntyped(httpResponse.getBody()));
                    } else {
                        System.debug('http response: ' + httpResponse.getBody());
                        throw new CalloutException(httpResponse.getBody());
                    }
                } catch (System.Exception e) {
                    System.debug('ERROR! ' + e);
                    throw e;
                }
                
                System.debug('response: ' + response);     
            }
            
            public class EnrollTargetRequest {
                @InvocableVariable(required=true)
                public Id targetId;
                
                @InvocableVariable(required=true)
                public String salesCadenceName;
            }
        }
        ```
    5. Save the Apex class.
2. Create a process.
    1. From Setup, enter Process Builder in the Quick Find box. Then click **Process Builder**.
    2. Click **New**.
    3. Name the process.
    4. In the process starts when menu, choose **A record changes**.
    5. Click **Save**.
    6. In the Process Builder, click the **Add Object** node.
    7. From the Object menu in the right pane, choose **Lead**, **Contact**, or **Person Account**.
    8. Under Start the process, choose **only when a record is created**.
        
        Don’t choose **when a record is created or edited**. This setting causes duplicate targets to appear in the cadence.
    9. Click **Save**.
    10. In the Process Builder, click the **+Add Criteria** node.
    11. In the right pane, name the criteria.
        
        For example, call it something like Trigger for Cadence Enrollment.
    12. Under Criteria for Executing Actions, click **No criteria—just execute the actions!**.
    13. Click **Save**.
    14. In the Immediate Actions node to the right of the criteria node you just added, click **+Add Action**.
    15. In the right pane, choose these settings.
        
        - Action Type: **Apex**
        - Action name: Enter a name for the action.
        - Apex Class: Choose the Apex class you created previously.
        - SalesCadenceName: For Value, enter the name of your cadence.
        - TargetId: For Type, choose **Field Reference**. For Value, click the field and search for Lead.Id, Contact.Id, or Account.Id depending on whether you want to add lead, contact, or person account records to the cadence.
    16. Click **Save**.
    17. Click **Activate** and then click **Confirm**.

#### See Also

- [Create a Process](https://help.salesforce.com/s/articleView?id=platform.process_create.htm&type=5&language=en_US)
- [Define Apex Triggers](https://help.salesforce.com/s/articleView?id=platform.code_define_trigger.htm&type=5&language=en_US)