Sub topic 9
Deploying the agent to the target org
This chapter covers the deployment step itself. It assumes you already know basic Salesforce administration, Agentforce concepts (agents, topics, actions), Salesforce CLI usage, and the structure of a Salesforce DX project. It also assumes you have already retrieved the agent metadata and written a manifest, as covered in earlier chapters. Here you validate and deploy that manifest, read the results, and prepare for the activation step that follows.
What a deployment does
A deployment sends the metadata in your local project to the target org through the Metadata API. The org checks each component, saves it, and runs tests where required. If any component fails and you have not enabled partial deployment, the whole deployment rolls back. For an agent, that rollback behavior matters. The agent is made of several linked components, and a half-deployed agent is not usable.
Deployment creates or updates the agent definition. It does not make the agent usable for end users. Activation, user assignment, and permission setup are separate steps. A deployed agent usually arrives in an inactive state in the target org.
Pre-deployment checklist
Run through these checks before you start. Each one removes a common cause of failure.
- The target org has the required features turned on, such as Einstein generative AI and Agentforce.
- The manifest lists the agent metadata and every dependency: Apex classes, Flows, and permission sets.
- The source API version in your project is not newer than the target org supports.
- You have authorized the target org with the CLI and can see it in your org list.
- Any environment-specific values, such as the agent user's username or endpoint names, have been adjusted for the target.
Check your authorized orgs with this command.
sf org listThe output shows each alias and its connection status. If the target org shows as disconnected, log in again with sf org login web --alias target-org.
Validate first with a dry run
A validation-only deployment runs all the checks of a real deployment but saves nothing. The CLI flag for this is --dry-run. Use it every time you deploy to an org you care about.
sf project deploy start \
--manifest manifest/package.xml \
--target-org target-org \
--dry-runIf the dry run succeeds, run the same command without the flag.
sf project deploy start \
--manifest manifest/package.xml \
--target-org target-orgThe two commands must use the same manifest and the same source files. If you change anything between them, the validation result no longer describes what you are about to deploy.
Test levels and production targets
Deployments to a production org run Apex tests. Deployments to a sandbox or scratch org do not require them by default. The --test-level flag controls this.
| Test level | What runs | Typical use |
|---|---|---|
| `NoTestRun` | No tests | Sandboxes and scratch orgs only |
| `RunSpecifiedTests` | Tests you name with `--tests` | Production, when you know which tests cover your Apex |
| `RunLocalTests` | All tests in your org except managed package tests | Production default for most teams |
| `RunAllTestsInOrg` | Every test, including managed packages | Rarely needed |
If your agent depends on Apex classes, include tests for them in the deployment. Production requires at least 75 percent code coverage across the Apex code in the org, and each class in the deployment needs coverage as well. A command that targets production and names specific tests looks like this.
sf project deploy start \
--manifest manifest/package.xml \
--target-org prod-org \
--test-level RunSpecifiedTests \
--tests AgentOrderLookupTestThe class name AgentOrderLookupTest is a placeholder. Use the names of your own test classes.
Reading the deployment output
The CLI prints a table of components with their state. Each row shows the component type, name, and whether it was created, changed, or unchanged. When a component fails, the output lists its file path, line number, and an error message.
You can check on a deployment that is still running, or look at one that has finished, with these commands.
sf project deploy report --use-most-recent
sf project deploy resume --use-most-recentUse report to view the status. Use resume if your terminal session ended or the wait time expired while the deployment was still running. By default the CLI waits a limited time. You can lengthen the wait with --wait 30 to wait up to 30 minutes.
Component ordering and dependencies
The Metadata API works out the deployment order itself, so you do not list components in order inside the manifest. It does expect every referenced component to exist either in the target org or in the same deployment.
The relationships look like this.
If an action points to an Apex class or Flow that is neither in the manifest nor already in the target org, the deployment fails with a missing reference error. Fix this by adding the missing component to the manifest, not by removing the reference from the agent.
Deploying a subset of components
Sometimes you want to deploy only part of the agent, for example to update one Flow. Use --metadata to name the components directly instead of using a manifest.
sf project deploy start \
--metadata Flow:Get_Order_Status \
--target-org target-orgBe careful with this approach. A partial deployment can leave the agent and its actions out of step, for example when you update an action's input definition but not the Flow that receives it. For a full promotion between orgs, use the manifest.
Redeploying over an existing agent
The first deployment creates the agent in the target org. Later deployments update it. Pay attention to these points.
- Agent versions. A
BotVersionis a saved version of the agent. If a version is active in the target org, you may not be able to overwrite it. Deploy changes as a new version, or deactivate the agent first. - Deleted components. A normal deployment does not delete anything from the target org. If you removed a topic or action in the source, the old one stays in the target unless you use a destructive changes manifest.
- Manual edits. If someone edited the agent directly in the target org, your deployment overwrites those edits. Retrieve from the target first if you suspect drift.
When the deployment fails
Read the first error, not the last. Later errors are often side effects of the first one. These are the failures you will see most often during this step.
| Error message pattern | Likely cause | Fix |
|---|---|---|
| `Invalid reference` or `no such component` | A dependency is missing from the manifest and the target org | Add the Apex class, Flow, or permission set to the manifest |
| `Feature not enabled` | Agentforce or generative AI is not turned on in the target | Enable the features in Setup, then redeploy |
| `Insufficient access` | The deploying user lacks Modify Metadata or Customize Application | Use a user with the right permissions |
| `User not found` | The agent user's username does not exist in the target org | Update the username in the metadata or create the user |
| `Code coverage` failure | Apex tests are missing or too low in production | Add tests and run them with `--test-level` |
| `Version already active` | You tried to overwrite an active `BotVersion` | Deploy a new version or deactivate first |
The agent user error is the one people meet most often. Usernames are unique across all Salesforce orgs, so a sandbox username such as [email protected] cannot be reused in another org. Your metadata must point to a username that exists in the target. The next chapters cover this setup in detail.
Confirming the deployment worked
After a successful deployment, confirm the result before you move on.
- Run
sf project deploy report --use-most-recentand check that the status isSucceeded. - Open Setup in the target org and search for Agents. Confirm the agent is listed.
- Open the agent and check that its topics and actions are present.
- Check that the agent status is Inactive, which is the expected state before activation.
The last step in this chain is to assign the permission set and activate the agent.
sf org assign permset --name Agent_Action_Access --target-org target-org
sf agent activate --api-name My_Service_Agent --target-org target-orgThe permission set name and the agent API name are placeholders. Replace them with your own. Activation and verification are covered in a later chapter, so treat these commands as a preview.
Summary of the workflow
- Authorize the target org and check the pre-deployment checklist.
- Run the deployment with
--dry-runand fix every error. - Run the real deployment with the same manifest.
- Read the report and confirm the agent exists in the target.
- Move on to permissions and activation.
Exercise
Your validation-only deployment succeeds. Before running the real deployment, a teammate changes one Flow in your local project. Why should you validate again, and what happens if the Flow references an Apex class that is missing from the manifest?
Show answer
A dry run only describes the files that existed when it ran. After the Flow changed, the validation result no longer matches what you would deploy, so you must validate again. If the Flow references an Apex class that is in neither the manifest nor the target org, the deployment fails with a missing reference error. The fix is to add the Apex class to the manifest, not to remove the reference.
Exercise
You deploy an agent to the target org with the full manifest. The deployment shows Succeeded, but a topic you deleted in the source org still appears in the target agent. Why, and how do you remove it?
Show answer
A normal deployment only creates and updates components. It never deletes components from the target org. To remove the old topic, deploy a destructive changes manifest that names it, or delete it manually in the target org.