Sub topic 4
Authenticating to the external system
This chapter assumes you know how Salesforce Setup and permission sets work, and that you have a general idea of what an external data source is. You will learn how Salesforce proves its identity to the external system, and what to configure before you create the external data source in the next chapter.
Why authentication comes first
Every query against an external object becomes a live callout to the external system. That system must accept the request, so Salesforce needs a URL to call and credentials to send. Storing those credentials in the external data source itself is not recommended. Use a named credential instead.
A named credential is a Setup record that holds the endpoint URL of the external system. In the current model it also points to an external credential, which holds the authentication details. Salesforce adds the credentials to each callout, and nobody has to paste secrets into code or settings.
The two pieces
| Piece | What it defines |
|---|---|
| Named credential | The base URL of the external system and the external credential it uses |
| External credential | The authentication protocol, one or more principals, and the permission sets that may use them |
The external credential supports protocols such as OAuth 2.0, basic authentication, JWT, and AWS Signature Version 4. Your choice depends on what the external system accepts.
A principal says whose identity is sent to the external system:
- A named principal is one shared identity. Every Salesforce user who reaches the external system does so as that identity.
- A per user principal gives each Salesforce user their own identity. Each user authenticates once, and the external system can apply its own row-level security.
Setting it up
- Create the external credential. Choose the authentication protocol and add a principal. For OAuth, you also need an authentication provider or the OAuth settings of the external system.
- Create the named credential. Enter the endpoint URL and select the external credential.
- Grant access. Add the external credential principal to a permission set under External Credential Principal Access, then assign that permission set to the users who will query the data.
- For a per user principal, each user must also authenticate, from their own settings page for external system authentication.
Later, when you create the external data source, you select this named credential, and the data source uses it for every callout.
Matching authentication to the adapter
The adapter you chose in the previous chapter affects what you configure:
- OData 2.0 and OData 4.0 adapters call the external system's endpoint, which you supply through the named credential.
- The cross-org adapter calls another Salesforce org, so the named credential normally uses OAuth 2.0 against that org.
- A custom Apex adapter declares its supported authentication methods itself, using
DataSource.AuthenticationCapability, and can read the named credential's settings through its connection parameters.
Common errors
- A 401 or 403 response usually means the credentials are wrong, expired, or lack rights in the external system. Check them there first.
- A user who gets an authentication error while an admin does not often lacks the permission set with principal access, or has not yet authenticated to a per user principal.
- An OAuth flow that fails at the redirect usually points to a callback URL that does not match what the external system or authentication provider has registered.
- A callout that fails before it reaches the server often has a wrong base URL. Check the protocol, host, and path.
Exercise
Your team wants each Salesforce user to see only the external orders that the external system allows that user to see. Which principal type should the external credential use, and what must each user do?
Show answer
Use a per user principal, so the external system receives each user's own identity and can apply its own access rules. Each user must be assigned a permission set that grants access to that principal and must authenticate to the external system once from their own settings.