Sub topic 7
Retrieving, deploying, and versioning agent metadata
This chapter assumes you have a Salesforce DX project with sfdx-project.json and a force-app directory, an authorized org alias (my-dev-org), and basic Git skills. You should also have created the Case_Helper agent with sf agent create.
Why agents are metadata
After sf agent create, the agent exists only in the org. To review changes, share work, or promote the agent to another org, you need its definition as files in your project. An agent is stored as several metadata components. Which ones appear depends on your org's API version, but you will typically see:
BotandBotVersion, which hold the agent record and its versions.- A planner bundle (
GenAiPlannerBundle), which holds the topics and instructions. - Plugin and function components (
GenAiPlugin,GenAiFunction), which hold individual topics and actions.
The CLI accepts an Agent: name that expands to these related components, so you don't list each one.
Retrieving the agent
Run this from the project root:
sf project retrieve start \
--metadata "Agent:Case_Helper" \
--target-org my-dev-orgThe files land in your package directory, for example under force-app/main/default/bots/Case_Helper/ and the planner bundle folder. Run git status to see exactly what arrived.
If you edit the agent in Setup, retrieve again before you change files locally. Otherwise you may overwrite UI changes with older local files.
Deploying changes
Edit the files locally, then deploy:
sf project deploy start \
--metadata "Agent:Case_Helper" \
--target-org my-dev-orgComponents the agent refers to must exist in the target org first. A custom action backed by CaseStatusAction needs that Apex class deployed before, or in the same deployment as, the agent. Deploying them together is the safest choice.
sf project deploy start \
--metadata "Agent:Case_Helper" \
--metadata "ApexClass:CaseStatusAction" \
--target-org my-dev-orgThe sync loop
Versioning with Git
Commit the retrieved files like any other source. These habits keep history useful:
- Retrieve, then commit the untouched result as a baseline. Later diffs show only your intentional edits.
- Make one logical change per commit, such as "Tighten case status topic instructions".
- Review diffs of instruction text carefully. A reworded instruction can change agent behavior even when the XML structure is unchanged.
- Commit agent files and the Apex they depend on together.
git add force-app/main/default
git commit -m "Retrieve Case_Helper baseline"An agent also has its own version records (BotVersion). Those track versions inside the org. Git tracks your source history. They are separate, so a Git commit does not create a new agent version.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Retrieve returns nothing for the agent | The API name is wrong, or it differs from the display name | Use the API name (`Case_Helper`), not "Case Helper" |
| Deploy fails on a missing action | The Apex class or Flow is not in the target org | Deploy the dependency first or in the same command |
| Local edits disappear after retrieve | You retrieved over uncommitted changes | Commit or stash before retrieving |
| Deploy to an active agent is rejected | The active version is locked | Deactivate the agent, deploy, then reactivate (covered in the activation chapter) |
Exercise
You edited a topic's instructions in Setup yesterday. Today you pull the latest Git branch, change the same topic locally, and want to deploy. What should you do first, and why?
Show answer
Retrieve the agent from the org with sf project retrieve start --metadata "Agent:Case_Helper" before deploying, after committing or stashing your local work. This brings in yesterday's Setup change so you can merge it with your edit. Deploying first would overwrite the Setup change with your local files.