Sub topic 12
Testing the agent in the conversation preview
This chapter covers how to test an agent in the Agentforce Builder conversation preview and how to debug what you find. It assumes you have already created an agent with at least one topic, instructions, and some actions. You should know the Setup menu and read basic Apex. The Apex action from the previous chapter, GetOpenCaseCountAction, is used as the running example.
What the conversation preview does
The conversation preview is a chat panel inside Agentforce Builder. You type as a user, and the agent responds using its current, saved configuration. It runs against your real org data, so actions that query or change records do query or change them. Use sandbox-style test data in a Developer Edition org, and avoid pointing a test at records you care about.
The preview is also a debugging tool. Next to each agent reply you can open the reasoning details. These show what the agent did to produce the reply:
- Which topic it chose for your message.
- Which instructions from that topic it followed.
- Which actions it called, with the inputs it sent and the outputs it received.
- The final response it generated from those outputs.
Treat this trace as the main source of truth. The reply text alone can look correct while the agent took the wrong path, for example by answering from the model's general knowledge instead of calling an action.
How a single turn is processed
Each user message goes through the same steps. Knowing them tells you where to look when something goes wrong.
Failures cluster at three points:
- Topic selection. The agent picks the topic whose description best fits the message. Vague or overlapping descriptions cause misrouting.
- Action selection and inputs. The agent decides whether to call an action and how to fill its inputs. Weak action descriptions or missing instructions cause skipped calls or wrong inputs.
- Reply generation. The agent turns action output into text. Instructions that say how to phrase the result control this step.
Running your first test
Open your agent in Agentforce Builder and use the preview panel. Make sure your latest changes are saved first, because the preview uses the saved version. If you change a topic and the behavior does not change, an unsaved edit is a common cause.
Using the sample topic from earlier, "Case Status Questions", try this sequence:
- Type:
How many open cases does Acme have? - Open the reasoning details and confirm the topic was Case Status Questions.
- Confirm the Get Open Case Count action was called, and check the
accountIdinput. - Read the reply and compare the number with a report or list view of open cases for that account.
Step 4 matters. An agent can call an action correctly and still report a number you never verified. Always check the output against the source data at least once.
Why the account lookup is a likely failure
The Apex action takes an Id, not an account name. The topic instructions say to ask for the account if the user has not named one, but a name is not an ID. The agent needs a way to turn "Acme" into a record ID. Typically this means a standard action that looks up or queries records, which you added in the standard actions chapter. If the trace shows the action called with a name string or an empty value, the agent has no route to the ID. Fix it in one of these ways:
- Add or enable a standard record lookup action and mention it in the topic instructions before the case count step.
- Change the Apex request to accept an account name and query for the account inside the method.
The first keeps the Apex simple. The second moves lookup logic into code, where you must handle zero and multiple matches yourself.
Test cases to cover
Run more than the happy path. A useful set for any topic:
| Test type | Example message | What you expect |
|---|---|---|
| Happy path | How many open cases does Acme have? | Topic matches, action called, number reported |
| Missing input | How many open cases are there? | Agent asks which account |
| No result | How many open cases does Zzyzx Corp have? | Agent says it found nothing and does not guess |
| Zero count | Open cases for an account with none | Reports 0, not "no result" |
| Off topic | What is the weather today? | Agent says it cannot help with that |
| Rephrased | Any support tickets still open for Acme? | Same topic and action as the happy path |
| Mixed intent | Open cases for Acme, and update their phone number | Handles case status, declines or redirects the rest |
The zero count and no result rows are different on purpose. The Apex method returns 0 for an account with no open cases, so a zero is a valid answer. The instruction "Do not guess when the action returns no result" applies only when the action returns nothing or fails. If the agent says "I could not find that" for a real zero, the reply step is misreading the output. Add a line to the instructions such as "If the action returns 0, say the account has no open cases."
Reading the trace to diagnose problems
Match the symptom to the step that failed.
| Symptom | Likely step | Typical fix |
|---|---|---|
| Wrong topic chosen | Topic selection | Make topic descriptions narrower and less overlapping |
| Answer given with no action call | Action selection | Tell the instructions explicitly when to use the action; improve the action description |
| Action called with empty or wrong input | Inputs | Add an instruction to collect the value first; check the input label and description |
| Action errors | Action execution | Test the Apex or flow separately (see below) |
| Correct output, awkward reply | Reply generation | Add a phrasing instruction, such as "report the number in one sentence" |
| Agent answers off-topic questions | Scope | Add an explicit refusal instruction to the topic |
Change one thing at a time and rerun the same message. If you edit three instructions at once, you cannot tell which one fixed the problem. Language-model output also varies between runs, so repeat an important test two or three times before you decide it passes.
Debugging the action outside the agent
When the trace shows an action error, take the agent out of the loop. Run the Apex directly in the Developer Console or with the CLI and check it alone.
Deploy the class first if you have not yet:
sf org login web --alias agentforce-dev
sf project deploy start --source-dir force-app --target-org agentforce-devThen run this anonymous Apex against a real account ID from your org. Replace the placeholder before running it.
Account a = [SELECT Id FROM Account LIMIT 1];
GetOpenCaseCountAction.Request req = new GetOpenCaseCountAction.Request();
req.accountId = a.Id;
List<GetOpenCaseCountAction.Response> results =
GetOpenCaseCountAction.getOpenCaseCount(
new List<GetOpenCaseCountAction.Request>{ req }
);
System.debug('Open cases: ' + results[0].openCaseCount);If this works and the agent still fails, the problem lies in how the agent calls the action, not in the code. Common causes:
- Access. The class uses
with sharing, so it respects the running user's record access. The agent user may not see the account or cases that you see as an admin. Check the agent user's permission sets from the permissions chapter. - Class access. The agent user's profile or permission set may lack access to the Apex class.
- Stale metadata. After you change the class signature, such as adding an input, the agent may still use the old action definition. Reopen the action in the builder and refresh or re-add it.
To see the Apex side while testing in the preview, open Debug Logs in Setup, add a trace flag for the agent user, run the preview message again, and read the resulting log. Look for SOQL_EXECUTE_BEGIN lines to confirm the query ran and for exceptions.
Edge cases and limits
- Saved versus draft. The preview reflects saved changes. After editing, save and then restart the conversation.
- Conversation memory. The agent carries context within a conversation. A test that depends on a clean state needs a fresh conversation, so reset the preview between unrelated tests.
- Non-determinism. The same message can produce slightly different wording. Judge on behavior (topic, action, inputs, facts), not exact text.
- Real data effects. Actions that create or update records change your org. Test those with throwaway records.
- Developer Edition quotas. Developer Edition orgs have limited generative AI and Data Cloud capacity. Heavy repeated testing can reach those limits and produce errors unrelated to your configuration. The final chapter covers limits and troubleshooting.
Exercises
Exercise
In the preview, you ask "How many open cases does Acme have?" The reply is "Acme has 4 open cases," but the reasoning details show no action was called. What is wrong, and what would you change?
Show answer
The agent answered without calling the Get Open Case Count action, so the number was not read from your data and cannot be trusted. Fix it in the topic instructions or the action description. Make the instruction state that the agent must use the Get Open Case Count action to answer case count questions, and make the action description say what it returns. Then save, start a new conversation, and rerun the message and confirm the action now appears in the trace.
Exercise
The trace shows the action was called, but the reply says "I could not find that" for an account that has no open cases. The Apex method returns 0 in this situation. Which step failed, and how do you fix it?
Show answer
The reply generation step failed. It treated the output 0 as "no result". Add an instruction to the topic such as "If the action returns 0, tell the user the account has no open cases." Keep the "do not guess" rule for the case where the action returns nothing or errors. Rerun the test and check that the reply now reports zero.
Exercise
The agent works for you as an admin but fails with an error for a test user. The anonymous Apex run works. What do you check first?
Show answer
Check the access of the agent user, not the code. The class is declared with sharing, so it respects the running user's record access, and the user may lack access to the accounts or cases. Also confirm the agent user's permission set includes access to the Apex class. A trace flag and debug log for the agent user will show the exact error.