Sub topic 7
Granting access with permission sets
This chapter assumes you can write basic Apex classes, know HTTP methods, headers and status codes, can find your way around Setup, and know the basic idea of API keys, Basic authentication and OAuth 2.0. It also assumes you have met the Named Credential and the External Credential from the earlier chapters.
By the end you will be able to grant a user access to an external credential principal, explain why the grant is needed, and predict which callouts will fail without it.
Why a Named Credential alone is not enough
A Named Credential says where a service lives. An External Credential says how to authenticate. Neither says who may use that authentication. The permission set closes that gap.
When Apex runs a callout to callout:Weather_API/..., Salesforce checks the running user. It looks at the External Credential linked to the Named Credential and asks whether that user has been granted access to a principal of that External Credential. If not, the callout fails before any request leaves Salesforce.
This makes the permission set the gate in front of the secret. The Apex code never sees the API key, but it can only trigger its use when the running user holds the right permission.
The setting: External Credential Principal Access
The grant lives in a permission set (or a profile, though permission sets are the better choice) under External Credential Principal Access. Each entry names one principal on one External Credential, written as ExternalCredentialName - PrincipalName.
In the running example, the permission set Weather_Callout_Access contains this entry:
Permission Set: Weather_Callout_Access
External Credential Principal Access: Weather_EC - Weather_PrincipalA user who is assigned this permission set can make callouts that use Weather_EC through Weather_Principal. A user without it cannot.
Steps to grant access
- In Setup, open Permission Sets and create one, or open an existing one. Use a name that describes the capability, such as
Weather_Callout_Access. - Open the External Credential Principal Access section.
- Click Edit.
- Move the principal, for example
Weather_EC - Weather_Principal, from Available to Enabled. - Save.
- Assign the permission set to each user, or to a permission set group, that needs to make the callout.
Only enabled principals count. Having the permission set assigned is not enough if the principal is still in the Available list.
What "the running user" means
The check applies to the user whose context executes the callout. This is where several surprises come from.
| Where the callout runs | Which user is checked |
|---|---|
| Apex called from a Lightning component or Visualforce page | The logged-in user |
| Anonymous Apex run from the Developer Console | The user who runs it |
| A trigger fired by a user's DML | The user who caused the DML |
| Scheduled Apex | The user who scheduled the job |
| Queueable or Batch Apex | The user who enqueued the job |
| Automated Process user or other system contexts | That user, which usually has no assignment |
Two things follow from this table.
- A developer who has the permission set can test a callout in Anonymous Apex, and it works. A different user with a narrower role then sees a failure from the same code. Always test with a user who has only the access you intend to grant.
- Asynchronous work such as scheduled jobs runs as a specific user. If that user loses the permission set, the job starts failing even though the code did not change.
The class WeatherService uses with sharing. This does not affect the credential check. Sharing rules control record visibility. Principal access is a separate permission and is evaluated no matter how the class is declared.
How the grant interacts with the principal type
The kind of principal you granted determines what else must be true for the callout to work.
| Principal type | What the permission set grants | Extra requirement |
|---|---|---|
| Named Principal | Use of one shared identity, with one stored set of credentials | None. The secret is already stored on the principal. |
| Per User Principal | Permission to authenticate and then use your own identity | Each user must complete authentication themselves before callouts succeed. |
With a named principal, assigning the permission set is the whole job. Everyone who holds it acts as the same external identity. The external system cannot tell them apart.
With a per user principal, the permission set only lets a user begin. The user must then authenticate to the external system, for example through an OAuth consent screen. A later chapter covers that flow. Until a user does so, their callouts fail even though the permission set is in place.
The flow below shows the decision Salesforce makes on each callout.
Designing permission sets
Treat the permission set as a statement of capability. A few rules keep the design clean.
- One permission set per integration capability. Name it after what the user can do, not who they are.
Weather_Callout_Accessis better thanSales_Team_Extras. - Grant the narrowest principal. If an External Credential has a read-only principal and a read-write principal, give most users only the read-only one.
- Use permission set groups for roles. Bundle several capability sets into a group and assign the group. This keeps assignments readable.
- Do not put principal access in a broad profile. Everyone with that profile gains the access, and removing it later is harder.
- Review assignments. Anyone with access can cause the stored secret to be used. Treat the assignment list as an access list for that secret.
Granting access to a user is not the same as exposing the secret. The user cannot read the API key from Setup unless they also hold administrative permissions. They can only cause Apex to use it.
Recognising a missing grant
A missing principal grant produces an error when the callout is attempted. The exact text depends on the context, but it generally says the user lacks access to the external credential, or that the credential cannot be used by the running user. The request never reaches the external service, so the external system's logs show nothing.
That last fact is a useful diagnostic. Compare the two failure types:
| Symptom | Likely cause |
|---|---|
| Error thrown from `send()` and nothing in the external system's logs | Missing principal access, or an unauthenticated per user principal |
| `HttpResponse` returned with status 401 or 403 | The request was sent, and the external service rejected the credentials |
| `HttpResponse` returned with status 404 | The path or base URL is wrong |
A quick checklist when a callout fails with an access error:
- Confirm which user is running the code. Use
UserInfo.getUserId()in a debug statement. - Open that user's assigned permission sets and permission set groups.
- Check that one of them lists the principal under External Credential Principal Access, and that it is enabled.
- If the principal is per user, check that this user has authenticated.
- Confirm the Named Credential points to the External Credential you expect.
Tests and permission sets
Apex tests that use Test.setMock do not make real callouts. This means a test passes even when no principal access exists, so the test cannot prove that your permission set is correct. Verify the grant manually, with a real user, in a sandbox.
Worked example
Suppose a support agent named Ana opens a case page that calls WeatherService.getForecast('Oslo'). The setup is the one from the earlier chapters.
- Ana's Apex runs the request to
callout:Weather_API/forecast?city=Oslo. - Salesforce finds that
Weather_APIusesWeather_EC. - Salesforce checks Ana's permission sets. None lists
Weather_EC - Weather_Principal. - The callout throws an error. Nothing is sent to
api.example.com. - An administrator adds Ana to a permission set group that contains
Weather_Callout_Access. - Ana reloads the page. The principal is named, so no further steps are needed. Salesforce adds the
X-Api-Keyheader and sends the request. - The service returns status 200, and
getForecastreturns the body.
If the principal had been per user, step 6 would also require Ana to authenticate first.
Exercise
A scheduled Apex job calls an API through a Named Credential. It worked for weeks, then began failing with an access error, though nobody changed the Apex class or the Named Credential. The external service's logs show no incoming requests. What is the most likely cause, and where do you check first?
Show answer
The job runs as the user who scheduled it, and that user most likely lost the permission set (or permission set group) that grants External Credential Principal Access. The request never left Salesforce, which fits the empty external logs. Check the scheduling user's assigned permission sets and confirm the principal is still enabled under External Credential Principal Access. If the principal is per user, also check that the user's authentication has not expired or been removed.
Exercise
A developer's callout works in Anonymous Apex, and the Apex test passes. A sales user running the same feature gets an access error. Give two reasons this can happen without any code defect.
Show answer
First, the developer's own permission sets include the principal access, while the sales user's do not, so the credential check fails only for the sales user. Second, the test uses HttpCalloutMock, which replaces the callout entirely and skips the credential check, so a passing test says nothing about user access. A third possibility, if the principal is per user, is that the sales user has not yet authenticated.