Sub topic 7
Retrieving, deploying, and versioning agent metadata
This chapter covers how an agent created with sf agent create becomes files in a Salesforce DX project. It then covers how to move those files between orgs and keep them in Git. It assumes you can run sf project retrieve start and sf project deploy start, know the layout of a DX project (sfdx-project.json, force-app), and use basic Git. You should already have created Case_Helper in a dev org.
Why the agent is metadata
An agent in your org is a set of metadata components, not a single object. The Setup UI hides this, but the CLI exposes it. Once the agent exists, you can retrieve it, review it in a pull request, and deploy it to another org like any other component.
The exact set of types depends on the API version and on how the agent was built. These are the ones you will meet most often:
| Metadata type | What it holds |
|---|---|
| `Bot` | The agent record: name, agent type, and settings. |
| `BotVersion` | A version of the agent, including its state and the topics attached to it. |
| `GenAiPlannerBundle` | The planner configuration: topics, their instructions, and the actions they reference. Newer API versions use this type. |
| `GenAiPlugin` | A topic definition, in orgs that store topics as separate components. |
| `GenAiFunction` | An action definition, such as one pointing to an invocable Apex method or a flow. |
| `PermissionSet` | Access for the agent's running user and for people who use it. |
Run sf project retrieve preview or inspect your project after a retrieve to see which of these your org produced. Don't assume the list above matches your org exactly.
The round trip
The working loop treats the org as the place where an agent is run and the repository as the place where it is stored.
The block above is only a placeholder in prose form. The real diagram follows.
Retrieving the agent
Retrieve by naming the metadata types and the agent's API name. The Bot name matches the --api-name you gave to sf agent create.
sf project retrieve start \
--metadata "Bot:Case_Helper" \
--metadata "GenAiPlannerBundle:Case_Helper_v1" \
--target-org my-dev-orgRetrieving the Bot does not always pull in every component it depends on. If topics or actions are missing after a retrieve, add the related types explicitly. A wildcard such as --metadata GenAiFunction retrieves every action in the org, which is useful when you are unsure of the names.
After the retrieve, the files appear under force-app/main/default. The folder names below are typical. Your layout may differ by API version.
force-app/main/default/
bots/Case_Helper/
Case_Helper.bot-meta.xml
v1.botVersion-meta.xml
genAiPlannerBundles/Case_Helper_v1/
Case_Helper_v1.genAiPlannerBundle
genAiFunctions/GetCaseStatus/
GetCaseStatus.genAiFunction-meta.xml
classes/
CaseStatusAction.cls
CaseStatusAction.cls-meta.xmlIf your org has source tracking (scratch orgs and some sandboxes), sf project retrieve start with no flags pulls only remote changes. This is convenient while you edit in the UI, but it can retrieve more than you expect. Run sf project retrieve preview first to see what would change.
Editing and deploying
Editing in a text editor is practical for small changes, such as rewording a topic's instructions or its scope. Treat these files with care. They are generated XML, and a hand edit that breaks a reference fails at deploy time.
Deploy the agent and its dependencies together:
sf project deploy start \
--metadata "ApexClass:CaseStatusAction" \
--metadata "GenAiFunction:GetCaseStatus" \
--metadata "GenAiPlannerBundle:Case_Helper_v1" \
--metadata "Bot:Case_Helper" \
--target-org my-test-orgDeploy order matters in one way. A GenAiFunction that points at an Apex class needs that class to exist in the target. Putting the class in the same deployment handles this, because Salesforce resolves the dependency order inside a single deployment. Splitting them across two deployments means you must deploy the class first.
To check a deployment without changing the org, add --dry-run. For production you can also add --test-level to run Apex tests, which covers the invocable class. Agent tests (covered in the testing chapter) are separate from Apex tests and need the agent to exist first.
sf project deploy start \
--source-dir force-app \
--dry-run \
--target-org my-test-orgWhat a deployment does not carry over
Several things stay behind when you move an agent between orgs.
- Activation state. A deployed agent version is usually inactive in the target. Activation is a separate step, covered in a later chapter.
- Data the agent reads. Cases, knowledge articles, and records your actions query do not travel with metadata. A test that asks about case
00001026fails in an org where that case does not exist. - Users. The running user named in the agent's settings must exist in the target org and hold the right permissions. A username such as
[email protected]often has a different suffix in other orgs. - Features and licenses. The target org must have Agentforce and Einstein generative AI enabled. Otherwise the deploy fails with an error about unsupported metadata types.
Versioning with Git
Commit the retrieved files like any other source. A few habits keep history useful.
- Retrieve before you edit. If someone changed the agent in the Setup UI, your local copy is stale. Retrieve, check
git diff, and commit the retrieved state before making your own change. - Keep commits small. One commit per topic or action change makes review easier than a single commit with a whole regenerated agent.
- Commit the spec files too. The
specs/caseAgentSpec.yamland test spec files are inputs to the workflow. Storing them next to the metadata documents how the agent was meant to behave. - Review instruction text like code. The words in a topic's instructions are behavior. A rewording is a behavior change, so it deserves a reviewer.
Your .gitignore should leave agent metadata tracked and exclude only local state:
.sfdx/
.sf/
node_modules/
*.logHandling noisy diffs
Retrieved XML sometimes reorders elements or changes whitespace between retrieves, which produces diffs with no meaningful change. Two practical responses:
- Retrieve with the same CLI and API version each time. Set
sourceApiVersioninsfdx-project.jsonso everyone uses one version. - Review diffs by looking at instruction text and action lists first, and skim ordering changes.
{
"packageDirectories": [{ "path": "force-app", "default": true }],
"namespace": "",
"sourceApiVersion": "65.0"
}Use an API version that your org supports. If you set one newer than the org, the retrieve fails.
Versions inside the org
Git history and BotVersion are two separate kinds of versioning. Git records every change to the files. A BotVersion is a snapshot inside the org that can be active or inactive. A common pattern is to treat a new BotVersion as a release, and Git commits as the many edits between releases. Keep the BotVersion number in your commit messages so you can match a commit to what was activated.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Deploy fails with "no such component" on a `GenAiFunction` | The Apex class or flow it references is not in the target | Include the class in the same deploy, or deploy it first |
| Retrieved agent has no topics | The planner or topic types were not retrieved | Add `GenAiPlannerBundle` or `GenAiPlugin` to the retrieve |
| Deploy succeeds but the agent does not answer | The new version is inactive | Activate it, then preview again |
| "Unknown metadata type" | Agentforce is not enabled in the target, or the CLI is out of date | Enable the features, then run `sf update` |
| Agent works in dev but fails in test | Missing permission set or running user | Deploy the permission set and assign it |
Exercise
You deploy Bot:Case_Helper and GenAiPlannerBundle:Case_Helper_v1 to a test org. The deploy succeeds, but sf agent preview returns no useful answer, and a teammate says the case lookup action is missing. Name two likely causes and the command or step that addresses each.
Show answer
First, the GenAiFunction for the case lookup and the CaseStatusAction Apex class were not part of the deployment, so the topic has nothing to call. Add --metadata "GenAiFunction:GetCaseStatus" and --metadata "ApexClass:CaseStatusAction" to the deploy, or retrieve them from the dev org first if they are not in your project. Second, the deployed agent version is probably inactive in the target org. Activate it, and then preview again. It is also worth checking that the running user and permission set exist in the test org.
Exercise
Why should you retrieve before editing agent files, even when you are the only developer on the agent?
Show answer
Anyone with Setup access, including you in an earlier session, may have changed the agent in the org. If you edit a stale local copy and deploy it, you overwrite those changes silently. Retrieving first, reviewing the git diff, and committing the retrieved state gives you a clean baseline for your own change.