Sub topic 12
Deploying the action and agent configuration
You have written the Apex class, tested it, and registered it as an action in Agent Builder. All of that lives in one org, probably a sandbox or scratch org. This chapter covers moving the same action and its agent configuration to another org, and what to check once it arrives.
This chapter assumes you can use the Salesforce CLI (sf) and have a Salesforce DX project. It also assumes you understand sandboxes, change sets or source deployment, and permission sets. Agent metadata types and their names change between API versions, so confirm the details below against the Metadata API reference for your API version.
What you are deploying
The action is more than the Apex class. It has several layers, and each layer is a separate metadata component.
| Layer | Metadata type | What it holds |
|---|---|---|
| Code | `ApexClass` | `GetOpenCasesAction` and its test class `GetOpenCasesActionTest` |
| Action | `GenAiFunction` | The agent action. It points to the Apex class and carries the action description |
| Topic | `GenAiPlugin` | The topic. It lists the actions it can use and holds the topic instructions |
| Agent | `GenAiPlannerBundle` (older API versions: `GenAiPlanner`, `Bot`, `BotVersion`) | The agent definition and the topics assigned to it |
| Access | `PermissionSet` | Access to the Apex class, plus object and field permissions |
The Metadata API calls the topic type GenAiPlugin and the action type GenAiFunction. These names are internal. Agent Builder shows "topic" and "action".
Components depend on each other in a fixed order. The action cannot exist before the class it calls. The topic cannot reference an action that is missing. The agent cannot reference a missing topic.
A deployment that includes everything resolves this order itself. Problems appear when you deploy only part of the chain. If the target org already has the agent, a deployment that changes only the class can pass. If you deploy only the action and the class is missing, it fails.
The action metadata
A GenAiFunction file is small. It names the Apex class as its invocation target and repeats the description that the agent reasons over.
<?xml version="1.0" encoding="UTF-8"?>
<GenAiFunction xmlns="http://soap.sforce.com/2006/04/metadata">
<description>Lists the open support cases for an account, newest first</description>
<invocationTarget>GetOpenCasesAction</invocationTarget>
<invocationTargetType>apex</invocationTargetType>
<isConfirmationRequired>false</isConfirmationRequired>
<masterLabel>Get Open Cases</masterLabel>
</GenAiFunction>invocationTarget is the class name. The platform finds the single @InvocableMethod inside it. This is why one class can hold only one invocable method, as covered earlier in this tutorial.
When you create the action through Agent Builder, the platform also generates input and output schema files that describe your @InvocableVariable fields. Do not write these by hand. Retrieve the action after you create it in the org and commit what comes back. That way the schema in source control matches what the org generated.
sf project retrieve start \
--metadata ApexClass:GetOpenCasesAction \
--metadata ApexClass:GetOpenCasesActionTest \
--metadata "GenAiFunction:*" \
--metadata "GenAiPlugin:*" \
--target-org dev-sandboxIf you only want one action, name it instead of using *. The name is the API name, which Agent Builder derives from the label (for example Get_Open_Cases). Check the exact value in the action's detail page or with sf org list metadata --metadata-type GenAiFunction.
A manifest for the whole chain
A manifest (package.xml) lists the components to retrieve or deploy as one unit. Using one manifest for both retrieve and deploy keeps the two directions consistent.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>GetOpenCasesAction</members>
<members>GetOpenCasesActionTest</members>
<name>ApexClass</name>
</types>
<types>
<members>*</members>
<name>GenAiFunction</name>
</types>
<types>
<members>*</members>
<name>GenAiPlugin</name>
</types>
<types>
<members>*</members>
<name>GenAiPlannerBundle</name>
</types>
<types>
<members>Case_Agent_Access</members>
<name>PermissionSet</name>
</types>
<version>65.0</version>
</Package>Two notes on this file. First, the wildcard * pulls every action, topic, and agent in the org, not just yours. That is fine for a small development org. In a shared org, list the specific components, or you will deploy other people's unfinished work. Second, set <version> to an API version your source org and target org both support. A target org on an older release cannot accept metadata types or fields that are newer than its release.
Deploying with tests
Run the deployment with the manifest, and name the tests you want to run.
sf project deploy start \
--manifest manifest/package.xml \
--target-org uat-sandbox \
--test-level RunSpecifiedTests \
--tests GetOpenCasesActionTestUse --dry-run first to validate without saving:
sf project deploy start \
--manifest manifest/package.xml \
--target-org production \
--test-level RunSpecifiedTests \
--tests GetOpenCasesActionTest \
--dry-runTest rules differ by target.
- A sandbox or scratch org deployment does not require tests by default. Run them anyway. The action's behavior in the target depends on that org's data, sharing, and field-level security.
- A production deployment that includes Apex must run tests. Production requires at least 75% overall Apex code coverage. With
RunSpecifiedTests, each class you deploy must also meet that coverage itself. - A test that passed in your sandbox can fail in the target. A common cause is a validation rule, required field, or flow in the target org that your test data does not satisfy. The
Caseinsert inGetOpenCasesActionTestis a typical place for this to happen.
Access in the target org
The action runs with WITH USER_MODE, so the data a user sees depends on the user's own access. In an agent, that user depends on the agent type. An employee-facing agent runs as the person chatting with it. A service agent runs as an agent user assigned to it. Whichever user it is, that user needs all of these:
- Access to the Apex class
GetOpenCasesAction, granted through a permission set or profile. - Read access to
CaseandAccount, and to the fields in the query:CaseNumber,Subject,AccountId,IsClosed, andCreatedDate. - The Agentforce permission set or license that the agent type requires.
Include the permission set in your manifest, as shown above. Do not rely on profile changes in a deployment, because profiles overwrite more than you intend. Permission sets add only what you list. After deployment, assign the permission set to the right users or to the agent user. Assignment is data, not metadata, so the deployment does not carry it.
If the permission set is missing, the usual symptom is an action that deploys cleanly and then returns "No open cases were found for this account." The query ran in user mode, found nothing it was allowed to see, and returned the empty-result message you designed. This is easy to mistake for a real empty account, so check access first.
Activating the agent
Agent versions that arrive by deployment are not necessarily active. Open the agent in Agent Builder in the target org, confirm the topic and action are attached, and activate the version. If you deploy a change to an agent that is already active, you may need to deactivate it before the deployment succeeds, or deploy a new version. Look at the deployment error message, since this behavior depends on the type and API version.
Checking the result
After deploying, repeat in the target org the same checks you ran in the development org.
- Open the agent in Agent Builder. Confirm the action appears under the topic with the description you wrote.
- In the preview panel, send a prompt that should trigger the action, such as "What open cases does Acme have?" Use an account that exists in the target org.
- Open the trace or reasoning detail for the turn. Confirm the agent picked
Get Open Casesand passed anaccountIdand not a name. - Test an account with no open cases and confirm the agent explains that nothing was found.
- Test as a user without the permission set, if you can. The agent should fail in a way that you can describe to the user.
Record the results. These five checks are your release test for the action.
Common deployment problems
| Symptom | Likely cause | Fix |
|---|---|---|
| `GenAiFunction` fails with a missing target | The class is not in the package or failed to compile | Add the class to the manifest and deploy both together |
| Production deploy blocked by tests | No tests specified, or coverage under 75% | Add `--test-level RunSpecifiedTests --tests ...` and cover the class |
| Action appears but the agent never uses it | Topic does not list the action, or the description differs from the one tested | Retrieve the topic, confirm its action list, and review the descriptions |
| Action always returns the empty message | Running user lacks class, object, or field access | Assign the permission set to the running user |
| Unknown metadata type error | Target org on an older release than the manifest version | Lower `<version>` or wait for the org to upgrade |
| Deployed an unrelated agent | Wildcard in the manifest | List components explicitly |
Keeping source and org in step
Treat source control as the source of truth. If someone edits an action description in Agent Builder in the sandbox, that edit exists only in that org. Retrieve before you deploy, and review the diff, so that a teammate's edit is not overwritten or silently lost. Because the description drives the agent's choices, a one-line change in a GenAiFunction file is a behavior change. Review it as you would review code.
Exercise
Your deployment to production passes in a sandbox but is rejected in production, even though the Apex compiles. The command was sf project deploy start --manifest manifest/package.xml --target-org production. What is missing, and what should you add?
Show answer
Production requires tests when the deployment contains Apex. Add a test level and the test class, for example --test-level RunSpecifiedTests --tests GetOpenCasesActionTest. Each deployed class must also reach the required code coverage (75% overall), so check that the test covers the class.
Exercise
After deployment, the agent in the target org always answers "No open cases were found for this account," even for an account that has open cases. The Apex code and the agent configuration match the sandbox. What is the most likely cause, and how do you confirm it?
Show answer
The running user probably lacks access. Because the query uses WITH USER_MODE, a user without the Apex class, or without read access to Case and its fields, gets no rows and so receives the empty-result message. Confirm by checking that the permission set was assigned to that user or agent user in the target org, and by running the query as that user. Permission set assignment is data, so the deployment did not carry it.