Salesforce Named Credentials and Connected Apps

Salesforce offers powerful tools to integrate with other platforms and services. Two of these tools are Named Credentials and Connected Apps. In this tutorial, you’ll learn what each of these is, when to use them, and see practical examples.

1. Named Credentials

What are Named Credentials?

Named Credentials are a safe and secure way to store sensitive information, such as authentication details, that your Salesforce apps might need to access external services. This is especially useful for integrations, where you’d need to store API endpoints, usernames, passwords, and other authentication details.

Why use Named Credentials?

  • Security: The main reason to use Named Credentials is to keep sensitive information secure. Salesforce encrypts this data and keeps it hidden.
  • Simplification: It simplifies the process of calling out to external systems by handling the authentication details behind the scenes.
  • Maintenance: If an endpoint or authentication detail changes, you can update it in one place instead of updating all the pieces of code that reference it.

How to set up a Named Credential?

  1. Go to Setup.
  2. In the Quick Find box, type Named Credentials.
  3. Click on New Named Credential.
  4. Fill in the necessary fields:
    • Label and Name: Identifying names for the credential.
    • URL: The endpoint for the external service.
    • Identity Type: Whether the credential is for a specific user or the whole organization.
    • Authentication Protocol: The type of authentication (e.g., Password Authentication, OAuth 2.0).
    • Depending on your choices, you’ll fill in related details like username, password, etc.
  5. Click Save.

Use Case:

Consider a scenario where Salesforce needs to communicate with an external REST API service that requires Basic Authentication. Instead of hardcoding the endpoint and the credentials in an Apex class, use a Named Credential.

Example: Once you’ve set up the Named Credential, you can easily make a callout in Apex:

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:MyNamedCredential/path_to_resource');
req.setMethod('GET');
HttpResponse res = new Http().send(req);

 

Note: Replace MyNamedCredential with the name you provided when creating the Named Credential.

2. Connected Apps

What are Connected Apps?

Connected Apps allow external applications to connect to Salesforce over Identity and Data APIs. These apps use standard OAuth protocols to authenticate, provide Single Sign-On, and provide tokens for making API calls.

Why use Connected Apps?

  • Authentication: Allows external apps to authenticate and access Salesforce resources using OAuth 2.0.
  • SSO (Single Sign-On): Lets users sign into Salesforce and other apps using a single identity.
  • Manage Access: You can control which users or profiles have access to the connected app and what data they can access.

How to create a Connected App?

  1. Go to Setup.
  2. In the Quick Find box, type App Manager.
  3. Click New Connected App.
  4. Provide the necessary details:
    • Connected App Name, API Name, Contact Email.
    • In the API section, enable OAuth and provide a callback URL (used for the OAuth flow). Also, select the required OAuth scopes.
    • Fill in other fields as needed for your use case.
  5. Click Save.

Upon creation, you’ll be provided with a Consumer Key and Consumer Secret which are used by external apps to initiate the OAuth flow.

Use Case:

Imagine a mobile app that needs to pull Salesforce data for a logged-in user. The mobile app uses the Consumer Key and Secret to authenticate, and once the user logs in, it obtains an OAuth token which can be used for making further API calls.

Example: The mobile app initiates the OAuth flow by redirecting the user to a Salesforce login page:

https://login.salesforce.com/services/oauth2/authorize?response_type=token&client_id=YOUR_CONSUMER_KEY&redirect_uri=YOUR_CALLBACK_URL

 

After successful authentication, Salesforce redirects to the redirect_uri with an access token that the app can use for making authenticated API requests.

Conclusion:

Named Credentials and Connected Apps are powerful tools that help developers securely integrate Salesforce with external systems. While Named Credentials are more about securely storing and managing external service details, Connected Apps are focused on providing external apps with a secure way to authenticate and access Salesforce resources. It’s essential to understand the use cases and choose the right tool for the job.

Leave a Comment