Sub topic 13
Building custom actions with Flow and Apex
Standard actions cover common tasks. When an agent needs your own logic, you build a custom action. This chapter assumes you know what topics and actions are, how an agent user differs from a running user, and that you can read basic Flow or Apex.
Choosing Flow or Apex
An agent action can point to an autolaunched Flow, an Apex invocable method, or a prompt template. Flow and Apex are the two options for logic you write yourself.
| Need | Better fit |
|---|---|
| Record lookups, updates, simple branching | Flow |
| Admins must maintain it | Flow |
| Complex logic, callouts, heavy data shaping | Apex |
| Reuse by developers across features | Apex |
Both are called the same way by the agent. The reasoning engine reads the action's name, description, and input and output definitions, then decides when to call it.
Building an Apex action
An invocable method becomes an action when you annotate the method and its input and output variables. The example below looks up an order status.
public with sharing class GetOrderStatusAction {
public class Request {
@InvocableVariable(label='Order Number' required=true)
public String orderNumber;
}
public class Response {
@InvocableVariable(label='Order Status')
public String status;
}
@InvocableMethod(
label='Get Order Status'
description='Returns the status of an order for a given order number'
)
public static List<Response> getStatus(List<Request> requests) {
Set<String> orderNumbers = new Set<String>();
for (Request r : requests) {
orderNumbers.add(r.orderNumber);
}
Map<String, String> statusByNumber = new Map<String, String>();
for (Order o : [SELECT OrderNumber, Status FROM Order WHERE OrderNumber IN :orderNumbers]) {
statusByNumber.put(o.OrderNumber, o.Status);
}
List<Response> results = new List<Response>();
for (Request r : requests) {
Response res = new Response();
res.status = statusByNumber.containsKey(r.orderNumber)
? statusByNumber.get(r.orderNumber)
: 'Order not found';
results.add(res);
}
return results;
}
}Three details matter here:
with sharingmakes the query respect record access of the user the agent runs as.- The method takes and returns a list, so it handles bulk calls with one query.
- The
descriptionandlabelvalues are what the reasoning engine reads. Write them as plain statements of what the action does.
Building a Flow action
For a Flow, create an autolaunched Flow. Mark the variables the agent must supply as Available for input, and the values it needs back as Available for output. Give each variable a clear name and description. The agent uses these to decide what to ask the customer or employee.
Adding the action to a topic
In Agent Builder, open the topic, add an action, and choose the Flow or Apex method as the reference action type. Then write topic instructions that name the action:
Topic: Order Status
Classification description: Customer asks where an order is or what state it is in.
Scope: Look up the status of a single order using the Get Order Status action.
Instructions:
- Ask for the order number if the customer has not given one.
- Call the Get Order Status action with that order number.
- Report only the status returned by the action.
- If the action returns 'Order not found', ask the customer to check the number once, then offer to connect them with a human agent.
- Do not discuss refunds or cancellations in this topic.Common errors
- Vague descriptions. The agent skips the action or calls it at the wrong time. State what the action does and what it returns.
- Missing access. The agent user needs permission to the Apex class or Flow and to the objects it reads. A service agent runs as the agent user. An employee agent runs with the employee's own permissions.
- Too many inputs. Each extra required input means another question to the user. Ask for only what you need.
- Unhandled empty results. Return a clear value such as
Order not found, and tell the instructions how to react.
Exercise
An agent never calls your new Apex action, although the method works when you test it directly. Name two things to check.
Show answer
Check that the invocable method's label and description clearly say what it does, and that the topic instructions refer to the action. Also confirm the agent user has access to the Apex class and the objects it queries.