Skip to content

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.

  1. Create a global class with an accessible no-argument constructor that implements lb.FinalAction_ManagedAsync_Interface.

  2. In runSync, read the records from AUDIENCE_RESULTS and your own inputs by their API names:

    global with sharing class CreateFollowUpTasks
    implements 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;
    }
    }
  3. Act only on the records you’re given: for large results, CAB calls runSync once per batch. Make the logic safe to repeat if Salesforce retries a batch.

  4. Deploy the class with its tests.

  1. In CAB Settings, go to Configure → Automation → Configure Final Actions and select New.

  2. Enter the Final Action Name. The API Name fills in by itself; your class receives it as actionApiName.

  3. Set Metadata Type to Apex and search for your class under Apex Class. Only classes that implement a final action interface appear.

  4. Optional: enter a Description. Under Show Advanced, set a Success Message, a Custom Permission Name or the Batch Size, the most records per runSync call. Select Next.

  5. Choose the object under Select Audience Object, here Contact. Under Set Available Fields, add every field the class reads, here OwnerId. Select Next.

  6. Under Set Input Values, select New Variable for each input the class reads. Its Name is the key in args, here subject with the Type text. Optionally set a default.

  7. Select Done.

Every setting is described in Flow final actions; they’re the same for Apex.

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.