In Salesforce, Named Credentials provide a secure and easy way to define authentication settings for external services. They abstract authentication details such as OAuth tokens, passwords, and endpoints from your code, improving security and maintainability. To access Named Credentials data in Apex, you typically use them in conjunction with Apex callouts. Here’s how you can do it:
Using Named Credentials in Apex Callouts
Define a Named Credential:
First, you need to define a Named Credential in Salesforce. Go to Setup → Named Credentials and create a new Named Credential. Provide the necessary details such as the URL, username, password, and authentication protocol (e.g., Password Authentication or OAuth).
Make an Apex Callout:
After setting up the Named Credential, you can use it in your Apex code to make callouts to the external service.
HttpRequest req = new HttpRequest();req.setEndpoint('callout:Named_Credential_Name/path/to/resource');req.setMethod('GET');// Set any other necessary headers, body, etc.Http http = new Http();HttpResponse res = http.send(req);// Process the responseif (res.getStatusCode() == 200) { // Successful response handling String responseBody = res.getBody(); // Process the responseBody as needed} else { // Error handling System.debug('Error: ' + res.getStatusCode() + ' ' + res.getStatus());}
Access Named Credential Details in Apex:
Named Credentials are accessed in Apex through the callout: schema. You specify the Named Credential name followed by the path to the resource.
req.setEndpoint('callout:Named_Credential_Name/path/to/resource');
Handling Authentication
Password Authentication: If your Named Credential uses password authentication, Salesforce handles the username and password securely. You don't need to include the username and password in your Apex code explicitly.
OAuth Authentication: If your Named Credential uses OAuth, Salesforce manages the OAuth token lifecycle. You can specify scopes and other OAuth parameters in the Named Credential configuration.
Considerations
Security: Named Credentials help secure sensitive information by abstracting it from your code.
Governor Limits: Apex callouts are subject to Salesforce governor limits. Ensure you handle responses efficiently and consider using asynchronous callouts for long-running operations.
Example Use Case
Suppose you have a Named Credential named My_API_Credential configured with a username-password authentication type and an endpoint URL https://api.example.com.
HttpRequest req = new HttpRequest();req.setEndpoint('callout:My_API_Credential/v1/resource');req.setMethod('GET');Http http = new Http();HttpResponse res = http.send(req);if (res.getStatusCode() == 200) { String responseBody = res.getBody(); // Process response} else { System.debug('Error: ' + res.getStatusCode() + ' ' + res.getStatus());}
In this example, My_API_Credential is used to authenticate and access https://api.example.com/v1/resource securely without exposing authentication details in the Apex code.
By leveraging Named Credentials in Apex, you ensure secure and efficient integration with external services while adhering to Salesforce security best practices.