Sub topic 12
Letting each user authenticate to the external system
This chapter assumes you can build a Named Credential and an External Credential, and that you know how a permission set grants principal access. It covers the per user principal, where each Salesforce user signs in to the external system with their own account.
When a named principal is not enough
A named principal stores one identity for the whole org. Every callout reaches the external system as that identity, whoever triggered it. This is wrong when the external system must know who is acting, or when each person's data and permissions differ there. Examples are a personal calendar, a file store, or a CRM with its own record-level security.
A per user principal fixes this. Salesforce keeps a separate set of tokens or credentials for each user and sends the right ones with each callout.
Configure the per user principal
Most of the setup is the same as for a named principal.
- Open the External Credential and set an authentication protocol, usually OAuth 2.0 with an authentication provider.
- Add a principal and set its identity type to Per User Principal.
- Add the principal to a permission set under External Credential Principal Access.
- Assign that permission set to the users who need the callout.
External Credential: Calendar_EC
Authentication Protocol: OAuth 2.0
Principal: Calendar_User (Per User Principal)
Named Credential: Calendar_API
URL: https://api.example.com/calendar
External Credential: Calendar_EC
Permission Set: Calendar_Callout_Access
External Credential Principal Access: Calendar_EC - Calendar_UserEach user authorizes once
The permission set only lets a user use the principal. It does not sign them in. Each user must also authenticate to the external system themselves.
- The user opens their personal settings and goes to Authentication Settings for External Systems.
- They add a new entry and select the external credential and principal.
- They are sent to the external system's login page and approve access.
- Salesforce stores the resulting tokens for that user only.
The Apex code does not change
The callout looks the same as it does with a named principal. Salesforce picks the credentials of the running user.
public with sharing class CalendarService {
public static String getEvents() {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Calendar_API/events');
req.setMethod('GET');
req.setHeader('Accept', 'application/json');
HttpResponse res = new Http().send(req);
if (res.getStatusCode() != 200) {
throw new CalloutException('Calendar API returned status ' + res.getStatusCode());
}
return res.getBody();
}
}Common problems
- The user has the permission set but never authenticated. The callout fails because no stored tokens exist for that user. Ask them to complete the Authentication Settings step.
- The code runs as a user who cannot sign in interactively. Scheduled jobs, queueables, and automated process users run as a specific user. That user needs their own stored credentials, which is hard to arrange for system users. For background work, a named principal is usually the better choice.
- A user's tokens expire or are revoked. The user must authenticate again. Plan for this in your error handling.
Exercise
A user has the permission set with access to a per user principal, but their callout fails with an authentication error. What is the most likely cause, and how do you fix it?
Show answer
The user has not authenticated to the external system, so Salesforce has no stored tokens for them. They should open Authentication Settings for External Systems in their personal settings, add an entry for the external credential, and approve access on the external system's login page.