Salesforce to Salesforce integration using Named Credentials in 5 lines

I have already written article to integrate Salesforce with other Salesforce instance around 3 years back. In last 3 years, Salesforce has changed a lot. This time I will integrate Salesforce with other Salesforce only only in 5 lines of code, can you believe it  🙂 ? Check my old article, and its around 50+ lines of code with security control.

What is Named Credential ?

A named credential specifies the URL of a callout endpoint and its required authentication parameters in one definition. You can simplify the setup of authenticated Apex callouts by specifying a named credential as the callout endpoint. You can instead specify a URL as the callout endpoint and register that URL in your organization’s remote site settings. In that case, however, you handle the authentication in your code. Doing so can be less secure and especially complicated for OAuth authentication.

Long description short , “using Named Credential, we can make call out to external system without supplying username or Password”.

To connect with external system using “Named Credential”, we need to follow below steps

  1. Create Connected App
  2. Create Authorization Provider
  3. Define Named Credential
  4. Use Apex to connect in 5 lines of code

For first 2 steps, you need to go through this article which explains in detail how to define Connected App and Authorization Provider.

Creating Named Credential

Now, directly start with step 3 to create Named credential by navigating to “Setup | Administer | Security Controls | Named Credentials | New Named Credential “.

Provide below information in settings :

Label and NameAny suitable value. We will need Name to refer in Apex code
URLURL of Salesforce instance where we want to Connect
Identity Type Named Principal
Authentication ProtocolOAuth 2.0
Authentication ProviderSelect Authentication Provider created in step 2
Scoperefresh_token full
Named Credential Salesforce
Named Credential Salesforce

Make sure to check “Start Authentication Flow on Save” and click on Save. After clicking on “Save” new page will open to authenticate Salesforce Org using OAuth2 connected App. If authentication is success, you can see message like “Authenticated as <Salesforce Username>” as shown in below image.

Authenticated Named Credential in Salesforce
Authenticated Named Credential in Salesforce

Now execute below code in anonymous Apex and you should be able to see list of all REST API available in Salesforce instance.

HttpRequest feedRequest = new HttpRequest();
Http http = new Http();
HTTPResponse feedResponse = http.send(feedRequest);

As you can see in above code, we are not supplying any credentials. syntax to use Named Credential is like

callout:[Named Credential Name]/[Callout URL of other Salesforce Org]

You can consume standard as well custom webservice of other org using this approach.


Related posts

  • Badan Singh Pundeer

    Thanks Jitendra, Its more than awesome!

  • Brad

    Thank you!

  • Faiza Naz

    I am try to connect my one dev org with my another dev org but when i try to authenticate named credentials .It gave me below error.

    We can’t log you in because of the following error. For more information, contact your Salesforce administrator.

    SAME_ORG_SSO: Cannot sign on into same org.

    I am providing my second dev org domain URL in the named credentials URL.Please let me know if i am doing something wrong.

    • It seems you are trying to access same org using Named credential. We need to separate Salesforce org for this example to be working.