Sub topic 14
Moving credential metadata between orgs
This chapter assumes you can build a Named Credential, an External Credential and a permission set, and that you know how to run a metadata deployment. Here you will learn what travels between orgs and what you must set up again by hand.
What deploys and what does not
Credential setup is spread across several metadata types. Deploy them together.
| Metadata type | What it carries | Deploys? |
|---|---|---|
| `ExternalCredential` | Protocol, principals, parameter names | Yes |
| `NamedCredential` | URL, custom headers, link to the External Credential | Yes |
| `PermissionSet` | External credential principal access | Yes |
| `AuthProvider` | OAuth provider settings (only for OAuth setups) | Yes |
| Secret values | API keys, passwords, client secrets | No |
| User tokens | Per user authentication results | No |
Salesforce never exports secrets. The metadata holds the shape of the credential, and each org keeps its own values.
A manifest for the weather example
This manifest uses the names from the earlier outline.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/milestones/metadata">
<types>
<members>Weather_EC</members>
<name>ExternalCredential</name>
</types>
<types>
<members>Weather_API</members>
<name>NamedCredential</name>
</types>
<types>
<members>Weather_Callout_Access</members>
<name>PermissionSet</name>
</types>
<version>61.0</version>
</Package>Deploy it with the Salesforce CLI:
sf project deploy start --manifest manifest/package.xml --target-org uatInclude the External Credential and the Named Credential in the same deployment, because the Named Credential refers to the External Credential. The permission set file carries the principal access as an entry like this:
<externalCredentialPrincipalAccesses>
<enabled>true</enabled>
<externalCredentialPrincipal>Weather_EC-Weather_Principal</externalCredentialPrincipal>
</externalCredentialPrincipalAccesses>Steps after deployment
- Open the External Credential in the target org and enter the secret on the principal.
- Check the Named Credential URL. A sandbox usually calls a test endpoint and production calls the live one.
- Assign the permission set to the users or integration user who make callouts.
- For OAuth, confirm the authentication provider exists and has the right client ID, secret and callback URL. Then authenticate again.
- Run one real callout, for example from Anonymous Apex.
Handling per-org differences
Do not edit the URL in the source org and redeploy it every time. Keep one manifest, then change the URL in each target org after the first deployment. Alternatively, keep a separate version of the Named Credential file for each environment in your pipeline. Apex code needs no change, because it always uses callout:Weather_API.
Never commit secrets to the repository, and keep them out of the pipeline's logs.
Exercise
You deploy WeatherEC, WeatherAPI and WeatherCalloutAccess to a UAT sandbox. The deployment succeeds, but the callout fails with an authentication error. Name two things to check.
Show answer
Check that the secret (the ApiKey parameter) has been entered on Weather_Principal in the UAT org, because secrets are not deployed. Also check that the Named Credential URL is correct for UAT and that the calling user has the permission set assigned.