Sub topic 10
Adding the Apex action to a topic in Agent Builder
What you need before you start
This chapter assumes you have finished the earlier chapters. You should have a deployed Apex class with an @InvocableMethod, request and response wrapper types, and descriptions on every @InvocableVariable. The examples use the GetOpenCasesAction class from those chapters. You also need an org with Agentforce enabled, access to Agent Builder, and an agent that already has at least one topic.
Button names and panel layouts in Agent Builder change between releases. This chapter uses the labels current at the time of writing. If your org shows slightly different wording, look for the same concept.
What registration does
An invocable method is only Apex until an agent knows about it. Registering it creates a metadata record, an agent action, that points at your class and adds agent-facing configuration. The action then belongs to one or more topics.
The reasoning engine works in two steps:
- It reads the user's message and picks the topic whose classification description fits best.
- Inside that topic, it looks at the topic's instructions and the descriptions of the actions assigned to it, then decides whether to call one.
An action that is not assigned to the selected topic is not available in that turn, however good its description is.
Step 1: Make sure the agent user can run the class
Before the action can work, the identity that runs it needs access to the Apex class. Which identity that is depends on the agent type. A service agent runs as a dedicated agent user, and an employee-facing agent runs as the person chatting with it.
- In Setup, open the permission set assigned to that identity.
- Under Apex Class Access, add
GetOpenCasesAction. - Confirm the same permission set grants read access to Case and the fields the query selects.
Because the class uses WITH USER_MODE, a missing field permission causes a query error at run time, not an empty result. Most "the action works for me but not for the agent" reports come from this step.
Step 2: Open the topic
- In Setup, search for Agentforce Agents and open your agent in Agent Builder.
- In the topics list, select the topic where the action belongs. For the case lookup, that is a topic such as "Case Status" whose classification description covers questions about open or existing support cases.
- Open the topic's Actions tab or section.
Do not put the action in a broad catch-all topic just because it is convenient. The engine sees every action assigned to the selected topic, and more candidates make wrong choices more likely.
Step 3: Create the action from the Apex class
In the topic, choose to add a new action. The wording is usually New Action or Create Action. Then work through the form.
- For Reference Action Type, choose Apex.
- In Reference Action, search for and select your class. The picker lists classes with an
@InvocableMethod. If the class is missing, check that it is deployed, that it compiles, and that the method ispublic staticorglobal staticwith the annotation. - Set the Agent Action Label. It defaults to the method label ("Get Open Cases"). Keep it short and verb-led.
- Write the Agent Action Instructions. This is the text the engine reads when deciding whether to call the action.
- Set Loading Text, the short message shown while the action runs, such as "Looking up open cases...".
- Save the action, which opens its input and output configuration.
The form usually prefills the instructions from the method's description. Treat that as a starting point. The agent-level instructions can be more specific than the Apex description, because they can mention the conversation context.
Writing the action instructions
Good instructions say what the action does, when to use it, and when not to. For this action:
Use this action when the user asks about open, unresolved, or pending support cases for a specific account. It returns the number of open cases and a short list of case numbers and subjects, newest first. Do not use it for closed cases or for creating or updating cases. If no account has been identified, ask the user which account they mean before calling it.The last sentence helps because the accountId input is required. Without a hint, the engine may guess or call the action with a missing value.
Step 4: Configure inputs
The builder reads your Request class and lists one input per @InvocableVariable. Each input shows the label and description you wrote in Apex. You can adjust settings here:
| Setting | Effect | Use it when |
|---|---|---|
| Require input | The agent must have a value before calling the action. | The Apex field has `required=true`, which the builder usually carries over. |
| Collect data from user | The agent asks the user for the value if it is missing. | The user is the only source, such as a free-text subject. |
| Input description | Guides what the engine puts in the field. | The format matters, such as "a 15 or 18 character Salesforce Id". |
An input that the user cannot reasonably know, such as an Id, usually comes from an earlier action or from a context variable. If the user will never type it, do not mark it as collected from the user. Chain it from a lookup action in the same topic, or map it from a variable.
For maxCases, leave the input optional. Your Apex code already treats null or values under 1 as 5, so the agent can omit it safely. Keep that fallback in Apex. Do not rely on the engine always supplying a value.
Step 5: Configure outputs
The builder lists one output per @InvocableVariable on the Response class. Two settings matter:
- Show in conversation (the label may read "Show in conversation" or "Display"): when on, the value is shown to the user directly. Turn it off for values the agent should use for reasoning but not display, such as internal Ids.
- Filter from agent action (the label may read "Filter from agent action" or "Hide from agent"): when on, the value is hidden from the reasoning engine. Use it to keep sensitive values out of the model's context.
For the case summary, leave summary visible so the agent can quote it. caseCount is useful for phrasing such as "You have 3 open cases." Both stay on.
Step 6: Mention the action in topic instructions
Adding the action to the topic makes it available. Topic instructions tell the engine how to use it in context. In the topic's Instructions section, add lines such as:
When the user asks about open cases, identify the account first. Then use Get Open Cases and summarize the results in one or two sentences.
If the action reports that there are no open cases, tell the user that plainly and offer to help with something else.Refer to the action by its label so the connection is clear. Keep each instruction a single, testable statement. Long, overlapping instruction lists make behavior hard to predict.
Step 7: Save, activate, and preview
- Save the topic and the action.
- In the preview panel, enter a message the topic should handle, such as "What open cases does Acme have?"
- Open the reasoning or trace view for the turn. It shows which topic was chosen, whether your action was selected, and the exact input values sent.
- Fix problems in this order: topic choice, then action choice, then inputs, then Apex.
The next chapter covers testing in more depth. At this stage, you only need to confirm that the action is reachable.
Changing an action after registration
The registered action stores a copy of the input and output shape. If you later add, rename, or remove an @InvocableVariable, the agent action does not always update automatically. After a change to the Apex class contract, open the action and refresh or recreate it, then check each input and output again. Renaming a variable is the riskiest change because existing mappings may point at a name that no longer exists.
Descriptions you edit in Agent Builder apply only to that action. If you edit them in Apex and redeploy, check whether the builder shows the old text and update it by hand where needed.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Class does not appear in the picker | Not deployed, compile error, or no `@InvocableMethod` | Deploy again and check that exactly one method in the class is annotated. |
| Action never selected | Wrong topic, or instructions too vague | Check the topic selection in the trace, then tighten the action instructions. |
| Engine passes a name instead of an Id | Input description does not state the expected format | Add the format to the input description, or add a lookup action earlier in the flow. |
| Insufficient access error at run time | Agent identity lacks class or field access | Add the class and field permissions to the correct permission set. |
| Output not shown to user | Output hidden or filtered | Turn on **Show in conversation** and turn off the filter for that output. |
Check your understanding
Exercise
A teammate registers GetOpenCasesAction and assigns it to a topic named "General Questions". In preview, asking "What open cases does Acme have?" never calls the action, and the trace shows a different topic was selected. List two things you could change, and say which you should check first.
Show answer
Check first that the topic classification description for the topic that was chosen, and for the topic holding the action, are not overlapping or misleading. The action cannot be called unless its own topic is selected. Then either move the action into the topic that matches case questions (for example "Case Status") or rewrite the classification description of the topic that contains it so it clearly covers open case questions. Only after the topic is chosen correctly should you refine the action instructions.
Exercise
Your Response class has a field internalRecordId that the agent should not mention or reason about. Which output setting would you use in Agent Builder, and why is it better than deleting the field?
Show answer
Turn on the filter that hides the output from the agent (and leave it off the conversation display). This keeps the value out of the reasoning engine's context without changing the Apex contract. Other consumers of the invocable method, such as Flow, can still read the field, and your unit tests can still assert on it.