{"id":4532,"date":"2015-05-29T17:45:48","date_gmt":"2015-05-29T17:45:48","guid":{"rendered":"http:\/\/www.jitendrazaa.com\/blog\/?p=4532"},"modified":"2019-10-24T13:38:08","modified_gmt":"2019-10-24T17:38:08","slug":"salesforce-to-salesforce-integration-using-named-credentials-in-just-5-lines-of-code","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-to-salesforce-integration-using-named-credentials-in-just-5-lines-of-code\/","title":{"rendered":"Salesforce to Salesforce integration using Named Credentials in 5 lines"},"content":{"rendered":"<p>I have already written article to <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/getting-record-from-one-salesforce-organization-to-other\/\">integrate Salesforce with other Salesforce instance around 3 years back<\/a>. 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 \u00a0\ud83d\ude42 ? Check my <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/getting-record-from-one-salesforce-organization-to-other\/\">old article<\/a>, and its around 50+ lines of code with security control.<\/p>\n<p><strong>What is Named Credential ?<\/strong><\/p>\n<p>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&#8217;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.<\/p>\n<blockquote>\n<p>Long description short , &#8220;using Named Credential, we can make call out to external system without supplying username or Password&#8221;.<\/p>\n<\/blockquote>\n<p><!--more--><\/p>\n<p>To connect with external system using &#8220;Named Credential&#8221;, we need to follow below steps<\/p>\n<ol>\n<li>Create Connected App<\/li>\n<li>Create Authorization Provider<\/li>\n<li>Define Named Credential<\/li>\n<li>Use Apex to connect in 5 lines of code<\/li>\n<\/ol>\n<p>For first 2 steps, you need to go through <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/login-to-salesforce-from-salesforce-using-authentication-provider\/#more-4516\">this article which explains in detail how to define Connected App and Authorization Provider<\/a>.<\/p>\n<p><strong>Creating Named Credential<\/strong><\/p>\n<p>Now, directly start with step 3 to create Named credential by navigating to &#8220;Setup | Administer | Security Controls | Named Credentials | New Named Credential &#8220;.<\/p>\n<p>Provide below information in settings :<\/p>\n\n<table id=\"tablepress-22\" class=\"tablepress tablepress-id-22\">\n<thead>\n<tr class=\"row-1\">\n\t<th class=\"column-1\">Setting<\/th><th class=\"column-2\">Value<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-striping row-hover\">\n<tr class=\"row-2\">\n\t<td class=\"column-1\">Label and Name<\/td><td class=\"column-2\">Any suitable value. We will need Name to refer in Apex code<\/td>\n<\/tr>\n<tr class=\"row-3\">\n\t<td class=\"column-1\">URL<\/td><td class=\"column-2\">URL of Salesforce instance where we want to Connect<\/td>\n<\/tr>\n<tr class=\"row-4\">\n\t<td class=\"column-1\"><a href=\"https:\/\/help.salesforce.com\/HTViewHelpDoc?id=named_credentials_define.htm&amp;language=en_US\" alt=\"Define a Named Credential\" title=\"Define a Named Credential\">Identity Type <\/a><\/td><td class=\"column-2\">Named Principal<\/td>\n<\/tr>\n<tr class=\"row-5\">\n\t<td class=\"column-1\">Authentication Protocol<\/td><td class=\"column-2\">OAuth 2.0<\/td>\n<\/tr>\n<tr class=\"row-6\">\n\t<td class=\"column-1\">Authentication Provider<\/td><td class=\"column-2\">Select Authentication Provider created in step 2<\/td>\n<\/tr>\n<tr class=\"row-7\">\n\t<td class=\"column-1\">Scope<\/td><td class=\"column-2\">refresh_token full<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<!-- #tablepress-22 from cache -->\n<figure id=\"attachment_4538\" aria-describedby=\"caption-attachment-4538\" style=\"width: 520px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Named-Credential-Salesforce.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4538\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Named-Credential-Salesforce.png?resize=520%2C570&#038;ssl=1\" alt=\"Named Credential Salesforce\" width=\"520\" height=\"570\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Named-Credential-Salesforce.png?w=520&amp;ssl=1 520w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Named-Credential-Salesforce.png?resize=274%2C300&amp;ssl=1 274w\" sizes=\"auto, (max-width: 520px) 100vw, 520px\" \/><\/a><figcaption id=\"caption-attachment-4538\" class=\"wp-caption-text\">Named Credential Salesforce<\/figcaption><\/figure>\n<p>Make sure to check &#8220;Start Authentication Flow on Save&#8221; and click on Save. After clicking on &#8220;Save&#8221; new page will open to authenticate Salesforce Org using OAuth2 connected App. If authentication is success, you can see message like &#8220;Authenticated as &lt;Salesforce Username&gt;&#8221; as shown in below image.<\/p>\n<figure id=\"attachment_4539\" aria-describedby=\"caption-attachment-4539\" style=\"width: 476px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Authenticated-Named-Credential-in-Salesforce.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4539\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Authenticated-Named-Credential-in-Salesforce.png?resize=476%2C330&#038;ssl=1\" alt=\"Authenticated Named Credential in Salesforce\" width=\"476\" height=\"330\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Authenticated-Named-Credential-in-Salesforce.png?w=476&amp;ssl=1 476w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Authenticated-Named-Credential-in-Salesforce.png?resize=300%2C208&amp;ssl=1 300w\" sizes=\"auto, (max-width: 476px) 100vw, 476px\" \/><\/a><figcaption id=\"caption-attachment-4539\" class=\"wp-caption-text\">Authenticated Named Credential in Salesforce<\/figcaption><\/figure>\n<p>Now execute below code in anonymous Apex and you should be able to see list of all\u00a0REST\u00a0API available in Salesforce instance.<\/p>\n<pre><pre class=\"brush: java; highlight: [2]; title: ; notranslate\" title=\"\">\nHttpRequest feedRequest = new HttpRequest();\nfeedRequest.setEndpoint('callout:Salesforce_Org\/services\/data\/v32.0');\nfeedRequest.setMethod('GET');\nHttp http = new Http();\nHTTPResponse feedResponse = http.send(feedRequest);\nwhile (feedResponse.getStatusCode() == 302) {\n                feedRequest.setEndpoint(feedResponse.getHeader('Location'));\n                feedResponse = new Http().send(feedRequest);\n            }\nSystem.debug(feedResponse.getBody());\n<\/pre><\/pre>\n<p>As you can see in above code, we are not supplying any credentials. syntax to use Named Credential is like<\/p>\n<pre><pre class=\"brush: java; title: ; notranslate\" title=\"\">\ncallout:&#x5B;Named Credential Name]\/&#x5B;Callout URL of other Salesforce Org]\n<\/pre><\/pre>\n<p>You can consume standard as well custom web-service of other org using this approach.<\/p>\n\n\n<h2 class=\"wp-block-heading\">Error : System.HttpResponse[Status=Moved Temporarily, StatusCode=302]<\/h2>\n\n\n\n<p>Sometime you may get above error in <strong>Named Credential<\/strong> and that&#8217;s why in above code I have added below extra block <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nwhile (feedResponse.getStatusCode() == 302) { feedRequest.setEndpoint(feedResponse.getHeader('Location')); feedResponse = new Http().send(feedRequest); }\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>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 \u00a0\ud83d\ude42 ? Check my old article, and its around 50+ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[337,319,479,120,320,260],"class_list":["post-4532","post","type-post","status-publish","format-standard","hentry","category-salesforce","tag-apex","tag-authentication-provider","tag-error-handling","tag-integration","tag-named-credentials","tag-oauth"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":6975,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-integration-patterns-best-practices-with-video\/","url_meta":{"origin":4532,"position":0},"title":"Salesforce Integration Patterns &#038; Best Practices &#8211; with Video","author":"Jitendra","date":"February 2, 2020","format":false,"excerpt":"When to use which integration patterns in Salesforce along with best practices. Video to show how to use Outbound message to integrate Salesforce without writing code","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Integration Patterns in Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/02\/Integration-Patterns-in-Salesforce.png?fit=1200%2C425&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/02\/Integration-Patterns-in-Salesforce.png?fit=1200%2C425&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/02\/Integration-Patterns-in-Salesforce.png?fit=1200%2C425&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/02\/Integration-Patterns-in-Salesforce.png?fit=1200%2C425&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/02\/Integration-Patterns-in-Salesforce.png?fit=1200%2C425&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":4516,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/login-to-salesforce-from-salesforce-using-authentication-provider\/","url_meta":{"origin":4532,"position":1},"title":"Login to Salesforce from Salesforce using Authentication Provider","author":"Jitendra","date":"May 29, 2015","format":false,"excerpt":"There are many ways to login to your Salesforce instance, using Google, Facebook, Linked, Twitter and even from other Salesforce Organization. I am sure many of readers has multiple Salesforce instances and its hard to remember password of each. We can connect every Salesforce instances and login using only one.\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce My Domain Login page","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Salesforce-My-Domain-Login-page.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":5562,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/summer-16-top-features\/","url_meta":{"origin":4532,"position":2},"title":"Salesforce Summer 16 &#8211; My favorite top 20 features","author":"Jitendra","date":"June 28, 2016","format":false,"excerpt":"List of Salesforce Summer 16 features","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"User Switcher in Salesforce Summer 16","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/06\/User-Switcher-in-Salesforce-Summer-16.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2835,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-apex-code-talk-important-questions-and-answers\/","url_meta":{"origin":4532,"position":3},"title":"Salesforce Apex Code talk &#8211; Important Questions and Answers","author":"Jitendra","date":"May 3, 2012","format":false,"excerpt":"Salesforce Apex Code talk on - 24-April-2012 , Important Interview Questions","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4486,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/continuation-object-in-apex-asynchronous-callouts-for-long-running-request-live-demo\/","url_meta":{"origin":4532,"position":4},"title":"Continuation object in Apex &#8211; Asynchronous callouts for long running request &#8211; Live Demo","author":"Jitendra","date":"May 22, 2015","format":false,"excerpt":"Check the below video first if you are planning to use Continuation https:\/\/youtu.be\/ya2N9EX9dmg We may run into\u00a0scenario in Salesforce project, where we need call external web service but no need to\u00a0wait for response. Response from external web service can be processed asynchronously and once processed it can be presented in\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Execution Flow of an Asynchronous Callout - Image from Salesforce documentation","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2445,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/create-a-custom-web-service-in-salesforce-and-consume-it-in-c-net-application\/","url_meta":{"origin":4532,"position":5},"title":"Create a custom Web service in Salesforce and consume it in C#.Net application","author":"Jitendra","date":"September 23, 2011","format":false,"excerpt":"In this tutorial, we will create the web service in salesforce and consume it in c#.Net application","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"Create Custom web service in salesforce using apex and consume using C#.Net","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/09\/Create-Custom-web-service-in-salesforce-using-apex-and-consume-using-C.Net_.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/4532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=4532"}],"version-history":[{"count":9,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/4532\/revisions"}],"predecessor-version":[{"id":6928,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/4532\/revisions\/6928"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=4532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=4532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=4532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}