Sub topic 5
Retrieving the agent from the source org
This chapter assumes you know basic Salesforce administration, can use Salesforce CLI, and have a Salesforce DX project. You should also know what agents, topics, and actions are. Retrieval is the step that copies the agent's metadata from the source org (for example, a sandbox) into your local project, so you can review it and later deploy it.
What you will retrieve
An agent is spread across several metadata types. The previous chapters covered them. For this step you need these:
| Metadata type | What it holds |
|---|---|
| Bot | The agent record and its settings |
| BotVersion | A version of the agent, including its configuration |
| GenAiPlanner | The planner that links the agent to its topics |
| GenAiPlugin | A topic and the actions grouped under it |
| GenAiFunction | An individual action the agent can call |
Retrieving only these types gives you the agent definition. It does not give you the Apex classes, Flows, or permission sets that the actions use. You add those later, when you write the manifest.
Authorize both orgs
Log in to the source org and the target org now. Aliases let you refer to each org by name in later commands.
sf org login web --alias source-org
sf org login web --alias target-orgEach command opens a browser window. Log in with a user that can view setup metadata and the agent. You do not use the target org yet, but authorizing it now confirms that your credentials work before you reach the deploy step.
Retrieve the agent metadata
Run the retrieve from the root of your DX project. Each --metadata flag names one type.
sf project retrieve start \
--metadata Bot \
--metadata BotVersion \
--metadata GenAiPlanner \
--metadata GenAiPlugin \
--metadata GenAiFunction \
--target-org source-orgWhen you name a type without a member, the CLI retrieves every component of that type. In a sandbox with many test agents, that can pull in more than you want. To retrieve one agent, add the member name after a colon, for example --metadata Bot:My_Service_Agent. Use the API name, not the label shown in Setup.
Check what arrived
The files land in your package directory, usually force-app/main/default. Look for folders such as bots, genAiPlanners, genAiPlugins, and genAiFunctions. Open the files and check three things:
- The Bot and its BotVersion files exist for the agent you expected.
- The planner lists the topics (plugins) you built.
- Each plugin lists its functions, and each function names a target such as an Apex class or Flow.
The target names in the functions are your starting point for the dependency inventory. Write them down. Every Apex class, Flow, or prompt template named there must also exist in the target org, or the deploy fails.
Common errors
- "No source-backed components present" or an empty result. The member name is wrong or the type is not enabled in the source org. Check the API name and confirm Agentforce is turned on.
- Retrieved too much. A bare type name returns all members. Name the members you need.
- Wrong org. The
--target-orgflag on a retrieve points to the org you read from. Here that is the source org. A common slip is passingtarget-orgbecause of the flag's name. - Version mismatch. If the project's
sourceApiVersionis older than the agent metadata types need, some types may not be retrieved. UpdatesourceApiVersioninsfdx-project.json.
Exercise
You run sf project retrieve start --metadata Bot --target-org target-org and the project fills with agents you never built. What two things went wrong, and how do you fix them?
Show answer
First, a bare type name retrieves every member of that type, so name the agent, for example --metadata Bot:My_Service_Agent. Second, the command read from target-org, but retrieval should read from the source org, so use --target-org source-org. Also, retrieving Bot alone leaves out BotVersion, GenAiPlanner, GenAiPlugin, and GenAiFunction, so add those types.