What are name credentials, and how do I use them in Salesforce

| By Ayush Sharma

In Salesforce, Named Credentials is a secure way to store and manage external API credentials, such as usernames, passwords, tokens, or endpoints, used for authenticating and accessing external systems or services.

Named Credentials act as a wrapper around these credentials, providing a single point of configuration and management. They are especially useful when integrating with external systems or services that require authentication.

Here are some key aspects of Named Credentials in Salesforce:

  1. Secure Storage: Named Credentials securely store authentication information. They use Salesforce’s encrypted storage, ensuring that sensitive credentials are not exposed.
  2. Authentication Protocols: Named Credentials support various authentication protocols, including Basic Authentication, OAuth 2.0, and Salesforce Identity.
  3. Remote Site Settings: When using Named Credentials, you can define the endpoint URL of the external system or service as a Remote Site. This establishes a trust relationship between Salesforce and the external system, allowing secure communication.
  4. Integration Considerations: Named Credentials simplify the integration process by handling the authentication details, allowing developers to focus on the core logic of integrating with external systems. They can be used in Apex code, Visualforce pages, Lightning components, or even in declarative features like Salesforce Connect or Outbound Messaging.
  5. Testing and Debugging: Salesforce provides tools to test and validate Named Credentials. You can validate the connection, check authentication, and troubleshoot any issues that may arise during integration.

By using Named Credentials, developers can avoid hard-coding sensitive credentials in their code, making integrations more secure, manageable, and scalable.

To use Named Credentials in Salesforce, follow these steps:

  1. Create a Named Credential:

    • Go to “Setup” in Salesforce.
    • In the Quick Find box, search for “Named Credentials” and select “Named Credentials.”
    • Click on “New Named Credential.”
    • Provide a unique Label and Name for the credential.
    • Choose the appropriate Named Credential Type based on the authentication method required by the external system.
    • Fill in the necessary details like URL, Username, Password, Token, etc., based on the chosen Named Credential Type.
      Save the Named Credential.
  2. Use Named Credentials in Apex:

    • In your Apex code, you can refer to the Named Credential using its Developer Name. For example, if the Developer Name of the Named Credential is “MyNamedCredential”, you can access it using MyNamedCredential.
    • You can use the Named Credential in methods like HttpRequest, Http, SOAPCallout, etc., to make authenticated API calls to the external system. For example :
      HttpRequest req = new HttpRequest();
      // Add additional request parameters, headers, etc.
      Http http = new Http();
      HttpResponse res = http.send(req);
      // Process the response
  3. Use Named Credentials in Declarative Features:
    Named Credentials can also be used in declarative features like Salesforce Connect or Outbound Messaging. When configuring these features, you can choose the Named Credential as the endpoint or authentication method.
  4. Test and Validate Named Credentials:

    • Salesforce provides tools to validate the Named Credential configuration.
    • In the Named Credential detail page, click on the “Test Endpoint” button to validate the connection to the external system.
    • You can also use Apex code to test the Named Credential functionality and handle any errors or exceptions.

By using Named Credentials, you can centralize and manage the authentication details for external systems or services, making integrations more secure, flexible, and easier to maintain.

Leave a Reply

Your email address will not be published. Required fields are marked *