Create an Apex final action
After these steps, users can run your Apex logic on the results of an audience.
Before you begin
- Edition
- Professional and Ultimate
- Permission
- Access to CAB Settings through the Campaign Audience Builder Admin permission set, and permission to deploy Apex. See Permission sets.
What a final action is and who can run it is in How final actions work. The interfaces, arguments and execution rules are in Apex final actions.
Write the class
Section titled “Write the class”-
Create a
globalclass with an accessible no-argument constructor that implementslb.FinalAction_ManagedAsync_Interface. -
In
runSync, read the records fromAUDIENCE_RESULTSand your own inputs by their API names:global with sharing class CreateFollowUpTasksimplements lb.FinalAction_ManagedAsync_Interface {global CreateFollowUpTasks() {}global void runSync(String actionApiName, Map<String, Object> args) {List<Contact> contacts = (List<Contact>) args.get(lb.FinalAction.Variable.AUDIENCE_RESULTS.name());String subject = (String) args.get('subject');List<Task> tasks = new List<Task>();for (Contact contact : contacts) {tasks.add(new Task(WhoId = contact.Id, OwnerId = contact.OwnerId, Subject = subject));}insert tasks;}} -
Act only on the records you’re given: for large results, CAB calls
runSynconce per batch. Make the logic safe to repeat if Salesforce retries a batch. -
Deploy the class with its tests.
Set up the final action
Section titled “Set up the final action”-
In CAB Settings, go to Configure → Automation → Configure Final Actions and select New.
-
Enter the Final Action Name. The API Name fills in by itself; your class receives it as
actionApiName. -
Set Metadata Type to Apex and search for your class under Apex Class. Only classes that implement a final action interface appear.
-
Optional: enter a Description. Under Show Advanced, set a Success Message, a Custom Permission Name or the Batch Size, the most records per
runSynccall. Select Next. -
Choose the object under Select Audience Object, here Contact. Under Set Available Fields, add every field the class reads, here
OwnerId. Select Next. -
Under Set Input Values, select New Variable for each input the class reads. Its Name is the key in
args, heresubjectwith the Type text. Optionally set a default. -
Select Done.
Every setting is described in Flow final actions; they’re the same for Apex.
Check the result
Section titled “Check the result”Open the results of a Contact audience. The final action appears in the menu next to Add to Campaign. Its window shows the description and the inputs; Run starts it.
To move an older class to the managed interface, see Apex final actions.

