Named credentials in salesforce
When endpoint changes are necessary, named credentials can be updated without requiring a code deployment.
How to set up named / external credentials in salesforce
The first step before configuring named credentials is to create an External Credential. External credentials specify how salesforce authenticate with external system.
The authentication process is handled by the platform and no need to add apex code. Following are the authentication protocols supported by the platform.
To set up new external credentials, navigate to set up -> named credentials and click on external credentials tab then click 'New'
Note
One external credentials can be used in multiple named credentials
Once external credentials are set up, setting up named credentials is quite is easy.
Navigate to set up -> named credentials and click 'New'
And click save
Using named credentials in apex
Once named credentials are configured, they can be used directly in apex. The following is a quick example of how to invoke a callout from apex using named credentials.
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:AWSS3Credentials/my_custom_bucket');
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
Here 'AWSS3Credentials' is our named credentials which points to the base url of our external service and we are accessing a custom bucket ('my_custom_bucket') from this endpoint.
Advantages
- Easy to maintain
Named credentials store the endpoint and authentication for an external service. If the endpoint is changed, the code referencing the credentials doesn't require any modification, only the endpoint needs an update.
- Secure credentials
The credentials for your external authentication are kept secure when using named credentials, eliminating the need to share credentials with developers for implementing API integrations.
By using external credentials, permission sets can be assigned to named credentials, ensuring that only authorized users are permitted to make external callouts from their logins.
- Remote site settings
Named credentials do not require remote site settings. The platform will automatically bypass the endpoints when making a callout.
- Authentication
The platform handles the authentication for named credentials, eliminating the need for custom code to manage authentication for the service.
Conclusion
Named credentials in Salesforce simplify external integrations, offering a centralized and secure way to configure endpoints and authentication. The apex API integration can take advantages such as secure credential storage, permission set assignments, no remote site settings by using named credentials.
What is Decodeforce?
Decodeforce is a dedicated platform aimed at helping Salesforce developers improve their Apex coding skills by solving real-world programming challenges
Solve a number of practice scenarios in DML, SOQL, trigger and many more on Decodeforce.
To know more click the Get started button below