Sub topic 5
Retrieving the agent from the source org
This chapter covers how to pull an Agentforce agent out of a source org, such as a sandbox, into a local Salesforce DX project. It assumes you can already do the following:
- Authorize an org with the Salesforce CLI (
sf). - Work in a Salesforce DX project with a
force-appsource folder. - Describe the agent's topics, actions, and instructions in the Agentforce Builder.
You will leave with a retrieved agent on disk that you can inspect, commit to version control, and package for deployment in the next chapter.
What a retrieve does
A retrieve asks the source org for the metadata files that describe a component and writes them into your local project. It reads from the org and never changes it. You can run it as often as you like.
The retrieve gives you a local, reviewable copy of the agent. Once the copy is in Git, you can diff it, review it, and deploy the same files to several orgs. This is the main reason to retrieve instead of rebuilding the agent by hand in each org.
A retrieve returns only what you ask for. If you retrieve the agent and nothing else, the Apex classes and Flows behind its actions stay in the source org. The previous chapters covered the dependency inventory, and this chapter uses it. Each dependency on that list is either retrieved here or already lives in your repository.
Step 1: authorize the source org
Authorize both orgs now so that later chapters can reuse the aliases. The alias is a local nickname. The CLI uses it with --target-org so you don't have to type a username each time.
sf org login web --alias source-org
sf org login web --alias target-orgEach command opens a browser window. Log in as a user who can read the agent's metadata. A System Administrator is the simplest choice. If you use a narrower profile, it needs the Modify Metadata or Modify All Data permission, or the retrieve may return fewer components than expected.
Confirm that both aliases exist:
sf org listThe output lists each org with its alias, username, and connection status. A status of Connected means the CLI can use the org.
Step 2: confirm what exists in the org
Before you retrieve, list the agents in the source org. Doing so gives you exact API names to use in filters.
sf org list metadata --metadata-type Bot --target-org source-org
sf org list metadata --metadata-type GenAiPlanner --target-org source-orgEach row shows a component's full name, its type, and when it was last modified. Note the API name of your agent. An agent's API name often differs from its display label. For example, the label "Service Agent" may have the API name My_Service_Agent. Filters and manifests use the API name, so copy it exactly.
Step 3: retrieve the agent metadata
The core command retrieves each metadata type that makes up the agent:
sf project retrieve start \
--metadata Bot \
--metadata BotVersion \
--metadata GenAiPlanner \
--metadata GenAiPlugin \
--metadata GenAiFunction \
--target-org source-orgRun it from the root of your DX project, the folder that contains sfdx-project.json. The CLI writes the files into your default package directory, usually force-app/main/default.
Each --metadata flag names one metadata type. Because no member name follows the type, the command retrieves every component of that type in the org. That behavior is convenient for a first exploration and risky in an org that holds several agents. You will retrieve other teams' work and may later deploy it by accident.
Retrieve only one agent
Add a colon and the API name to limit the retrieve:
sf project retrieve start \
--metadata "Bot:My_Service_Agent" \
--metadata "GenAiPlanner:My_Service_Agent" \
--target-org source-orgA filtered retrieve has a limit. The planner, the topics (GenAiPlugin), and the actions (GenAiFunction) are separate components. Each one has its own API name, and those names may not match the agent's name. If you filter by name, you must list every topic and action that the agent uses. Missing one produces an agent that retrieves cleanly and then fails when it is deployed or run.
Two approaches avoid that problem:
- Retrieve whole types on a first pass, then delete the files that belong to other agents.
- Retrieve with a manifest that you build from the dependency inventory. The next chapter covers manifests.
Step 4: read what you retrieved
After the command finishes, the CLI prints a table of the files it wrote. Open the project and look for folders like these:
| Folder under `force-app/main/default` | Metadata type | What it holds |
|---|---|---|
| `bots/` | Bot, BotVersion | The agent definition and its versions |
| `genAiPlanners/` | GenAiPlanner | The planner that selects topics for each request |
| `genAiPlugins/` | GenAiPlugin | Topics, with their instructions and the actions each topic can use |
| `genAiFunctions/` | GenAiFunction | Actions the agent can call, each pointing to an Apex class, Flow, or other invocation target |
A typical result looks like this:
force-app/main/default/
├── bots/
│ └── My_Service_Agent/
│ ├── My_Service_Agent.bot-meta.xml
│ └── v1.botVersion-meta.xml
├── genAiPlanners/
│ └── My_Service_Agent.genAiPlanner-meta.xml
├── genAiPlugins/
│ └── Order_Support.genAiPlugin-meta.xml
└── genAiFunctions/
└── Get_Order_Status.genAiFunction-meta.xmlThe exact folder layout and file names depend on your CLI version and the API version of your project. If your output differs a little from this tree, that is not necessarily a problem.
Open the files and check three things:
- Bot version files. Each version of the agent has its own
botVersion-meta.xml. If the agent has several versions, decide which one you plan to promote. Version metadata can include an active or inactive status, and activation is handled after deployment. - Action targets. Open a
genAiFunction-meta.xmlfile and find the element that names the invocation target, such as an Apex class or a Flow. Compare it with your dependency inventory. Every target named here must exist in the target org. - Org-specific values. Look for hard-coded record IDs, usernames, and URLs inside instructions or configuration. IDs differ between orgs. The "Handling environment-specific settings" chapter explains how to deal with these.
The retrieve workflow
The diagram shows the order of the steps and the point where the dependency inventory feeds back into the retrieve.
Retrieve the dependencies
Once the agent metadata is on disk, retrieve what its actions rely on. Use the names from your dependency inventory:
sf project retrieve start \
--metadata "ApexClass:OrderStatusService" \
--metadata "Flow:Update_Case_Status" \
--metadata "PermissionSet:Agent_Action_Access" \
--target-org source-orgThe names in this command are placeholders. Replace them with the components your agent uses. If a component already exists in your repository, check whether the source org has a newer version. A stale local copy is a common cause of a deployment that works but behaves differently in the target org.
You can preview what a retrieve would change before running it:
sf project retrieve preview --target-org source-orgThis command compares the org with your local project and shows which components are new, changed, or deleted in the org. It depends on source tracking, which sandboxes support by default. Production orgs and sandboxes without tracking enabled do not support it. In those orgs, use an explicit retrieve instead.
Common problems
The retrieve returns zero files. Check the metadata type spelling (GenAiPlanner, not GenAIPlanner) and the API name. Then check that your user has permission to view the agent. A typo in a filter returns a warning, not a hard failure, so read the output.
The command says a metadata type is not supported. The type may not exist in your project's API version, or the feature may not be enabled in the org. Check sourceApiVersion in sfdx-project.json and raise it if it is older than the version that introduced the agent types. The sample manifest in this tutorial uses version 63.0.
Files appear in an unexpected folder. The CLI writes to the package directory marked as the default in sfdx-project.json. If you have more than one package directory, check which one has "default": true.
Local edits are overwritten. A retrieve replaces local files with the org's version. Commit or stash your changes first, so Git can show you what the retrieve changed.
What to check before moving on
Before you write the manifest, confirm this short list:
- The agent's Bot, BotVersion, GenAiPlanner, GenAiPlugin, and GenAiFunction files are in the project.
- Each action's Apex class or Flow is also in the project.
- The permission sets that give access to those actions are in the project.
- You have scanned the files for hard-coded IDs and URLs.
- The files are committed, so later changes are easy to see.
Exercise
You run sf project retrieve start --metadata "Bot:My_Service_Agent" --target-org source-org. The command succeeds, but when you later deploy the files, the agent's topics and actions are missing in the target org. What caused this, and how do you fix it?
Show answer
The filter retrieved only the Bot component. Topics (GenAiPlugin), actions (GenAiFunction), and the planner (GenAiPlanner) are separate metadata components, and a retrieve does not follow dependencies. Retrieve those types too, either whole or by their individual API names, and also retrieve the Apex classes, Flows, and permission sets the actions rely on. Building a manifest from your dependency inventory is the most reliable way to avoid missing anything.
Exercise
Why might sf project retrieve preview fail in your source org, and what do you use instead?
Show answer
The preview command relies on source tracking, which is not available in production orgs or in sandboxes where tracking is not enabled. In that case, run sf project retrieve start with explicit --metadata flags or a manifest, then use git diff to see what changed.