Sub topic 6
Principals: named versus per user
This chapter assumes you can read basic Apex and can navigate Setup and permission sets. It also assumes you know the earlier chapters' split: the Named Credential holds the endpoint, and the External Credential holds the authentication definition. Here you decide whose identity Salesforce presents to the external service.
What a principal is
A principal is the identity that an External Credential uses when it calls the external system. The External Credential says how to authenticate, for example OAuth 2.0 or a custom API key. The principal says whose credentials fill in that definition.
Every External Credential has at least one principal. There are two types:
- A named principal is one shared identity. Every user who is allowed to use it makes callouts as that same identity.
- A per user principal gives each user their own identity. Every user authenticates to the external system separately, and Salesforce stores a separate set of credentials for each of them.
You do not choose the type per callout. You choose it when you create the principal, and the Apex code is the same either way. Your callout still starts with callout: and the Named Credential name. Salesforce decides which stored credentials to attach when the request is sent.
Named principal
With a named principal, an administrator supplies the credentials once, in Setup. For an API key, the administrator enters the key as an authentication parameter on the principal. For OAuth 2.0, the administrator runs the Authenticate action on the principal and signs in to the external system once.
Use a named principal when:
- The external system has one service account or one API key for your whole integration.
- Users should not need accounts on the external system.
- The callout runs in the background, for example in a Queueable or a scheduled job, and no person is present to sign in.
The trade-offs are these:
- The external system sees one identity for all traffic. Its audit log cannot tell your users apart.
- Anyone who has the principal can make every call the shared identity can make. The external account's permissions are your only upper limit, so keep that account narrow.
- If the shared credential expires or is revoked, every user loses access at once.
Per user principal
With a per user principal, the administrator does not enter credentials. Each user connects their own account, usually through an OAuth 2.0 sign-in. The user starts this from their personal settings under Authentication Settings for External Systems. Salesforce then keeps that user's tokens separate from everyone else's.
Use a per user principal when:
- The external system must enforce each person's own permissions, such as a document store where users see only their own files.
- The external system's audit trail must record the real person.
- Company policy forbids sharing one account across people.
The trade-offs are these:
- Every user must complete the sign-in before their first callout. An administrator cannot do it for them.
- A user who has not authenticated gets a failure at callout time. Your UI and error handling should say what to do.
- Background work is awkward. A job has no interactive user, so it can use only credentials that belong to the user the job runs as. Be careful with triggers fired by platform events or other automation that runs as a system user. That user cannot sign in on their own.
- Support load rises, because tokens expire or are revoked person by person.
Support for per user principals depends on the authentication protocol. OAuth 2.0 is the standard case. Check the current Salesforce documentation before you plan per user access for another protocol.
Comparing the two
| Question | Named principal | Per user principal |
|---|---|---|
| Who enters the credentials? | An administrator, once | Each user, individually |
| Identity seen by the external system | One shared identity | The individual user |
| Works for background jobs | Yes | Only for the user the job runs as |
| Setup effort | Low | Higher, and ongoing user support |
| Blast radius if a credential leaks | All users of the principal | One user |
| External audit trail | One account | Per person |
Choosing between them
The questions below capture the usual decision path.
Splitting is a normal pattern. One External Credential can hold several principals. You can also create two External Credentials, one with a named principal for automation and one with a per user principal for interactive features, each behind its own Named Credential. Your Apex then picks the Named Credential that matches the situation.
Principals and permission sets
A principal does nothing until someone is allowed to use it. Access is granted through a permission set, under External Credential Principal Access. This applies to both types. A user with no permission set that grants the principal cannot use the credential, even if they have Apex access to the class that makes the callout.
For a per user principal, permission set access is only the first step. The user must also authenticate. Both conditions have to hold.
The outline below shows one External Credential with both types of principal. The names are examples.
External Credential: Docs_EC
Authentication Protocol: OAuth 2.0
Principal: Docs_Service (Named Principal)
Sequence Number: 1
Authenticated by an administrator in Setup
Principal: Docs_User (Per User Principal)
Sequence Number: 2
Authenticated by each user in their own settings
Permission Set: Docs_Automation
External Credential Principal Access: Docs_EC - Docs_Service
Permission Set: Docs_Interactive
External Credential Principal Access: Docs_EC - Docs_UserEach principal has a sequence number. If a user holds access to more than one principal on the same External Credential, Salesforce uses the sequence number to decide which one applies. Avoid overlapping grants unless you have tested which principal wins in your org. Overlap makes it hard to reason about which identity a call used.
Seeing the difference in Apex
The Apex does not change between principal types. The only visible difference appears when things go wrong. A per user principal adds a failure that a named principal rarely has: the current user has not authenticated yet. The method below wraps the callout and gives the caller a clearer message.
public with sharing class DocsService {
public static String listFiles() {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Docs_API/files');
req.setMethod('GET');
req.setHeader('Accept', 'application/json');
try {
HttpResponse res = new Http().send(req);
if (res.getStatusCode() != 200) {
throw new CalloutException('Docs API returned status ' + res.getStatusCode());
}
return res.getBody();
} catch (CalloutException e) {
// Credential problems surface here as well as network and status errors.
throw new CalloutException('Could not reach Docs API. Check that you have '
+ 'access to the credential and have connected your account. Details: '
+ e.getMessage());
}
}
}The catch block wraps the original message instead of hiding it, so the underlying cause is still visible in logs.
Common mistakes
- Granting the permission set but not authenticating. For a named principal with OAuth, the principal shows as not authenticated until an administrator runs Authenticate. For a per user principal, each user has to authenticate too.
- Using a per user principal for scheduled or event-driven code. The job runs with an identity that has no stored credentials, so the callout fails.
- Using a named principal with a high-privilege account. Every permitted user inherits that power. Create a dedicated service account with only the access the integration needs.
- Forgetting that access follows the running user. Test as a user who has only the intended permission sets, not as a system administrator.
- Overlapping principal grants. A user in two permission sets that grant different principals may get an identity you did not intend.
Exercise
A Queueable job sends nightly sync requests to an external API. The API accepts a single service account, and the business does not need to know which Salesforce user triggered the sync. Which principal type fits, and why would a per user principal cause trouble?
Show answer
A named principal fits. The administrator authenticates the service account once, and the job can use it without any person signing in. A per user principal needs the running user to have authenticated personally, and a background job has no interactive user who can do that, so the callout would fail.
Exercise
An External Credential has a per user principal, and a user has the permission set that grants access to it. Their callout still fails with a credential error. What is the most likely missing step?
Show answer
The user has not authenticated to the external system. Permission set access only allows them to use the principal. For a per user principal, they must also connect their own account in their authentication settings for external systems.