Sub topic 6
Actions: What an Agent Can Actually Do
Prerequisites
This chapter assumes you know basic Salesforce objects and records, and you have seen Flow or Apex at a high level. It also assumes you know that a large language model (LLM) generates text from a prompt. Earlier chapters introduced topics and instructions. This chapter covers the part of an agent that touches your data and systems.
Why actions matter
An LLM on its own can only produce text. It cannot read an order record, update a case, or send an email. An action is the unit of work that lets an agent do those things. The agent decides when to run an action, and Salesforce runs it and returns the result.
This split is the main idea of the chapter. The reasoning engine chooses and fills in the action. The action's own logic, such as a Flow, Apex method, or prompt template, does the work. The LLM never runs your code or queries your database directly.
What an action is
An action is a named capability with a defined contract:
- A label and a description that tell the agent what the action does.
- A set of inputs, which are the values the action needs.
- A set of outputs, which are the values it returns.
- A target, which is the Salesforce logic that runs when the action is invoked.
The target can be a Flow, an Apex invocable method, a prompt template, or another supported type. Chapter 7 compares those types. Here we treat them all as one thing: a callable unit with inputs and outputs.
| Part | Who reads or uses it | Purpose |
|---|---|---|
| Label and description | The LLM | Helps it judge whether the action fits the request |
| Input names and descriptions | The LLM | Tells it what values to collect or pass in |
| Output names and descriptions | The LLM | Tells it what the returned values mean |
| Target logic (Flow, Apex, and so on) | Salesforce runtime | Does the real work |
The first three rows are text the model reads. The fourth is code or configuration the model never sees. If the descriptions are vague, the agent has to guess, even when the target logic is perfect.
Actions belong to topics
An action is attached to a topic, and the agent considers only the actions of the topic it is currently in. That is the guardrail built into the design. If "Get Order Status" is attached only to the Order Support topic, a conversation classified into a different topic cannot call it.
The topic example below shows where actions sit alongside the classification description, scope, and instructions.
Topic: Order Support
Classification description: Handles customer questions about the status of an existing order, such as where it is or when it will arrive.
Scope: Look up order status and explain it. Does not process returns, refunds, or changes to an order.
Instructions:
- Ask for the order number if the customer has not given one.
- Use the order status action to look up the order before answering.
- If the order cannot be found, say so and do not guess.
Actions: Get Order StatusNotice how the instructions refer to the action in plain language ("Use the order status action"). The instructions steer when to use it. The action's own description says what it does. Keep these two jobs separate so you do not repeat yourself in both places.
The action lifecycle
The following diagram shows what happens when the agent decides to use an action.
Three points follow from the diagram:
- Input values come from the conversation. If a required input is missing, the agent should ask the user for it before invoking the action.
- The action returns structured outputs, not a finished sentence. The reasoning engine turns them into the reply.
- The reply is only as accurate as the outputs. If the action returns "Not Found", the agent should report that, not invent a status.
Building an action in Apex
An Apex method becomes an agent action when it is an invocable method. The annotations carry the text that the agent reads.
public with sharing class OrderStatusAction {
public class Request {
@InvocableVariable(label='Order Number' description='The order number the customer provided' required=true)
public String orderNumber;
}
public class Response {
@InvocableVariable(label='Status' description='Current status of the order, or Not Found')
public String status;
}
@InvocableMethod(label='Get Order Status' description='Looks up the current status of an order by its 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) : 'Not Found';
results.add(res);
}
return results;
}
}Here is how each part maps to the action contract:
- The
labelanddescriptionon@InvocableMethodbecome the action's label and description. - The
Requestclass holds the inputs. Itsdescriptiontells the model what to pass in, andrequired=truemarks the value as mandatory. - The
Responseclass holds the outputs. The description onstatusexplains that the value may beNot Found, so the model knows what that string means. - The method takes and returns a list because invocable methods are bulk-safe by design. The code collects all order numbers first and runs one query, which keeps it within governor limits.
After you deploy the class, you add it to the topic from Agent Builder by choosing the Apex action type and selecting the method. Check the label and description shown there, because they come from your annotations.
What to design into every action
A good action is narrow, predictable, and honest about failure. Apply these rules when you build one:
- Do one job. "Get Order Status" is easier to select correctly than "Handle Order Things". Narrow actions give the model a clear match or a clear mismatch.
- Return a explicit result for the empty case. The example returns
Not Foundinstead of null or an exception, and the topic instruction tells the agent not to guess in that case. - Name inputs in the user's terms. An input called
orderNumberwith a plain description is easier to fill thanordNumwith none. - Keep side effects visible. An action that changes data should say so in its description. Read-only actions and write actions deserve different care, because a wrongly chosen write is harder to undo.
- Handle bulk calls. Even if the agent usually sends one request, the invocable signature accepts a list, so write for many.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The agent never calls the action | The action is not attached to the active topic, or its description does not match the request | Attach it to the right topic and rewrite the description around what the user asks for |
| The agent asks for the wrong value | Input description is vague | Describe the input in the user's words and give an example format |
| The agent answers without calling the action | The topic instructions do not say to look the data up first | Add an instruction such as "Use the order status action before answering" |
| The agent invents a status when nothing is found | The action returns null or throws, and no instruction covers that case | Return an explicit value like `Not Found` and tell the agent to say so |
| The action runs but returns less data than expected | Sharing rules or field access for the running user | Check the user context and permissions, not the model |
| The method is missing from the action picker | Not annotated with `@InvocableMethod`, not `public` or `global`, or not deployed | Check the annotation, visibility, and deployment |
The second and third rows connect to chapters 9 and 10, which cover how the agent chooses among actions and how to write descriptions that steer that choice. The testing chapter later shows how to see which action the agent picked and which inputs it sent.
Summary of the model
An action is a described contract (label, description, inputs, outputs) wrapped around real logic. The agent reads the contract, picks an action from the active topic, fills the inputs from the conversation, and lets Salesforce run the target. The outputs come back as data, and the agent writes the reply from them. Most problems trace back to a mismatch between the contract the model reads and the behavior the logic delivers.
Exercise
An Apex invocable method returns null for the status when no order matches. The agent sometimes tells customers "Your order is being processed" for order numbers that do not exist. Name two changes that reduce this, and say where each change goes.
Show answer
First, change the action so it returns an explicit value such as "Not Found" when no order matches, and mention that value in the output variable's description (in the Apex code). Second, add an instruction to the topic, such as "If the order cannot be found, say so and do not guess" (in the topic's instructions). The first change gives the model an unambiguous result to report. The second tells it what to do with that result.
Exercise
A topic has the action "Get Order Status" attached. A user in a different topic asks about an order, and the agent never calls the action. Give the most likely reason.
Show answer
Actions are attached to topics, and the agent considers only the actions of the current topic. The conversation was classified into a different topic that does not include "Get Order Status", so the action was not available to choose.