Sub topic 15
ECA metadata and source-driven deployment
This chapter shows how an external client app (ECA) is represented as metadata files, how to retrieve and deploy them with the Salesforce CLI, and how to handle values that must not live in source control. It assumes you have read the chapters on ECA settings versus policies and on distribution states. You also need a Salesforce DX project and a working sf login to a sandbox or scratch org.
Why ECAs suit source control
A connected app stores everything in one ConnectedApp record. That mixes developer choices (callback URL, scopes) with admin choices (permitted users, IP relaxation). An ECA splits these across several metadata types. The split lets a team put developer-owned settings in version control and promote them through environments. Admins can then tune policies separately in each org.
The metadata types
| Metadata type | Holds | Typical owner |
|---|---|---|
| `ExternalClientApplication` | Label, contact email, description, distribution state | Developer |
| `ExtlClntAppGlobalOauthSettings` | Global OAuth settings, including the consumer key and callback URL | Developer |
| `ExtlClntAppOauthSettings` | The OAuth settings of the app, such as scopes and enabled flows | Developer |
| `ExtlClntAppOauthPolicies` (OAuth policies) | Permitted users, IP relaxation, refresh token behavior, session rules | Admin |
| `ExtlClntAppConfigurablePolicies` and related policy types | Other admin-controlled policy values | Admin |
Field names change between API versions. Retrieve a real app from your org and read the files before you write them by hand. Don't copy field names from memory or from older blog posts.
Source-format files go in per-type folders, for example externalClientApps/, extlClntAppGlobalOauthSettings/, extlClntAppOauthSettings/ and extlClntAppOauthPolicies/. Each file name starts with the app's API name.
Retrieve an existing ECA
Create the app once in Setup, then pull it into source. The first command retrieves by type. The second retrieves a single app by name.
sf project retrieve start \
--metadata ExternalClientApplication \
--metadata ExtlClntAppGlobalOauthSettings \
--metadata ExtlClntAppOauthSettings \
--metadata ExtlClntAppOauthPolicies \
--target-org my-sandbox
sf project retrieve start \
--metadata ExternalClientApplication:Orders_Sync \
--target-org my-sandboxIf you prefer a manifest, list the same types with a wildcard member in package.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>*</members>
<name>ExternalClientApplication</name>
</types>
<types>
<members>*</members>
<name>ExtlClntAppGlobalOauthSettings</name>
</types>
<types>
<members>*</members>
<name>ExtlClntAppOauthSettings</name>
</types>
<types>
<members>*</members>
<name>ExtlClntAppOauthPolicies</name>
</types>
<version>62.0</version>
</Package>Use an API version at least as new as the one your org supports for these types. If a type is not recognized, raise the version.
Read the main file
The ExternalClientApplication file is the root of the app. A minimal one looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<ExternalClientApplication xmlns="http://soap.sforce.com/2006/04/metadata">
<contactEmail>[email protected]</contactEmail>
<description>Syncs orders to the warehouse system</description>
<distributionState>Local</distributionState>
<isProtected>false</isProtected>
<label>Orders Sync</label>
</ExternalClientApplication>distributionState is Local for an app used only in this org and its sandboxes, or Packaged for an app that ships inside a package. You can't change an app from Packaged back to Local after the fact, so decide before you commit the value.
The consumer key and secret
OAuth settings can be deployed, but the consumer secret is a credential, not configuration. Treat the two values differently:
- The consumer key identifies the app. It is safe to see in an authorize URL, and it can be retrieved with the settings.
- The consumer secret is not stored in your source files. Read it in Setup in each org, then store it in your secret manager or CI secret store.
- Each org that receives a deployment generates its own credentials. Don't assume the key you used in the sandbox works in production.
- If a secret leaks, rotate it in the target org and update the stored value. No source change is needed.
For JWT bearer integrations, the certificate you upload to the app is the public half of a key pair. Keep the private key out of the repository. Store it in your CI secret store and write it to a temporary file only while the job runs.
Deploy to another org
Deploy the app files to a sandbox or scratch org with a source-directory deploy:
sf project deploy start \
--source-dir force-app/main/default/externalClientApps \
--source-dir force-app/main/default/extlClntAppGlobalOauthSettings \
--source-dir force-app/main/default/extlClntAppOauthSettings \
--source-dir force-app/main/default/extlClntAppOauthPolicies \
--target-org my-sandbox \
--test-level NoTestRunPreview first with sf project deploy preview --target-org my-sandbox, which lists what would change without changing it. For production, use --dry-run to validate before the real deploy.
Deploy order matters. The root ExternalClientApplication must exist before the settings and policy files that reference it. A single deploy start command handles that for you. Split deployments can fail with a missing-parent error.
What to version and what not to
Version the root app, the OAuth settings and the global settings. Whether to version policies depends on who owns them.
- If developers own the whole integration, version the policies. Every environment then gets the same permitted-user and IP rules.
- If admins tune policies per org, leave the policy files out of the deployment. Otherwise each deploy overwrites their changes.
- If production policies differ from sandbox policies, keep separate files per environment and deploy the right set from your pipeline.
Permission set assignments are separate metadata. If the policy says "Admin approved users are pre-authorized", the permission sets that grant access must also be deployed, or nobody will be able to log in.
A pipeline shape
The diagram shows a common promotion path for an ECA.
Common errors
- Missing parent app. A settings file references an ECA that is not in the deployment or the target org. Include the root file.
- Unknown type. The API version in
package.xmlorsfdx-project.jsonis too old. Raise it. - Policies overwritten. An admin edited policies in production and the next deploy replaced them. Exclude policy files, or use
.forceignoreto skip them for that target. - Login fails after deploy. The app deployed, but no permission set or profile grants access under a policy that requires approved users. Deploy the access metadata too.
- Wrong credentials. An integration still holds the sandbox key. Use the key and secret from the org it calls.
To skip policy files for a single environment, add their folder to .forceignore:
**/extlClntAppOauthPolicies/**Check the result
After a deploy, confirm the app works by requesting a token and calling the API. Use the flow the app enables, as in the earlier chapters, and then check the version resource:
curl https://MyDomain.my.salesforce.com/services/data/ \
-H "Authorization: Bearer $ACCESS_TOKEN"A JSON list of API versions means the token is valid and the deployed app accepts it.
Exercise
Your team deploys an ECA from a sandbox to production. Admins in production tightened the IP relaxation policy last week. After the deploy, the integration's IP rules are back to the sandbox values. What happened, and how do you prevent it?
Show answer
The deployment included the OAuth policy metadata file, which replaced the admin's production values with the sandbox values. Prevent it by leaving the policy files out of production deployments, for example by listing the policy folder in .forceignore for that target or by deploying only the app and settings types. Alternatively, keep a production-specific policy file under version control and deploy that one.