Sub topic 15
Legacy Named Credentials and migrating to the new model
This chapter assumes you have read the earlier chapters on Named Credentials, External Credentials, principals, and permission set access. You should be comfortable with callout: endpoints in Apex. Many orgs still contain Named Credentials created before the External Credential model existed. Here you learn how to recognize them, how they differ from the new model, and how to move them over without breaking callouts.
What a legacy Named Credential is
A legacy Named Credential keeps everything in one record: the endpoint URL, the authentication protocol, the identity type, and the secrets. The new model splits these across two records, a Named Credential for the location and an External Credential for authentication.
A legacy record has these settings:
- URL: the base endpoint of the service.
- Identity Type: Named Principal (one shared identity) or Per User (each user authenticates separately).
- Authentication Protocol: Password, OAuth 2.0, JWT, JWT Token Exchange, AWS Signature Version 4, or No Authentication.
- Username and Password, or an Authentication Provider for OAuth 2.0.
- Generate Authorization Header, which controls whether Salesforce builds the
Authorizationheader for you. - Allow Merge Fields in HTTP Header and Allow Merge Fields in HTTP Body, which let Apex use credential merge fields in requests.
Apex code that calls a legacy credential looks the same as code for the new model:
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Weather_API/forecast?city=Oslo');
req.setMethod('GET');
HttpResponse res = new Http().send(req);Because the callout: syntax is identical, Apex alone cannot tell you which model sits behind Weather_API. You have to check in Setup.
Differences between the two models
| Aspect | Legacy Named Credential | Named Credential with External Credential |
|---|---|---|
| Records | One record holds URL and authentication | A Named Credential and an External Credential |
| Secrets | Stored in the Named Credential itself | Stored on an External Credential principal |
| Who may use it | Named Principal credentials are not gated by permission sets | Users need External Credential Principal Access in a permission set |
| Per user setup | Users authenticate in their personal authentication settings | Users authenticate per principal, and the permission set controls who sees it |
| Custom headers | Merge fields such as `{!$Credential.Username}` | Formulas such as `{!$Credential.Weather_EC.ApiKey}` |
| Reuse | One credential per endpoint | One External Credential can serve several Named Credentials |
The access difference matters most. A legacy Named Principal credential did not require a permission set grant. A migrated credential does. If you skip that grant after migrating, callouts that worked yesterday fail today.
Why migrate
Salesforce treats the External Credential model as the current one, and new features land there first. Legacy credentials continue to work, so migration is not an emergency. These reasons favor doing it deliberately:
- Access control. Permission set principal access gives you a per-credential, per-profile audit trail that legacy Named Principals lack.
- Reuse. Several endpoints that share one login can point to one External Credential, so a rotated secret changes in one place.
- Maintainability. Authentication changes no longer force edits to the endpoint record.
- Support. Documentation and newer protocol options target the new model.
Check the release notes for your org's release before you commit to a schedule. Salesforce can change retirement guidance, and this chapter does not state a date.
Migration strategy
The safest approach is parallel build, then cutover. Apex callouts identify the credential only by its name, so you can rehearse the change in a sandbox before touching production.
Step 1: Inventory
List every legacy Named Credential and the code that uses it. Apex classes, flows with external actions, and external services can all reference a credential. Search your code for callout: and review the Named Credentials list in Setup.
Legacy credential: Weather_API
Identity Type: Named Principal
Protocol: Password
Username: svc_weather
Used by: WeatherService (Apex), Daily_Forecast_FlowFor each entry, record the identity type, the protocol, and any merge fields in headers or bodies. Those details decide how you rebuild it.
Step 2: Rebuild as an External Credential
Create an External Credential with the matching protocol. A legacy Password credential maps to a Basic authentication protocol with a principal that holds the username and password. A legacy OAuth 2.0 credential maps to an OAuth 2.0 protocol that reuses the same authentication provider or a new configuration. A legacy Per User identity maps to a Per User principal.
Do not copy the secret into a note or a ticket. Enter it directly on the principal.
Step 3: Create the new Named Credential
Create a Named Credential with the same URL and link it to the External Credential. Replace any legacy merge fields. The legacy {!$Credential.Username} and {!$Credential.Password} become parameter formulas on the External Credential:
Legacy header value:
{!$Credential.Username}
New header value:
{!$Credential.Weather_EC.Username}Test any header and body formulas on their own, because a wrong parameter name produces an error only when the callout runs.
Step 4: Grant access
Add the External Credential principal to a permission set under External Credential Principal Access, then assign that permission set to every user or integration user who runs the callout. This includes the user who runs scheduled and queueable Apex.
Step 5: Handle the name
Named Credential API names must be unique, so the new record cannot take the name Weather_API while the legacy record still holds it. You have two options:
- New name, code change. Create
Weather_API_v2, change thecallout:string in Apex, and deploy. This is easy to roll back, because you can switch the string back. - Same name, no code change. Rename or delete the legacy record, then create the new one under the original name. Callers need no edits, but there is a window with no working credential, and rollback means recreating the legacy record.
Option 1 suits production when you want a reversible change. Option 2 suits cases where many callers use the name, such as flows and external services.
Step 6: Use the migration action if offered
Recent Salesforce releases include a migration action on legacy Named Credential records that builds the External Credential and the new Named Credential for you. Availability depends on your release and credential type, and results can differ for custom protocols. Review whatever it creates. Confirm the principal, the permission set grant, and the headers before you rely on it.
Verifying the migration
Run the tests you wrote earlier with HttpCalloutMock. They confirm that the request has the right shape, but they do not exercise the credential, because mocks bypass it. Verify the real path in a sandbox:
// Run in Anonymous Apex as the user who will make the callout
String body = WeatherService.getForecast('Oslo');
System.debug(body);Run it as the real integration user, not as an administrator. An administrator may succeed because of broad access that the target user lacks.
Common problems
- Callout fails right after cutover. The permission set with principal access is missing or not assigned to the running user.
- Authentication errors with correct secrets. A header formula still uses a legacy merge field, or the parameter name is misspelled.
- Per User callouts fail for everyone. Users have not re-authenticated. Legacy authentication settings do not carry over to the new principal, so each user must connect again.
- Duplicate name errors. The legacy record still owns the API name.
- Deployment gaps. The Named Credential, External Credential, and permission set must all be deployed, or the target org has a record that points to nothing.
Exercise
Your org has a legacy Named Principal credential called BillingAPI. Apex calls it with callout:BillingAPI/invoices. You create an External Credential and a new Named Credential called BillingAPIv2 with the same URL. After you change the Apex endpoint to callout:BillingAPIv2/invoices, callouts fail for all users except administrators. What is the most likely cause, and how do you fix it?
Show answer
The users lack access to the new External Credential principal. Legacy Named Principal credentials needed no permission set grant, but the new model does. Administrators succeed because of their broad permissions. Add the principal under External Credential Principal Access in a permission set and assign it to the affected users, including the integration user that runs background Apex. Then retest as a non-administrator.