Sub topic 7
Granting access with permission sets
This chapter assumes you can create permission sets in Setup and that you know the difference between a Named Credential and an External Credential. It also assumes you have read about principals. Here you will see how a permission set connects a user to an External Credential principal. Without that connection, a callout fails even when the Named Credential is configured correctly.
Why a permission set is required
A Named Credential and its External Credential describe where a service lives and how to authenticate. Neither says who may use them. Salesforce answers that question with External Credential Principal Access, a setting inside a permission set or profile.
When Apex runs a callout, Salesforce checks whether the running user has access to the principal that the External Credential uses. If the user does not, the callout stops before any request leaves Salesforce.
Use permission sets rather than profiles. Permission sets are easier to audit and to assign to only the users who need the callout.
Configure the access
Follow these steps for the Weather example from this tutorial.
- In Setup, open Permission Sets and create one named
Weather_Callout_Access. - Open the permission set and select External Credential Principal Access.
- Choose Edit, then move
Weather_EC - Weather_Principalto the Enabled list. - Save the permission set.
- Assign the permission set to each user who runs the callout.
The outline below shows the result.
Permission Set: Weather_Callout_Access
External Credential Principal Access: Weather_EC - Weather_PrincipalWho counts as the running user
The permission is checked against the user whose context runs the Apex code. This depends on how the code is started.
| How the callout starts | User checked |
|---|---|
| A user clicks a button or opens a page that calls Apex | The signed-in user |
| A trigger fires from a user's record change | The user who made the change |
| A scheduled or Queueable job | The user who scheduled or enqueued it |
| An Automated Process or Platform Event trigger | The Automated Process user or the configured trigger user |
Scheduled and background jobs are the most common source of surprises. The user who sets up the job needs access, and so does any user who later runs it.
Named principals and per user principals
The permission set matters for both principal types, but it does different work in each.
- Named principal: the permission set lets a user share one stored identity. Everyone with access calls the service as the same account.
- Per user principal: the permission set lets a user start authenticating. Each user must also sign in to the external system, and their own token is stored.
Common mistakes
- Assigning the permission set to admins only, then finding that other users fail in production.
- Enabling the Named Credential in a permission set but forgetting the principal. For the new model, the principal is the setting that matters.
- Testing as a system administrator, who may have wider access than the intended users.
Exercise
A nightly Schedulable class calls callout:Weather_API/forecast. It was scheduled by an administrator, and the permission set was assigned only to the sales team. The job fails. Which user needs the principal access, and how do you fix it?
Show answer
The job runs in the context of the user who scheduled it, the administrator. Add Weather_EC - Weather_Principal to a permission set and assign that permission set to the administrator, or reschedule the job as a user who already has it.