Integration Using Named Credentials | Salesforce Developer Guide
The Following are the Benefits of Using Named Credentials:
- A Named credential specifies the end URL of the callout and its verification parameters required in a single description.
- You can bypass the remote site settings, which are required for receiving calls from external sites, on the site specified in the named information.
- Using Named credential, we can make calls to an external system without providing a username or Password to the code.
- By separating the last point URL and confirmation from the callout description, the details make the callouts easier to store. For example, if the last point URL changes, you only update the specified information. All callouts referring to fictional verification automatically continue to work.
- Supports two types of current authentication protocols: Basic Authentication (Password Authentication) or OAuth
To refer to a Named credential from a callout description, use the Named credential URL. Example: callout: My_Named_Credential / some_path.
The following are the steps to connect to an external system using "Named Credential":
- Create a Connected Application
- Create an Authorization Provider
- Explain Fictional Verification
- Use Apex code
Don't forget to check out: Salesforce Forms Integration Without Code
Create a Linked App:
- Go to Setup and search Connected Apps in quick find box and click on New
- Provide all necessary information
- In the "Callback URL" enter the Salesforce Temporary URL. We'll come back to this step later to provide a back-up drive URL
- To use OAuth, Select the “Enable OAuth settings” box.
- Choose a width
- Click on Save
Create an Authorization Provider:
- Navigate to “Setup | Manage | Security Controls | Auth. Providers | Create New”.
- Select "Salesforce" as the Provider Type
- Provide “Consumer Key” and “Consumer Secret”.
- In “Default Scope” enter a value.
- Click on Save
Once you have saved it, it will give you a set of URLs in the "Salesforce Configuration" section on the same page. Copy the "Callback URL" and edit the Connected Application that we created in the previous step and set this URL as the backup URL.
Define Named Credentials:
- Navigate to “Setup | Manage | Security Controls | Fiction Details | New Named Credentials “.
- Give a name (label)
- In the URL, provide the Salesforce example URL where we want to link
- Choose "Named Principal" as the Properties type
- In our example choose " Authentication Protocol " as OAuth 2.0
- Select " Auth Provider "
- In scope, fill value as "refresh_token full"
- Make sure “Start Authentication Flow on Save” checkbox is checked (this is necessary)
- Click Save
After clicking "Save" a new page will open to authorize Salesforce Org using the OAuth2 linked application. Sign in using the details of the Salesforce org that you want to connect to. If authentication is successful, you can see the message as "Authorized as".
Check out another amazing blog by Rajesh here: Invoke Apex Actions from Flow | Salesforce Flow Guide
Use Apex Code to Connect:
Use lines like the one below in your Apex code.
HttpRequest req = new HttpRequest (); req.setEndpoint (‘callout: My_Named_Credentials / services / some_path’); req.setMethod (‘GET’); Http http = new Http (); HTTPResponse resp = http.send (req);