You are here:
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
| 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 configure remote settings: | Customize Application OR Modify All Data |
| To add prospects to cadences: | Sales Engagement User OR Sales Engagement User Included |
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.
-
Create the Apex class.
- From the Setup menu, choose Developer Console.
- From the File menu, choose New | Apex Class.
- Enter EnrollTargetIntoSalesCadence for class name. Click OK.
-
In the Developer Console, replace the code you see with the following
Apex.
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; } } - Save the Apex class.
-
Create a process.
- From Setup, enter Process Builder in the Quick Find box. Then click Process Builder.
- Click New.
- Name the process.
- In the process starts when menu, choose A record changes.
- Click Save.
- In the Process Builder, click the Add Object node.
- From the Object menu in the right pane, choose Lead, Contact, or Person Account.
-
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.
- Click Save.
- In the Process Builder, click the +Add Criteria node.
-
In the right pane, name the criteria.
For example, call it something like Trigger for Cadence Enrollment.
- Under Criteria for Executing Actions, click No criteria—just execute the actions!.
- Click Save.
- In the Immediate Actions node to the right of the criteria node you just added, click +Add Action.
-
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.
- Click Save.
- Click Activate and then click Confirm.
Did this article solve your issue?
Let us know so we can improve!

