Loading
Feature degradation | Gmail Email delivery failureRead More
Sales Productivity
Table of Contents
Select Filters

          No results
          No results
          Here are some search tips

          Check the spelling of your keywords.
          Use more general search terms.
          Select fewer filters to broaden your search.

          Search all of Salesforce Help
          Remove Prospects from Cadences Automatically with Process Builder and Apex

          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

          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.
          User Permissions Needed
          To create, edit, or view processes:

          Manage Flow

          AND

          View All Data

          To define Apex triggers: Author Apex
          To remove prospects from cadences:

          Sales Engagement User or Sales Engagement User Included

          OR

          Sales Engagement Cadence Creator or Sales Engagement Cadence Creator Included

          Note
          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.
              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.
              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.
           
          Loading
          Salesforce Help | Article