Sub topic 8
Topics and the kinds of actions an agent can call
This chapter assumes you can already create an agent from a spec with sf agent create and preview it with sf agent preview. You should also know what an invocable Apex method is and be able to read a Salesforce DX project. It explains how an agent decides what to do and which kinds of actions it can call. Later chapters build on this when you write tests and custom actions.
How topics and actions fit together
An agent does not run a fixed script. For each user message, the reasoning engine (the planner) makes two decisions in order:
- It picks a topic, a category of work the agent handles, such as case status or order returns.
- Inside that topic, it picks which actions to call, if any, and in what order.
A topic bundles three things:
- A name and a classification description. The planner reads the description to decide whether a message belongs to the topic.
- A scope, which states what the topic does and does not cover.
- Instructions, written in natural language, that guide behavior inside the topic.
The topic also owns a list of actions. An action is a unit of work with defined inputs and outputs. The agent only sees the actions attached to the current topic.
Because the planner chooses from natural-language text, the quality of your descriptions largely determines how reliably the agent behaves. Vague descriptions cause wrong topic choices, and topics that overlap cause unstable choices.
Writing topics that classify well
Each topic needs a description that separates it clearly from the others. Compare these two:
| Weak | Better |
|---|---|
| Handles cases | Answers questions about the status of an existing support case, given a case number |
| Help with orders | Looks up order shipping status and delivery dates for orders already placed |
Follow these rules:
- Describe one job per topic. If you write "and" often, consider two topics.
- State what the topic is for, not only the objects it touches. Two topics that both mention "cases" will compete.
- Put exclusions in the scope, for example "Does not create or modify cases."
- Keep the number of topics modest. Each additional topic gives the planner another option to confuse with the others. The
--max-topicsflag onsf agent generate agent-speclimits how many topics the generator proposes.
The spec file from sf agent generate agent-spec lists topics with a name and a description. A trimmed example looks like this:
agentType: customer
role: Answer questions about the status of customer support cases
companyName: Acme Support
companyDescription: Acme provides hardware and support plans
topics:
- name: Case Status
description: Answers questions about the status of an existing support case, given a case number
- name: General FAQ
description: Answers general questions about Acme hardware and support plans using approved knowledge contentField names can differ between plugin versions. Generate a spec with your installed plugin and edit that file instead of copying this one.
The kinds of actions
Actions differ in what runs behind them. The agent treats them all the same way: a name, a description, inputs, and outputs.
| Action type | What runs | Typical use |
|---|---|---|
| Standard action | Salesforce-provided capability | Query records, summarize a record, draft an email, answer from knowledge |
| Apex action | An invocable Apex method | Custom logic or queries that need code |
| Flow action | An autolaunched flow | Business processes you already model in Flow |
| Prompt template action | A prompt template | Generating or summarizing text with grounded data |
| External service action | An API described by an OpenAPI schema, or a MuleSoft API | Calling systems outside Salesforce |
Choose by where the logic already lives and who will maintain it:
- If a flow already does the job, expose that flow. You avoid duplicating logic and admins can maintain it.
- If you need loops, complex queries, or unit-tested logic, write Apex.
- If the output is mostly generated text, use a prompt template.
- If the data lives outside Salesforce and has an API description, use an external service.
- Prefer a standard action over a custom one when it covers the need. It costs nothing to maintain.
What the agent reads from an action
The planner cannot see your code. It sees only the metadata around it, so these fields matter:
- Action name and description: these tell the planner when to call it.
- Input names and descriptions: these tell the planner what value to supply and where to find it in the conversation.
- Output names and descriptions: these tell the planner how to use the result in its reply.
- Action instructions: guidance on when and how to call the action.
For an invocable Apex method, the label and description on @InvocableMethod and the label on each @InvocableVariable become this metadata. The example below carries the information the planner needs. The description says what the method returns, and each field has a plain-language label.
public with sharing class CaseStatusAction {
public class Request {
@InvocableVariable(label='Case Number' required=true)
public String caseNumber;
}
public class Result {
@InvocableVariable(label='Case Status')
public String status;
}
@InvocableMethod(label='Get Case Status' description='Returns the status of a case from its case number')
public static List<Result> getStatus(List<Request> requests) {
List<Result> results = new List<Result>();
for (Request req : requests) {
Result res = new Result();
List<Case> found = [
SELECT Status FROM Case WHERE CaseNumber = :req.caseNumber LIMIT 1
];
res.status = found.isEmpty() ? 'Not found' : found[0].Status;
results.add(res);
}
return results;
}
}Two design points apply to any action:
- Return a clear value for the empty case. This method returns
'Not found'. Without that, the agent receives nothing and may invent an answer. - Respect sharing. The class uses
with sharing. An agent can call the action on behalf of a user, so record access rules still need to hold.
The chapter on custom actions covers wiring this class to a topic. Here, the point is that the topic's action list is what makes it callable.
How topics and actions appear as metadata
In your DX project, an agent is stored as several metadata types, not one file. The exact set depends on your API version and whether you author with a spec or with Agent Script. Commonly you will see the following:
- A bot and bot version, which represent the agent itself.
- A planner bundle, which holds the agent's topics and actions together.
- Topic (plugin) and action (function) definitions that the planner references.
Check what your org produces by retrieving the agent and looking at the result:
sf project retrieve start --metadata Agent:Case_Helper --target-org my-dev-orgTreat the retrieved files as the source of truth for what the agent has. If a topic or action is missing there, the planner cannot use it, whatever your spec says. The metadata chapter covers retrieval and versioning in detail.
Checking topic and action choice
Preview is the quickest way to see the planner's choices. Run the preview and send messages that should hit different topics:
sf agent preview --api-name Case_Helper --target-org my-dev-orgTry three kinds of input: a clear match, a message that sits between two topics, and a message that matches none. Then turn the cases that matter into test cases. A test case names the expected topic and actions, so a topic or action rename breaks the test:
name: Case Helper Tests
description: Checks topic and action selection for case status questions
subjectType: AGENT
subjectName: Case_Helper
testCases:
- utterance: What is the status of case 00001026?
expectedTopic: case_status
expectedActions:
- GetCaseStatus
expectedOutcome: The agent tells the user the current status of the case.Topic and action names in expectedTopic and expectedActions must match the names in your org. Copy them from the retrieved metadata and not from memory.
Common errors
- Wrong topic chosen. Two descriptions overlap. Rewrite them so each states a distinct job, add exclusions to the scope, and rerun the same utterance.
- Right topic, no action called. The action description does not match how users phrase the request, or the action is not attached to that topic. Check the topic's action list.
- Action called with a wrong or missing input. The input description is vague. Say what the value looks like, for example "The case number, such as 00001026."
- Agent answers from general knowledge instead of calling the action. The topic instructions do not tell it to use the action. Add an instruction such as "Always look up case status with the Get Case Status action. Do not guess."
- Action fails at runtime. The running user lacks access to the Apex class, flow, or underlying objects. Check permissions before changing descriptions.
- Test fails after a rename. Update the expected names in the test spec.
Summary
The planner picks a topic from its description, then picks actions from that topic's list. Actions can be standard, Apex, Flow, prompt template, or external service, and the planner treats them alike because it reads only their metadata. Write precise descriptions, give each topic one job, and verify the choices with preview and tests.
Exercise
A user asks "Where is my order?" The agent picks the Case Status topic instead of an Order Tracking topic. Both topic descriptions mention "status." Name two changes you would make, and say how you would confirm the fix.
Show answer
Rewrite both descriptions so each states a distinct job. For example, Case Status covers support cases identified by a case number, and Order Tracking covers shipping and delivery of placed orders. Add an exclusion to the Case Status scope, such as "Does not answer questions about orders or shipments." Confirm by rerunning the same utterance in sf agent preview, then add it to a test spec with expectedTopic set to the Order Tracking topic and run sf agent test run.