---
schema_version: "1"
id: 00eda4dd65d519854d5c954d7dbde2fb635a30f66c9efa72305b5ab76afb4787
title: Remove Prospects from Cadences 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_remove_targets_process_apex.md
canonical_url: https://help.salesforce.com/s/articleView?id=sales.hvs_cadences_auto_remove_targets_process_apex.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 0382856aac2cc4edba5f38e1a7e522b67d51e6ed6bdf85acfac775f0d62fd494
description: Save sales reps a step when they realize a prospect isn’t ready to buy. Create an Apex class and a process to automatically remove prospects when reps change their status to Unqualified.
products:
  - Sales Cloud
  - Cross Cloud
  - Service Cloud Voice
release: "264"
---
# Remove Prospects from Cadences Automatically with Process Builder and Apex

Save sales reps a step when they realize a prospect isn’t ready to buy. Create an Apex class and a process to automatically remove prospects when reps change their status to Unqualified.

### 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="d122914e80">User
              Permissions Needed</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d122914e80 ">To create, edit, or view processes:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d122914e80 ">
              <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="d122914e80 ">To define Apex triggers:</td>

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

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

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d122914e80 ">
              <p>Sales Engagement User or Sales Engagement User
                Included</p>
              <p>OR</p>
              <p style="margin-bottom:0;">Sales Engagement Cadence Creator or
                Sales Engagement Cadence Creator 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 `RemoveTargetFromSalesCadence` action. You can also build processes with the `AssignTargetToSalesCadence` action.

Set your process to start when records are created or edited.

1. Create the Apex class.
    1. From the Setup menu, choose **Developer Console**.
    2. From the File menu, choose **New** | **Apex Class**.
    3. Enter RemoveTargetFromSalesCadence for class name. Click **OK**.
    4. In the Developer Console, replace the code you see with the following Apex.
        
        ```java
        public class RemoveTargetFromSalesCadence {
        
           @InvocableMethod(label='Remove Target From Cadence' description='Uses REST Invocable Action to remove a Target to a Cadence')
           public static void removeTarget(List<RemoveTargetRequest> targetsToRemove) {
               System.debug('*** Targets to remove *** ' + targetsToRemove);
               if (targetsToRemove != null) {
                   for (RemoveTargetRequest req : targetsToRemove) {
                       System.debug('*** request *** ' + req);
                       System.debug('\ttarget: ' + req.targetId);
                       System.debug('\tcompletion reason code: ' + req.completionReasonCode);
                       sendRequest(req.targetId, req.completionReasonCode);
                   }
               } else {
                   System.debug('*** null targetsToRemove received ***');
               }
           }
        
           @future(callout = true)
           private static void sendRequest(String targetId, String completionReasonCode) {
               String sfdcURL = URL.getSalesforceBaseUrl().toExternalForm();
               String actionsRestURL = sfdcURL + '/services/data/v45.0/actions';
               String removeTargetRESTUrl = actionsRestURL + '/standard/removeTargetFromSalesCadence';
        
               HttpRequest httpReq = new HttpRequest();
               httpReq.setMethod('POST');
               httpReq.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
               httpReq.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
               httpReq.setEndpoint(removeTargetRESTUrl);
               httpReq.setHeader('Content-Type', 'application/json');
               httpReq.setBody('{"inputs" : [{"completionReasonCode" : "' + completionReasonCode + '", "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 RemoveTargetRequest {
               @InvocableVariable(required=true)
               public Id targetId;
        
               @InvocableVariable(required=true)
               public String completionReasonCode;
           }
        }
        ```
    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 **when a record is created or edited**.
    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 Removal.
    12. Under Criteria for Executing Actions, click **Formula evaluates to true**.
    13. Insert the following formula into the Build Formula section.
        
        ```java
        NOT(ISNEW()) && ISPICKVAL([Lead].Status,"Unqualified")
        ```
        
        The `NOT(ISNEW())` statement is used to evaluate records that were not newly added into the org. This code block also assumes that you want to use the Lead Status value of Unqualified as the criteria for removing a lead from cadences. Replace this value (or this field) to suit your own business processes.
    14. Click **Save**.
    15. In the Immediate Actions node to the right of the criteria node you just added, click **+Add Action**.
    16. 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.
        - CompletionReasonCode: For Value, enter ManuallyRemoved.
        - 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 remove lead, contact, or person account records from the cadence.
    17. Click **Save**.
    18. Click **Activate** and then click **Confirm**.