Sub topic 14
Troubleshooting common problems
This chapter covers the failures you are most likely to hit in the agent development loop: setup, authorization, spec generation, creation, preview, metadata, actions, tests, and activation. It assumes you have completed the earlier chapters. You should be comfortable with the sf command line, know what a Salesforce DX project looks like, and have run sf agent generate agent-spec, sf agent create, sf agent preview, and sf agent test run at least once.
A method for diagnosing problems
Most failures fall into one of four layers. Check them in this order, because a problem in a lower layer produces confusing symptoms in the layers above it.
- Tooling: the CLI and agent plugin are installed and current.
- Org: you are authorized to the right org, and your user has the required permissions.
- Metadata: the agent, topics, and actions exist in the org and in your project.
- Behavior: the agent selects the expected topic and actions and gives the expected answer.
Tooling problems
The `sf agent` commands are not found
If sf agent generate agent-spec returns a "command not found" style error, the agent plugin is missing or outdated. Agent commands change faster than the core CLI, so an old plugin is a common cause of flags that are "unknown" or behavior that differs from the documentation.
sf version --verbose
sf plugins
sf update
sf plugins updateRead the plugin list for the agent plugin and its version. If it is absent, install it with sf plugins install agent, then run sf agent --help to confirm the topic is available.
A flag is rejected
An "unexpected argument" or "unknown flag" error usually means one of two things. Either your plugin version predates the flag, or you copied a command written for a different version. Run the command with --help and compare. Do not guess at flag names, because the help output matches the plugin you actually have installed.
Authorization and permission problems
Wrong org or expired login
Commands such as sf agent create act on whatever --target-org resolves to. A stale alias, or a default org that points at a different sandbox, sends your work to the wrong place. Before debugging anything else, check where you are pointing.
sf org list
sf org display --target-org my-dev-orgIf the org shows as expired or disconnected, authorize again with sf org login web --alias my-dev-org. In a script, pass --target-org explicitly on every command, so a changed default cannot redirect it.
The org rejects the command
If authorization succeeds but a command fails with a message about a feature, a permission, or an unsupported operation, check three things:
- Agentforce is enabled in the org. Not every org type has it.
- Your user has the permission sets that allow you to create and manage agents.
- You are not targeting an org type that lacks the feature, such as an org created from a scratch org definition that does not enable it.
Errors of this kind come from the org, not the CLI. Reinstalling the CLI will not fix them. Ask an admin to confirm the setup, or compare your user with one that can build the same agent in the Setup UI.
Spec and creation problems
The generated spec is poor
sf agent generate agent-spec produces topics from your --role, --company-name, and --company-description values. Vague input produces vague topics. If the spec has overlapping or irrelevant topics, edit the YAML by hand before running sf agent create. The spec is meant to be reviewed. Treat it as a draft, not as final output.
`sf agent create` fails or creates something unexpected
Check these causes first:
- Name collisions.
--api-namemust be unique in the org. IfCase_Helperalready exists, creation fails or conflicts with the existing agent. Pick a new API name, or retrieve the existing agent and work from that. - Invalid spec path. A wrong
--specpath produces a file error. Use a path relative to where you run the command. - Spec YAML errors. Bad indentation or a missing field in the spec is reported as a parse or validation error. Fix the YAML and run the command again.
After creation, retrieve the metadata and look at what the org actually holds instead of assuming it matches the spec.
Preview problems
sf agent preview is the fastest place to see misbehavior, so use it to separate two cases: the agent never ran the action, or it ran the action and the action returned bad data.
| Symptom in preview | Likely cause | What to do |
|---|---|---|
| Agent answers generically and calls no action | Topic or action description does not match the utterance | Rewrite the descriptions in plain language that matches how users ask |
| Agent picks the wrong topic | Two topics have overlapping scopes | Narrow each topic's scope and instructions |
| Action is called but returns "Not found" | Input value is wrong, or the record is not visible to the running user | Test the Apex directly with a known record |
| Agent cannot start | Agent is missing, not created, or the API name is misspelled | Check `--api-name` and `--target-org` |
The agent decides which action to call by reading labels and descriptions. If the description on your @InvocableMethod is vague, the agent has little basis to choose it. This is why the Apex example uses label='Get Case Status' and a description saying exactly what the method returns.
Metadata and source control problems
Agent metadata consists of several related components, not one file, and a partial retrieve or deploy leaves them out of sync. Typical symptoms are a deployed agent that has no topics, or a diff that shows changes you did not make.
- Retrieve everything the agent depends on. If you retrieve the agent but not its related topic and action components, a later deploy to another org can fail on missing references.
- Deploy dependencies first. An action backed by Apex needs the Apex class in the target org before the agent that references it. Deploy the class, then the agent.
- Expect noisy diffs. Orgs may reorder or normalize metadata on retrieve. Retrieve into a clean branch, review the diff, and commit only intended changes.
- Do not edit the same agent in the Setup UI and in source at once. The next retrieve or deploy overwrites one set of changes with the other. Pick one authoring path for each agent.
git status
sf project deploy start --source-dir force-app --target-org my-dev-orgRead the deploy output for component-level errors. A single failing component can block the rest.
Custom action problems
When an Apex-backed action fails, test the Apex without the agent. This takes the agent's reasoning out of the picture.
- The action does not show up for the agent. Confirm the class is deployed, the method has
@InvocableMethod, and the class and method are visible to the agent's running user. - Input is empty or wrong. Check that
@InvocableVariablefields are public and thatrequired=truefields can be filled from what the user says. A user who gives "case 1026" may not match aCaseNumberof00001026, so tell the agent in its instructions what format to use. - Results are missing. An invocable method must return a list, and the returned list should match the input list one to one. Returning fewer results than requests breaks the pairing.
- Sharing hides records. The example class uses
with sharing, so the running user's record access applies. A user who cannot see a case gets "Not found" even though the case exists.
Test problems
Test failures come in two kinds: the test could not run, and the test ran but the agent did not behave as expected.
The test will not deploy or run
sf agent test create needs a valid spec. The subjectName must be the agent's API name, such as Case_Helper. A misspelling there fails the test even if the agent is fine. Also check that the agent exists in the target org and that you pass the same --target-org to create and run.
If sf agent test run --wait 10 returns before results are ready, the test is still running. Fetch results later with the job ID that the run prints.
sf agent test results --job-id <job-id> --result-format human --target-org my-dev-orgThe test ran and failed
Compare each expectation with what the agent did.
testCases:
- utterance: What is the status of case 00001026?
expectedTopic: case_status
expectedActions:
- GetCaseStatus
expectedOutcome: The agent tells the user the current status of the case.Three mismatches cause most failures:
expectedTopicorexpectedActionsdo not match the names in your org. Copy the exact names from the retrieved metadata.- The case number in the utterance does not exist in the test org, so the outcome fails even though the topic and action are correct.
- The
expectedOutcomeis too specific. Describe the meaning of the answer, not exact wording, because responses vary.
A test that passes in one run and fails in the next usually has an expected outcome that is too narrow, or data that changes between runs.
Activation problems
An agent that works in preview but not for real users is often inactive, or active on a different version than you tested. Check the activation state before you debug behavior. If an update does not take effect, the version you edited may not be the active one. Deactivate it, make the change, retest, and activate it again. In CI, run the tests before the activation step, so you never activate a version that has not passed.
A short checklist
- Run
sf version --verboseand update the CLI and plugin. - Run
sf org displayand confirm the org and your login. - Retrieve the agent and read what the org holds.
- Preview the failing utterance and note the topic and action chosen.
- Test the Apex action on its own.
- Compare test spec names with the metadata names.
- Confirm which agent version is active.
Exercise
A test case expects the action GetCaseStatus, but the test fails. In preview, the agent picks the right topic and calls the action, and the action returns "Not found" for case 00001026. Name two likely causes and one check for each.
Show answer
First, the case may not exist in the org you are testing against. Check by querying for CaseNumber 00001026 in that org. Second, the running user may lack access to the case, because the class uses "with sharing". Check by looking at the case as that user, or by running the Apex as that user. A third possibility is a number-format mismatch between what the user typed and the stored CaseNumber.
Exercise
You run sf agent create and the command targets a sandbox you did not intend. Which command would have shown you this before running it, and how do you prevent it in a script?
Show answer
sf org display --target-org <alias> shows which org an alias resolves to. In a script, pass an explicit --target-org on every command so a changed default org cannot redirect it.