Create Named Credential in Salesforce

When we want to send data from Salesforce to an external system like a Google API or a payment system, we need to provide authentication details (username, password, or access token). If we write these details directly in Apex code, it is not secure and difficult to maintain.

To solve this, we create a Named Credential in Salesforce. It stores the external system URL and authentication details securely. Then, whenever Apex, Flow, or Integration needs to connect with that system, it just uses the Named Credential.

By using Named Credential, you store these details safely in the Salesforce setup. If anything changes, you only need to update the Named Credential; there is no need to edit the Apex code.

In this article, we will learn how to create a named credential in Salesforce, allowing us to securely connect Salesforce with external systems without providing sensitive information in Apex code.

Named Credential in Salesforce

A named credential in Salesforce is a secure way to store the login details, such as URL, username, password, or access token of an external system.

Instead of writing these details in Apex code, we save them in Salesforce setup as a named credential. Then, whenever we need to connect Salesforce with that external system (using Apex, Flow, or integration), we only need to call the named credential.

For example, when a new lead is created, we want to verify if the email provided is real and active before assigning it to the sales team. Instead of using Apex code, I used an HTTP callout in the flow to send the email address to an external email validation API.

Create External Credential in Salesforce

Before creating the named credential, we first need to create the external credential to define the authentication method, such as API key, username-password, or OAuth, that Salesforce will use to connect with the external system.

To create the credentials in Salesforce, click on the Setup. -> Home Page. -> In the Quick Find Box, Search for Named Credentials. You will see an option under Security. -> Click on it.

There, you need to click the “External Credential” option and then click the “New” button to create external credentials.

Named Credentials in Salesforce

When creating an external credential, first enter a Label, such as an email validation credential. The Name field will be automatically populated based on the label you provide.

After that, select the Authentication Protocol, which defines the method Salesforce will use to connect with the external system (for example, api key, username-password, OAuth, or custom).

Then click the Save button.

External Credentials in Salesforce

Then we need to create the principal in the external credential to define who is making the callout to the external system. Here, I provided the parameter name as Email Validation Principle.

A Principal defines who or what can use the External Credential. It links the external credential to a user, permission set, or integration, so Salesforce knows which user or process can access the external system.

External Named Credentials in Salesforce

Create Named Credential in Salesforce

Now we are ready to create a Named Credential to securely connect Salesforce with the external system using the External Credential.

For that, click the Named Credential option and then click the New button.

Create Named Credentials in Salesforce

Here, we also need to provide a Label as per our requirement, and the Name will be automatically populated.

After that, we have to provide a URL. This is the endpoint of the external system to which you want Salesforce to connect. It tells Salesforce where to send the data.

So here, I have provided the external system URL where Salesforce will send the email address to validate.

For authentication, in the External Credential field, select the External Credential you created earlier. This links the Named Credential to the authentication details stored in Salesforce.

Then click the Save button.

Create New Named Credentials in Salesforce

Create a Permission Set for the User in Salesforce

Initially, the user doesn’t have external credential principal access, so we need to create the permission set and assign it to the user.

A Permission Set in Salesforce is used to grant access to users, allowing them to use External Credentials or Named Credentials.

When you create an External Credential, you define how Salesforce will connect to an external system. But Salesforce needs to know which users or processes are allowed to use it.

Assign Permission Set to User in Salesforce

In this way, we can create named credentials in Salesforce and use them.

Conclusion

I hope you have got an idea of how to create a Named Credential in Salesforce. I have also explained how it works with an External Credential and Permission Set.

By using Named Credential, you store these details safely in the Salesforce setup. If anything changes, you only need to update the Named Credential; there is no need to edit the Apex code.

You may like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.