Sub topic 11
Activating and deactivating an agent
Creating an agent with sf agent create leaves it inactive. Users and channels can't reach it until you activate it. This chapter covers the activation commands, what activation changes, and how to fit activation into a safe release flow.
This chapter assumes you have these skills:
- You can authorize an org with the Salesforce CLI.
- You have created an agent from a spec.
- You know that agent metadata lives in a Salesforce DX project.
- You have run
sf agent test runat least once.
What activation means
An agent in Agentforce has versions. Each version holds the topics, instructions, and actions that define its behavior. A version is either active or inactive. Activation makes a version available to the channels and surfaces the agent is assigned to, such as a messaging channel or an embedded service.
Three rules shape the workflow:
- Only one version of an agent is active at a time.
- Edits to topics, instructions, and actions are safest on an inactive version. Some changes to an active version are rejected, so plan to deactivate before you deploy them.
- Activation changes live behavior, so treat it as a release step. Don't treat it as a build step.
The status is stored in the agent's metadata as well as in the org. In a DX project the agent appears as Bot and BotVersion metadata, and the version file carries a status value. That detail matters later when you decide what to commit to source control.
The commands
The agent plugin provides two commands. Both take the agent's API name and a target org.
sf agent activate --api-name Case_Helper --target-org my-dev-org
sf agent deactivate --api-name Case_Helper --target-org my-dev-orgThe API name is the same value you passed to --api-name in sf agent create. It isn't the display name, so use Case_Helper and not Case Helper.
| Task | Command | Result |
|---|---|---|
| Make the agent available to users | `sf agent activate` | The agent version becomes active |
| Take the agent out of service | `sf agent deactivate` | The version becomes inactive |
| Machine-readable output for scripts | add `--json` | JSON with a status and a result or an error |
Flags can differ between plugin versions. Run sf agent activate --help to see the options your installed version supports, such as selecting a specific version when an agent has several. Update the plugin with sf plugins update if a flag documented here is missing.
Running a command in a script is easier when you ask for JSON and check the exit code:
#!/usr/bin/env bash
set -euo pipefail
ORG="my-dev-org"
AGENT="Case_Helper"
if sf agent activate --api-name "$AGENT" --target-org "$ORG" --json > activate.json; then
echo "Activated $AGENT in $ORG"
else
echo "Activation failed. Output saved to activate.json" >&2
cat activate.json >&2
exit 1
fiThe CLI exits with a non-zero code when the command fails. With set -e a pipeline stops at that point instead of carrying on as if the agent were live.
Checking the current status
Don't guess whether an agent is active. Query it. The agent's version is stored in BotVersion, and you can read its status with a SOQL query:
sf data query \
--query "SELECT DeveloperName, VersionNumber, Status FROM BotVersion WHERE BotDefinition.DeveloperName = 'Case_Helper'" \
--target-org my-dev-orgThe Status column shows Active or Inactive. Run this before and after an activation step in a script. A mismatch between what you expected and what the org reports is a useful early warning.
You can also see the status in the source file after a retrieve:
sf project retrieve start --metadata Bot:Case_Helper --target-org my-dev-orgOpen the retrieved BotVersion file and look for the status element. Retrieving captures the org's current state, so a version someone activated in Setup will show as active in your files.
A safe change cycle
For an agent that's already live, a typical change goes through these steps:
- Retrieve the latest metadata so your local files match the org.
- Make the change on a branch, such as a new instruction or an updated action description.
- Deactivate the agent in a sandbox or dev org if the change touches an active version.
- Deploy the metadata with
sf project deploy start. - Preview the conversation with
sf agent previewfor a quick manual check. - Run the test suite with
sf agent test run. - Activate only when the tests pass.
As a script, steps 3 to 7 look like this:
sf agent deactivate --api-name Case_Helper --target-org my-dev-org
sf project deploy start --source-dir force-app --target-org my-dev-org
sf agent test run --api-name Case_Helper_Tests --wait 10 --result-format human --target-org my-dev-org
sf agent activate --api-name Case_Helper --target-org my-dev-orgIf the test command exits with a failure, a script with set -e stops before the activate line. That's the behavior you want. Without it, a failing test is only a message on the screen.
Versions and what to do in production
Deactivating and redeploying in place is fine for a dev org. In production, it causes downtime between the deactivate and the activate. Two approaches reduce that risk.
Create a new version and switch. Develop the change as a new version of the agent while the current version stays active. Test the new version. Then activate it. Because only one version is active at a time, activating the new version replaces the current one, and the previous version becomes inactive. The old version remains available if you need to switch back.
Validate elsewhere first. Deploy and test the change in a sandbox that mirrors production. Production then receives a change that already passed. The production step is a deploy followed by an activation.
| Approach | Downtime | Rollback | Best for |
|---|---|---|---|
| Deactivate, deploy, activate in place | Yes, between deactivate and activate | Redeploy the old metadata | Dev and scratch orgs |
| New version, then activate | Short or none | Activate the previous version | Production agents with real traffic |
| Validate in sandbox, deploy to production | Depends on the production step | Same as the production step | Teams with a promotion pipeline |
Whichever approach you choose, record it in your release notes so the person on call knows how to undo a bad release.
Status and source control
Because BotVersion stores its status, the status can travel with your commits. This has consequences.
- If you commit a version with
Activestatus and deploy it to another org, the deployment may try to activate it there. That can be what you want in a release pipeline and a surprise in a developer's scratch org. - If a teammate retrieves metadata while an agent is active in the org, their working copy picks up
Active, and a later commit can change the status without anyone meaning to.
Pick one convention and enforce it in code review:
- Commit the status as
Inactive, and always activate withsf agent activateas an explicit pipeline step. The status then shows up in the pipeline log and not as a change in a diff. - Commit the real status and accept that deploys carry it. This keeps the repo true to production but needs careful handling of lower environments.
The first option is easier to reason about for most teams. It also matches the idea that activation is a release action.
Common problems
- The command can't find the agent. You probably used the display name. Use the API name, and confirm the agent exists in the target org with the SOQL query above.
- Activation fails after a deploy. The version may be incomplete. For example, a topic may reference an action that was not deployed. Check that the action's Apex class or flow is in the org, then retry.
- Deploy fails on an active agent. Deactivate first, deploy, then activate again.
- The status in the org differs from your files. Someone changed it in Setup. Retrieve again and decide which state is correct before you commit.
- A user still gets the old behavior. Confirm which version is active with the query. Then check that the channel is assigned to this agent.
Exercises
Exercise
A teammate's pull request deploys cleanly to a scratch org, but the agent in that org is now Active, and nobody ran sf agent activate. What is the most likely cause, and what convention prevents it?
Show answer
The committed BotVersion metadata has an Active status, so the deploy carried the status into the org. Commit the status as Inactive and activate only through an explicit sf agent activate step in the pipeline. Add a review check for any diff that changes a BotVersion status.
Exercise
Write the shell commands that deploy the project, run the CaseHelperTests suite, and activate the agent only if the tests pass. Which shell option makes the script stop at a failed test?
Show answer
Use set -e (ideally set -euo pipefail) at the top of the script, then run sf project deploy start --source-dir force-app --target-org my-dev-org, then sf agent test run --api-name Case_Helper_Tests --wait 10 --result-format human --target-org my-dev-org, then sf agent activate --api-name Case_Helper --target-org my-dev-org. Because the test command exits non-zero on failure, set -e ends the script before the activate line runs.