How to access named credentials salesforce data in apex?

Is there a way of accessing the data (exact fields) stored in Spring 15' Named Credentials from apex? For example the username, password and endpoint link fields? And I don't mean for the callout, but for other purposes.

Answered by Clare Matthews

You can use Named Credentials salesforce to create an Endpoint URL with necessary parameters. For example, if this is is your URL:

https://www.someendpointurl.com?param1=named¶m2=credentials
In Apex, You can access this URL like this:
request.setEndpoint('callout:EndPointURL');
Where, EndPointURL is the Name of the Named Credentials.
Benefits of using Named Credentials:
Authentication is done by Salesforce and you need not to worry about that.
No need to create a Remote Site Setting if using a Named Credential.
Callout is easier to maintain. No hard coding involved.
If you are also using Sandboxes for callout, just create the Named Credentials with the same name and save different URLs.
Hope this helps..!!
But actually accessing the data stored in the Named Credentials fields (e.g. as String) is not possible.

Your Answer

Answer (1)

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.








4 Months

Interviews

Parent Categories