Sub topic 15
Legacy Named Credentials and migrating to the new model
This chapter assumes you can write basic Apex callouts, navigate Setup, and assign permission sets. It also assumes you have read the earlier chapters on Named Credentials, External Credentials and principals.
What "legacy" means
Salesforce now has two Named Credential models. The legacy model puts the endpoint URL, the authentication protocol, and the secret or OAuth settings in one record. The current model splits these into a Named Credential and an External Credential, as you saw in earlier chapters.
Existing legacy records keep working. Apex code that uses callout:Name does not change between the two models. The difference is in Setup and in how access is granted.
| Aspect | Legacy Named Credential | Named Credential with External Credential |
|---|---|---|
| Where authentication lives | On the Named Credential itself | On the External Credential |
| Who can use it | Anyone who can run the Apex code | Users whose permission sets include the principal |
| Custom headers | Limited, set on the record | Formulas such as `{!$Credential.Weather_EC.ApiKey}` |
| Reuse | One authentication setup per record | One External Credential can serve several Named Credentials |
The main security gain is the permission set check. A legacy credential is available to any user who triggers the callout. With the new model, a user without principal access gets an error, which supports least privilege.
Why migrate
- You can restrict which users can trigger a callout.
- Several Named Credentials can share one External Credential, so a secret is stored once.
- Custom headers and formulas give you more control over what is sent.
- New features are built on the current model.
Migration steps
- Inventory your legacy records. In Setup, open Named Credentials and look at the Legacy tab.
- Create an External Credential that matches the legacy authentication protocol, with a principal. For a password or API key, enter the secret again. Secrets in legacy records cannot be read back.
- Create a new Named Credential with the same URL and link it to the External Credential.
- Create or update a permission set that grants access to the principal, and assign it to the users or integration users who run the callout.
- Update the Apex endpoint from the legacy name to the new one, or reuse the same name once the legacy record is deleted.
- Run your tests and perform a real callout in a sandbox before touching production.
Example: changing the endpoint
Only the name in the endpoint changes, and only if you used a different name for the new record.
// Before: legacy credential
req.setEndpoint('callout:Weather_Legacy/forecast?city=' + EncodingUtil.urlEncode(city, 'UTF-8'));
// After: new Named Credential backed by Weather_EC
req.setEndpoint('callout:Weather_API/forecast?city=' + EncodingUtil.urlEncode(city, 'UTF-8'));Because your mock tests assert on the endpoint prefix, update them as well, for example startsWith('callout:Weather_API/').
Common migration errors
- Callout fails for some users after migration. The permission set with principal access is not assigned to them. Legacy credentials never required it.
- Authentication fails with the new record. The secret was not re-entered on the principal, or the header formula refers to the wrong parameter name.
- Tests fail on the endpoint assertion. The test still expects the legacy name.
Exercise
After you migrate a Named Credential, an integration user gets an access error on the callout, while administrators succeed. What is the most likely cause, and how do you fix it?
Show answer
The integration user does not have the permission set that grants External Credential Principal Access. Legacy credentials had no such check. Add the principal to a permission set and assign that set to the integration user.