Named Credentials – Setup | Apex Callout

Are you concerned about exposing the URL to an external system for integration and wish not to do so? Are you worried about sharing the Username / Password to connect to an external system and how to hide it?  Then Named Credentials is the way to go and you are at right place to learn and know how to implement it!


Check how to use Named Credentials in Code for callouts


Setup Named Credentials in Salesforce

Go to setup –> Named Credentials.  Click New Named Credential button.

named credentials code salesforce sfdcfanboy

Enter the Label, Name and URL

new named credential salesforce sfdcfanboy

Authentication Protocol as Password Authentication

Choose Identity Type as Named Principal and then choose Password Authentication for the protocol. Enter the username and password which you will pass.

edit named credential salesforce sfdcfanboy

When Authentication Protocol is OAuth 2.0

Named Credential can also be authenticated through OAuth method. Once we select OAuth 2.0 in Authentication Protocol. It shows us to Select Authentication Provider (configured in Salesforce).  Before we can use this Named Credential to make the Callout, we have to authorize the details by Checking “Start Authentication flow on Save”. It will authenticate the details when we save the Named Credentials Definition. Put the scope as “refresh_token full”.

named credential salesforce oauth 2.0 sfdcfanboy

Once the Named Credentials are saved with “Authentication Status” Authenticated as <Username>, it is ready to use to make Callout. Remember Named Credentials won’t work if Status is Pending.

Use Named Credentials in code for callouts


Check how to use Named Credentials in Code for callouts


 

 

Advertisement

2 Replies to “Named Credentials – Setup | Apex Callout”

  1. I would love to automate the process to create the Named Credential record for a user. Can you set the flag ‘start authentication flow on save’ automatically using apex?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: