Sub topic 8
Handling environment-specific settings
This chapter assumes you can retrieve and deploy metadata with Salesforce CLI, you know what a Salesforce DX project is, and you have already built an inventory of your agent's dependencies. Here you will find the values in an agent that differ between orgs, and you will learn how to handle them without editing files by hand each time.
Why settings differ between orgs
An agent you retrieve from a sandbox carries values that belong to that sandbox. If you deploy those values unchanged, the agent may deploy cleanly and then fail at runtime. Common examples:
- The agent user's username. Usernames are unique across all Salesforce orgs, so a sandbox username such as
[email protected]does not exist in production. - Named credentials, external credentials, and endpoint URLs that Apex actions use to call outside systems.
- Record IDs, queue names, or email addresses hard-coded in a Flow or Apex class.
- Custom settings, custom metadata records, and custom labels that hold environment values.
- Sandbox-only data that prompts or actions refer to, such as test accounts.
Sorting settings into three groups
Before you change anything, sort each value into one of three groups.
| Group | Example | How to handle it |
|---|---|---|
| Same everywhere | Topic instructions, action descriptions | Deploy as-is |
| Different per org, in metadata | Agent user username, endpoint URLs | Replace at deploy time |
| Different per org, not in metadata | Secrets, passwords, API keys | Set in the target org after deploy |
Secrets belong in the third group. Do not commit them to source control. Deploy the named credential definition, then enter the secret in the target org.
Replacing values at deploy time
Salesforce CLI can swap values during deployment. Add a replacements section to sfdx-project.json. The CLI replaces a string in a file with the value of an environment variable at the moment you deploy. Your source files stay unchanged.
{
"packageDirectories": [{ "path": "force-app", "default": true }],
"sourceApiVersion": "63.0",
"replacements": [
{
"filename": "force-app/main/default/bots/My_Service_Agent/My_Service_Agent.bot-meta.xml",
"stringToReplace": "[email protected]",
"replaceWithEnv": "AGENT_USERNAME"
}
]
}Set the variable for the target org, then deploy:
export AGENT_USERNAME="[email protected]"
sf project deploy start \
--manifest manifest/package.xml \
--target-org target-orgThe filename path must match a file in your project. The agent's .bot-meta.xml file is where the bot user is stored. Check the exact file name in your own project before you run the command.
If the environment variable is not set, the CLI stops with an error by default. That is useful, because it prevents a deployment with the wrong value. You can set replaceWithEnv together with allowUnsetEnvVariable to skip the replacement, but avoid this for values the agent needs.
Keeping one file per environment
For values that are hard to replace with a single string, some teams keep one folder of overrides per environment, such as config/dev and config/prod. The pipeline copies the right folder over the source before the deployment. This is simple, but you now maintain several copies. Prefer replacements where it works, and use separate folders only for files that need larger changes.
Checking the result
After the deployment, confirm that the values arrived correctly.
- Open the agent in the target org and check that the agent user is the one you expect.
- Open each named credential used by an action. Confirm the endpoint is correct and the secret is set.
- Run an action that calls the external system and check that it reaches the right service.
A wrong agent user is a common cause of an agent that activates but cannot run its actions. The next chapters cover the agent user and activation in more detail.
Exercise
Your agent's metadata contains the sandbox agent username [email protected]. You need to deploy to production, where the username is [email protected], without editing the source file. What do you add to sfdx-project.json, and where does the production value come from?
Show answer
Add an entry to the replacements array with the filename of the agent's .bot-meta.xml file, stringToReplace set to [email protected], and replaceWithEnv set to a variable name such as AGENT_USERNAME. The production value comes from that environment variable, which you set (for example with export AGENT_USERNAME="[email protected]") before running sf project deploy start.