Sub topic 2
Setting up the Salesforce CLI for agent work
This chapter prepares your machine for the rest of the tutorial. By the end you will have a current Salesforce CLI, the agent plugin installed and checked, and a Salesforce DX project ready to hold agent metadata.
Prerequisites and scope
This chapter assumes you can use a terminal and have worked with Salesforce DX projects (sfdx-project.json, the force-app folder). It also assumes you have installed software with a package manager before.
Authorizing an org is covered in the next chapter. Here you only confirm that the tools exist and behave as expected. Nothing in this chapter needs a connected org, except one optional default setting at the end.
What you are installing
Agentforce DX is not a separate product to download. It is a set of commands under the sf agent topic in the Salesforce CLI. The pieces are:
| Piece | What it does | How you get it |
|---|---|---|
| Salesforce CLI (`sf`) | The command-line program that talks to your orgs | Installer or npm |
| Agent plugin (`@salesforce/plugin-agent`) | Adds `sf agent generate`, `create`, `preview`, `test`, and related commands | Bundled in current CLI releases, or installed as a plugin |
| Deploy and retrieve commands | Move agent metadata between your project and an org | Part of the core CLI |
| A Salesforce DX project | The folder where agent metadata lives under source control | `sf project generate` |
Two points about the CLI itself matter here.
- The
sfcommand (version 2) is the current CLI. The oldersfdxcommand is the legacy interface. The agent commands are only documented forsf, so do not usesfdxfor this work. - Agent commands change faster than most CLI features. Flags and subcommands can differ between releases, so treat
--helpoutput on your machine as the authority over any tutorial text, including this one.
Step 1: Install or update the CLI
You can install the CLI in two common ways. Pick one and stay with it, because mixing them leads to two copies of sf on your path.
Option A: the installer. Download the installer for your operating system from the Salesforce CLI download page. This version updates itself with sf update.
Option B: npm. If you already manage Node.js, install the CLI globally. Use a current LTS release of Node.js.
node --version
npm install --global @salesforce/cli
sf versionThe npm install cannot use sf update. To upgrade an npm-installed CLI, run the install command again with the @latest tag:
npm install --global @salesforce/cli@latestFor an installer-based CLI, update with:
sf updateIf you are unsure which copy you are running, check where the shell finds it:
which sf
sf version --verboseOn Windows, use where sf instead of which sf. The verbose output lists the CLI version, the Node.js version it uses, and the plugins that are loaded.
Step 2: Confirm the agent plugin is present
A CLI plugin is an add-on package that contributes commands. Recent CLI versions ship the agent plugin with the core. Check whether it is loaded:
sf plugins --core
sf pluginsThe first command lists plugins bundled with the CLI. The second lists plugins you installed yourself. Look for @salesforce/plugin-agent in one of them.
If it is missing, install it:
sf plugins install @salesforce/plugin-agentThen confirm that the commands exist:
sf agent --helpThe help output lists the subcommands of sf agent. You should see entries for generating specs, creating agents, previewing, testing, activating, and deactivating. The exact list depends on your version.
To see details for one plugin, such as its installed version, run:
sf plugins inspect @salesforce/plugin-agentCore plugin versus user-installed plugin
The two sources behave differently, and this is a common source of confusion.
- A core plugin is pinned by the CLI release. It changes when you update the CLI.
- A user-installed plugin is stored separately and takes priority over the bundled one. It stays at the version you installed until you update it with
sf plugins update.
If you install the plugin yourself and later update the CLI, your installed copy can fall behind what the CLI would have bundled. If you do not need a specific plugin version, remove your copy and let the CLI provide it:
sf plugins uninstall @salesforce/plugin-agentStep 3: Choose a release channel (optional)
The CLI has release channels. The default is stable. Two others exist for earlier access:
| Channel | Use it when |
|---|---|
| `stable` | You want the supported release. This is the right default. |
| `stable-rc` | You want to try the next stable release before it ships. |
| `nightly` | You need a fix that has merged but not been released. Expect instability. |
Channels apply to installer-based CLIs through sf update:
sf update stable-rcMove back to stable when you have what you need. Do not run nightly in a shared team workflow, because teammates and CI will not match your behavior.
Step 4: Create a DX project for agent metadata
Agents are metadata, so they belong in a Salesforce DX project under version control. If you do not already have a project, create one:
sf project generate --name agent-project
cd agent-project
git initThe generated project contains sfdx-project.json and a force-app/main/default folder. The packageDirectories entry in sfdx-project.json tells the CLI where source lives. Agent metadata you retrieve later lands under that directory, in folders named after the metadata type.
The tutorial's examples also write files to a specs/ folder, such as specs/caseAgentSpec.yaml. Create it now so later commands do not fail on a missing path:
mkdir specsKeep specs/ in source control with the rest of the project. A spec is the input to agent creation, so reviewers should see it change in pull requests.
Step 5: Set defaults to shorten commands
Almost every agent command takes --target-org. You can set a default so you can omit it. After you authorize an org in the next chapter, run:
sf config set target-org my-dev-org
sf config listWithout --global, the setting applies only to the current project directory. That is usually what you want, because it stops a command in project A from running against the org of project B.
How the pieces fit together
The diagram shows the order of setup and which step depends on which.
A verification script
Running these checks by hand is fine once. If you share the setup with a team, a short script helps. This one fails with a clear message if the agent commands are not available.
#!/usr/bin/env bash
set -euo pipefail
if ! command -v sf >/dev/null 2>&1; then
echo "sf CLI not found. Install @salesforce/cli first." >&2
exit 1
fi
sf version
if ! sf agent --help >/dev/null 2>&1; then
echo "The sf agent commands are missing. Update the CLI or install @salesforce/plugin-agent." >&2
exit 1
fi
if [ ! -f sfdx-project.json ]; then
echo "Run this from the root of a Salesforce DX project." >&2
exit 1
fi
echo "Setup looks good."Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `sf: command not found` | CLI not installed, or its folder is not on `PATH` | Reinstall, then open a new terminal |
| `sf agent` is not a recognized command | Old CLI, or the plugin is not installed | Update the CLI, or run `sf plugins install @salesforce/plugin-agent` |
| A flag from this tutorial is rejected | Your plugin version differs from the one documented | Run the command with `--help` and use the flags it lists |
| `sf update` says it does not apply | The CLI was installed through npm | Upgrade with `npm install --global @salesforce/cli@latest` |
| Two different versions appear in `sf version` and `which sf` | Both an installer copy and an npm copy exist | Remove one copy |
| Command fails outside a project | You are not in a directory with `sfdx-project.json` | `cd` into the project root |
Exercise
You installed the CLI with npm. After a month, sf agent --help shows fewer subcommands than the documentation describes. You run sf update and it reports that the update does not apply. What should you do, and how do you confirm that the agent plugin is current afterward?
Show answer
Because the CLI came from npm, upgrade it with npm install --global @salesforce/cli@latest. Then run sf plugins --core and sf plugins to see whether the agent plugin is bundled or user-installed. If a user-installed copy exists, it overrides the bundled one, so either run sf plugins update or remove it with sf plugins uninstall @salesforce/plugin-agent so the CLI provides the current version. Confirm with sf plugins inspect @salesforce/plugin-agent and sf agent --help.