Sub topic 4
Authenticating to the external system
Every Salesforce Connect request is a live callout from Salesforce to another system. That system needs to know who is calling and whether to allow it. This chapter covers how to configure that authentication with named credentials and external credentials, and what goes wrong when it is set up poorly.
Prerequisites
This chapter assumes you know the following:
- Basic Salesforce Setup navigation and permission sets.
- What a REST or OData endpoint is, and what an HTTP header is.
- The idea of an external data source from the previous chapters. You will create one in the next chapter, so here we only prepare what it points to.
Why authentication is its own step
An external data source stores a URL and a choice of adapter. It should not store a password or token in plain view. Salesforce keeps connection details and secrets in separate records so that:
- Administrators can change a secret without editing the data source.
- Secrets are not exposed to people who can only view the data source.
- The same endpoint can be reused by Apex callouts, flows, and external objects.
The records that do this are named credentials and external credentials.
The two building blocks
A named credential says where to call. It holds the base URL of the external system and points to an external credential for the "how to log in" part.
An external credential says how to authenticate. It holds the authentication protocol, one or more principals, and the secrets or tokens those principals use.
A principal is an identity used for the callout. A permission set grants users access to a principal. Only users with that access can make callouts that use it.
| Piece | Answers the question | Typical contents |
|---|---|---|
| Named credential | Where do I call? | Base URL, link to an external credential, callout options |
| External credential | How do I prove who I am? | Protocol, principals, parameters such as username, password, or token |
| Permission set | Who may use this identity? | Principal access setting |
Salesforce also still supports legacy named credentials, which combine the URL and the login details in one record. New setups should use the newer pair, so this chapter uses it. If you open an older org, you may see legacy records. They work, but you cannot assign them to principals with permission sets.
How the pieces connect
The data source refers to the named credential. The named credential refers to the external credential. The external credential has a principal, and a permission set controls who may use it.
Choosing an authentication protocol
The external credential supports several protocols. Pick the one the external system requires.
| Protocol | When to use it | Notes |
|---|---|---|
| No authentication | Public endpoints, demos, test services | Anyone can read the data. Do not use for private data. |
| Basic | The system accepts a username and password | Simple. The password is stored as a secret. |
| Custom | The system uses an API key or header token | You define headers or parameters yourself. |
| OAuth 2.0 | The system supports OAuth, such as another Salesforce org or a cloud API | Tokens expire and are refreshed. Requires an authentication provider or similar setup. |
| AWS Signature Version 4 | The endpoint is an AWS service that requires signed requests | Uses an access key and secret. |
The adapter you chose earlier limits your options. A cross-org adapter talks to another Salesforce org, so OAuth 2.0 is the normal choice. An OData adapter works with whatever protocol the OData service uses. Check the service documentation before you start.
Naming the principal
Each external credential has at least one principal. There are two common kinds.
- Named principal. One identity is shared by every user who has access. All callouts appear to the external system as the same account. Use this when the external system has a single integration account.
- Per-user principal. Each user signs in to the external system separately. The external system sees each person's identity. Use this when the external system enforces its own per-user data access.
This choice matters more than any other in this chapter. With a named principal, the external system cannot tell users apart, so any filtering by user has to be done in Salesforce. With a per-user principal, the external system can enforce its own rules, but each user must authenticate once before they can see data.
Worked example: Basic authentication to a product service
Suppose your company has an inventory service at https://inventory.example.com/odata. It uses Basic authentication with a dedicated integration account. You plan to expose its products as the external object Product__x.
Step 1: Create the external credential
- In Setup, open Named Credentials, then the External Credentials tab, and select New.
- Set the label to
Inventory Service Loginand the authentication protocol to Basic Authentication. - Save the record.
- In the Principals section, add a principal. Set the name to
IntegrationAccount, the sequence number to 1, and the identity type to Named Principal. - Edit the principal's authentication parameters. Enter the integration account's username and password.
The password is encrypted at rest. You cannot read it back from the screen after saving, so keep it in your team's password manager as well.
Step 2: Create the named credential
- On the Named Credentials tab, select New.
- Set the label to
Inventory Serviceand the URL tohttps://inventory.example.com/odata. - Choose
Inventory Service Loginas the external credential. - Save.
Use only the base URL here. The adapter adds paths such as /Products when it reads data.
Step 3: Grant access to the principal
- Open or create a permission set, for example
Inventory Data Access. - Under External Credential Principal Access, add
Inventory Service Login - IntegrationAccount. - Assign the permission set to the users who should see the external data.
Without this step, the callout fails even though the credential is configured correctly. This is the most common mistake with the newer model.
Step 4: Point the data source at the named credential
In the next chapter you will create the external data source. For now, note the setting you will use: set the Identity Type and the Named Credential so the data source uses Inventory Service. The URL field is filled in from the named credential, so you do not type it again.
Checking the setup
You can test the same named credential from Apex before you create any external object. This confirms that the URL, the login, and the permission set all work. The callout: prefix tells Apex to use a named credential, and the named credential supplies the host and the authentication.
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Inventory_Service/Products?$top=1');
req.setMethod('GET');
req.setHeader('Accept', 'application/json');
HttpResponse res = new Http().send(req);
System.debug(res.getStatusCode());
System.debug(res.getBody());Run this as a user who has the permission set. A status of 200 means the pieces work together. The name after callout: is the named credential's developer name, which Setup shows with underscores instead of spaces.
Do not paste the credential's username or password into this code or into any custom setting. The point of the named credential is that code never sees the secret.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| 401 Unauthorized | Wrong or expired username, password, or token | Update the principal's parameters. For OAuth, reauthenticate. |
| 403 Forbidden | The external account lacks rights to the resource | Fix permissions in the external system, not in Salesforce. |
| Error saying the user is not authorized for the credential | The user's permission sets do not include the principal | Add principal access to a permission set and assign it. |
| Callout fails with an endpoint or host error | Wrong base URL, or a trailing path duplicated by the adapter | Use only the base service URL in the named credential. |
| Per-user setup works for you but not a colleague | The colleague has not authenticated to the external system | Have them sign in under **Authentication Settings for External Systems** in their personal settings. |
| Works in Apex, fails for the external object | The data source points at a different or legacy credential | Check the data source's named credential and identity type. |
Design guidance
- Use a dedicated integration account. Give it only the rights the data source needs. If it only has to read products, give it read access only. This limits damage if the secret leaks.
- Prefer HTTPS. Basic credentials sent over plain HTTP can be read in transit. Salesforce expects HTTPS for most protocols.
- Choose the principal type early. Changing from a named principal to per-user later changes what users see and requires every user to sign in.
- Plan for secret rotation. Passwords and client secrets expire or change. Know who updates the principal and where the new value is stored.
- Separate environments. Use one named credential per environment, such as sandbox and production, with the same developer name where possible. Then metadata deploys do not need to edit the data source.
Summary
A named credential holds the URL. An external credential holds the protocol and the secrets. A principal inside the external credential is the identity, and a permission set decides who may use it. The external data source refers to the named credential, so authentication is configured once and reused. Choose the protocol the external system requires, decide between a shared and a per-user principal, and test the setup before you create external objects.
Exercise
A colleague configured a named credential and an external credential with a named principal. A test callout works for the admin who built it, but other users get an error saying they are not authorized for the credential. What is the most likely cause, and how do you fix it?
Show answer
The principal is not granted to those users. Add the external credential principal to a permission set under External Credential Principal Access, then assign that permission set to the affected users. The admin likely had access through a permission set or profile that the other users do not have.
Exercise
Your external system enforces its own record-level access, so a sales rep should only see the customers they own there. Should you use a named principal or a per-user principal, and what must each user do before they see data?
Show answer
Use a per-user principal so the external system sees each person's identity and applies its own rules. Each user must authenticate to the external system once, under Authentication Settings for External Systems in their personal settings, before their callouts succeed.
Exercise
Why should the named credential's URL contain only the base service URL, for example https://inventory.example.com/odata, and not a full path such as https://inventory.example.com/odata/Products?
Show answer
The adapter adds the resource paths and query options itself when it reads data. A full path in the named credential would be duplicated or produce invalid URLs, and it would stop the credential from being reused for other resources on the same service.