Thursday, September 17, 2020

Integrating two Salesforce Org Using Named Credential

Security is the one which every customer concern about while implementing SF application and integrating with external system. So if we want to hide the external url, username, password while connecting external system, then Named Credential is something that you need to implement.

Named credential specifies the call out end point url and authentication in one definition. It doesn’t require to add end point URL in Remote Site settings. Named credential support two types of authentication protocol Basic User Name/Password Authentication and OAuth 2.0 protocol.

In this blog article, we will be going to connect two salesforce Org using Named Credential and OAuth 2.0 authentication protocol.

Following steps, we need to perform –

Steps#1: Create connected App in destination org

Steps#2: Create AuthProvider in source Org

Steps#3: Create named Credential in source Org

Steps#4: Write apex in source Org to fetch data from destination Org.


Connected App in destination Org:

A connected app is a framework that enables an external application to integrate with Salesforce using different authentication protocol. Please check salesforce article about Connected App


Go to SetupàCreateàAppsà New (Connected App)

Checked Enable OAuth Settings and select the OAuth scope. OAuth scope is basically the level access that connected App needed for the integration.

Put callback url as dummy url which we will change later.


Create Auth Provider in Source Org:

Go to SetupàSecurity Controls à Auth. Provider à New

Copy the Consumer Key/Consumer Secret of the connected app created in destination org.

The Authorization end point should be -

https://<base url of the destination org>/services/oauth2/authorize

and Token Endpoint Url should be-

https://<base url of the destination org>/services/oauth2/token

Default Scope is the permission scope which need to be enter. There should be a space between two scope.

Upon clicks on save, it will generate call back url-

Copy the call back url and paste the same in the connected App in destination Org.

Named Credentials in source Org:

Go to SetupàSecurity Controls à Named Credentials à New Name Credential

Select Authentication Protocol as OAuth 2.0 and choose Authentication Provider which has been created in earlier steps. On clicks on save the page will redirect to destination Org where you need to login to verify the authentication.


Write Apex to connect destination org and fetch data:

Now calling an external system is only 5-6 lines code. No user name, No password, No end point URL. You only need to specify the named credential name which will take care the authentication.

Set httpsRequest endpoint as-


No comments:

Post a Comment