Sub topic 4
Actions: how an agent does work
This chapter assumes you know basic Salesforce objects, records, and permission sets. It also assumes you can read simple Flow or Apex. You should have a general idea of what topics and instructions are from the previous chapter.
What an action is
An action is a unit of work that an agent can run. A topic decides what the agent handles. An action is what the agent does when it needs to read data, change a record, or call another system. Without actions, an agent can only talk. With them, it can look up an order, update a case, or start a process.
Each action has a name, a description, inputs, and outputs. The reasoning engine reads the name and description to decide whether the action fits the customer's request. It then fills in the inputs from the conversation and uses the outputs in its reply.
Common action types
Actions can be backed by several kinds of implementation.
| Backing | What it does | Typical use |
|---|---|---|
| Flow | Runs an autolaunched flow | Create a case, update a record |
| Apex | Calls an invocable method | Custom logic, complex queries |
| Prompt template | Generates text from a template | Summarize a record, draft a reply |
| Standard action | Prebuilt by Salesforce | Answer questions from knowledge, look up a record |
Standard actions are the fastest way to start. Build your own when your business logic is not covered.
How actions connect to topics
An action belongs to a topic. The agent can only call actions that are assigned to the topic it has chosen. This keeps each topic narrow. The topic's instructions then tell the agent when to call each action and what to do with the result.
An Apex action
The class below is an invocable method. Its annotated variables become the action's inputs and outputs. It uses with sharing, so the query respects the record access of the user running the action. It also takes a list of requests and runs one query, so it is safe for bulk calls.
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;
}
}The label and description matter. The reasoning engine uses them to pick the action, so write them as plain statements of what the action does.
Common errors
- Vague descriptions. "Handles orders" does not tell the agent when to use the action. Say what it returns and for what input.
- Missing required inputs. Mark inputs
required=truewhen the action cannot run without them. Then add an instruction to ask the customer for the value. - Overly broad actions. One action that reads, edits, and deletes is hard to control. Prefer small actions with one job.
- Ignoring access. An action runs with the permissions of the identity behind the agent. A
without sharingclass can return records that user should not see.
Exercise
The Get Order Status action returns "Order not found" for an unknown number. Where do you tell the agent how to respond to that result, and why not inside the Apex class?
Show answer
Put it in the topic's instructions, for example "ask the customer to check the number once, then offer a human agent." The Apex class only returns data. The topic instructions control how the agent behaves with that data, and they can be changed without deploying code.