Sub topic 11
Testing the action in the agent
This chapter assumes you have already written and unit-tested GetOpenCasesAction, registered it as an action on a topic in Agent Builder, and have an org with Agentforce enabled. Apex unit tests prove the method works. They don't prove the agent will pick the action, fill in its inputs, or explain the result well. Those checks happen in the agent itself.
What you are testing
Three things can fail between a user's message and your Apex code:
- Topic selection. The agent must route the request to the topic that holds your action.
- Action selection and inputs. The agent must choose your action and supply valid values for each input.
- Result handling. The agent must turn your response into a useful answer.
Run a conversation in the preview
Open the agent in Agent Builder and use the preview panel. Send a message that should trigger your action, such as "What open cases does the Acme account have?". Then check the reasoning details the builder shows for the turn. The exact name and layout of this view vary by release, so look for the panel that lists the selected topic, the chosen action, the input values, and the output.
Check these in order:
- The expected topic was selected.
- Your action was called, not skipped.
- Each input has the value you expect.
- The output matches what the method returned.
Common failures and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| Wrong topic selected | Topic description overlaps with another topic | Make the topic description and scope more specific |
| Action never called | The action description doesn't say when to use it | Rewrite the description to state the purpose and the trigger |
| Agent asks the user for an Id | `accountId` is an `Id` input and the agent has no source for it | Add an earlier action that looks up the account, or supply the value from context |
| Input is empty or wrong | Input description is vague | State the format and meaning in the `@InvocableVariable` description |
| Action runs but returns nothing useful | Missing records or no access | Check the empty-result message and the running user's permissions |
| Behavior differs from your tests | Preview user has different access than your admin user | Test as a user with the target profile or permission set |
The accountId row is the most frequent surprise for beginners. A user says "Acme", but the method needs a 15- or 18-character Id. The agent can't invent one. Design the conversation so some earlier step produces the Id.
Debugging when the output is wrong
The reasoning view shows what the agent sent and received. If the inputs look correct but the result is wrong, the problem is in Apex. Open the Debug Logs for the running user, reproduce the request, and look for the SOQL, any USER_MODE access errors, and unhandled exceptions. Add System.debug statements for the request list size and the result count while you investigate, and remove them afterward.
If the Apex is right but the answer is poor, change the Response descriptions or the topic instructions, not the code. After you change a description in Apex, deploy the class, then refresh the action in Agent Builder and start a new preview conversation. An old conversation can keep earlier context.
Write repeatable test cases
A single manual chat is not a regression test. Agentforce Testing Center lets you define test cases with an utterance, the expected topic, the expected action, and the expected outcome, then run them as a batch. Build a small set for each action:
- A request that should call the action (the normal case).
- A request that omits a required input.
- A request for an account that has no open cases, to confirm the empty-result message is explained clearly.
- A request that should not call the action, such as an unrelated question.
- A request as a low-access user, to confirm sharing and field-level security hold.
Rerun the set after any change to descriptions, topic instructions, or the Apex class. Agent behavior is not fully deterministic, so judge results over several runs and treat an occasional miss as a signal to tighten the descriptions.
Exercise
In the preview, the agent replies "Please provide the account Id" instead of calling GetOpenCasesAction. Name two ways to fix this without changing the Apex logic.
Show answer
Add an earlier action or topic step that finds the account from its name and returns the Id for the agent to pass along, or make the Id available from conversation context (for example, when the agent is used on an account record page). Improving the accountId input description to say where the value comes from also helps.