Sub topic 10
Adding the Apex action to a topic in Agent Builder
An invocable Apex class does nothing for an agent until you register it as an action and assign that action to a topic. This chapter covers that step in Agent Builder. It assumes you have a deployed class like GetOpenCasesAction with @InvocableMethod and @InvocableVariable metadata, and that you can open Agent Builder in an org with Agentforce enabled. It also assumes you know what a topic is: a job the agent can do, with its own instructions and actions.
The working model
Registration creates an agent action that wraps your Apex method. Salesforce reads the class's inputs, outputs, labels, and descriptions and stores them as the action's definition. The topic then lists the action as one of the tools the agent may use while that topic is active.
The agent never sees your code. It sees the action name, its instructions, and its input and output descriptions. If those are weak, the agent picks the wrong action or fills inputs badly, even when the Apex is correct.
Add the action to a topic
- In Setup, open Agentforce Agents and open your agent in Agent Builder.
- Select the topic that should own the action. If none fits, create one first. A topic about case management suits
GetOpenCasesAction. - In the topic's action list, choose New Action.
- Set the reference action type to Apex, then pick your class. The picker lists invocable methods by their
label, so "Get Open Cases" is what you search for. - Review the generated action name and Agent Action Instructions. Rewrite the instructions so they say when to use the action, not only what it does.
- Under Inputs, open each one and check its instructions. Decide whether the agent must collect it from the user.
- Under Outputs, decide which fields the agent may show in its reply.
- Save, then add the action to the topic.
Menu names shift between releases, so follow the flow above rather than exact button text.
Inputs and outputs settings
| Setting | What it controls | Example |
|---|---|---|
| Require input | The agent must have a value before it runs the action | `accountId` is required |
| Collect data from user | The agent asks the user instead of guessing | Ask for `maxCases` only if the user mentions a count |
| Input instructions | How the agent finds or formats the value | "Use the Id of the account the user is viewing" |
| Show in conversation | Whether an output may appear in the reply | Show `summary`, hide internal Ids |
| Filter from agent | Hides an output from the reasoning engine | Hide fields that are only technical |
The required=true flag on an @InvocableVariable carries into the action, but you can still tune how the agent gathers the value here.
Common errors
- The class is missing from the picker. The class must be deployed, public or global, and contain exactly one
@InvocableMethod. Check that it compiled in this org and not only in a sandbox. - The agent never calls the action. The action is probably not assigned to the active topic, or the topic's scope and instructions do not mention the task. Compare the user's wording with the action instructions.
- The agent asks for an Id the user cannot know. The input instructions should say where the value comes from, such as record context or a previous action's output.
- You changed the Apex signature and the action broke. Adding or renaming an
@InvocableVariablechanges the action contract. Refresh or recreate the action so Builder reads the new inputs and outputs. - Two actions overlap. If "Get Open Cases" and "Find Cases" both fit a request, the agent may alternate between them. Merge them or make each description name its own boundary.
Exercise
You registered GetOpenCasesAction and added it to a topic, but in conversation the agent keeps asking the user for the account's 18-character Id. Name two changes in Agent Builder that would fix this.
Show answer
First, rewrite the instructions for the accountId input so they say where the value comes from, such as the account in the current record context or an earlier action's output. Second, turn off "Collect data from user" for that input if the value can be supplied automatically. Also check that the topic instructions tell the agent how to find the account.