Sub topic 11
Testing the agent after deployment
This chapter assumes you have deployed and activated the agent in the target org. It also assumes you know the Salesforce CLI basics and how topics and actions fit together. Activation only shows that the metadata loaded. It does not show that the agent behaves the way it did in the source org, so you test that next.
What can differ after deployment
The metadata can be identical in both orgs while behavior differs. These are the usual causes:
- The agent user lacks permissions on an Apex class, Flow, or object that an action uses.
- Target org data differs, so a record lookup returns nothing.
- Environment-specific values, such as named credentials or endpoints, still point at the source org.
- A feature or setting the topic relies on is not enabled in the target org.
Test for each of these separately. That way a failure tells you where to look.
Step 1: Smoke test in preview
Start with a few utterances (the messages a user types) that cover each topic. You can use the Agent Builder preview panel or the CLI:
sf agent preview --api-name My_Service_Agent --target-org target-orgFor each utterance, check three things:
- The agent chose the expected topic.
- The agent called the expected action.
- The action returned real data and not an error.
A common failure here is that the agent picks the right topic but the action fails. This usually means a permission or data problem, not a metadata problem.
Step 2: Write a repeatable test spec
Manual checks do not scale. Define test cases in a YAML file instead. Each case holds an utterance and the topic, actions, and outcome you expect.
name: My Service Agent Tests
description: Post-deployment checks for the service agent
subjectType: AGENT
subjectName: My_Service_Agent
testCases:
- utterance: Where is my order?
expectedTopic: Order_Status
expectedActions:
- Get_Order_Status
expectedOutcome: The agent reports the current status of the order.
- utterance: What is the weather on Mars?
expectedTopic: Off_Topic
expectedActions: []
expectedOutcome: The agent politely declines and explains what it can help with.The topic and action names must be the API names that exist in the target org. Include at least one out-of-scope utterance, as in the second case. It checks that the agent's guardrails (its limits on what it will answer) survived the move.
Step 3: Create and run the test in the target org
Creating the test deploys it as metadata (an AiEvaluationDefinition) to the target org. Then you run it.
sf agent test create \
--spec specs/agent-tests.yaml \
--api-name My_Service_Agent_Tests \
--target-org target-org
sf agent test run \
--api-name My_Service_Agent_Tests \
--wait 10 \
--target-org target-orgThe --wait 10 option makes the command wait up to 10 minutes and print the results. Without it, the command returns a job ID, and you can fetch the results later with sf agent test results --job-id <id> --target-org target-org.
Because the test definition is metadata, you can retrieve it and keep it in version control with the agent. Then the same tests run against every org you deploy to.
Reading failures
| Symptom | Likely cause | Where to look |
|---|---|---|
| Wrong topic selected | Topic descriptions or instructions changed, or the agent version is not the active one | Confirm the active `BotVersion` |
| Right topic, action not called | The action is missing from the topic, or the agent user cannot access it | Agent user permission sets |
| Action called but returns an error | The Apex class or Flow lacks access, or an endpoint is wrong | Debug logs and named credentials |
| Action returns empty data | The target org has different or no data | Test records in the target org |
Exercise
A test case passes in the source sandbox. In the target org the agent picks the correct topic, but the action returns an access error. Which part of the deployment do you check first, and why?
Show answer
Check the agent user's permission sets in the target org. The correct topic means the agent metadata deployed and reasoning works. An access error on the action points to the agent user lacking access to the Apex class, Flow, or object, so assign the missing permission set and rerun the test.