{"id":2624,"date":"2012-01-17T23:25:52","date_gmt":"2012-01-17T17:55:52","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2624"},"modified":"2015-06-01T15:14:17","modified_gmt":"2015-06-01T15:14:17","slug":"getting-record-from-one-salesforce-organization-to-other","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/getting-record-from-one-salesforce-organization-to-other\/","title":{"rendered":"Getting record from other Salesforce organization OR communication between multiple salesforce organization"},"content":{"rendered":"<p>In this article, I\u00a0will explain the code which can be used for connecting and getting the records from different or multiple salesforce organization using Apex and REST Service.<\/p>\n<p>Note : <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-to-salesforce-integration-using-named-credentials-in-just-5-lines-of-code\/\">Here is new article to connect two salesforce instances in just 5 lines of code using Named Credentials.<\/a><\/p>\n<p>To start first we will need to authorize below two URL which can be accessed from salesforce environment.<br \/>\nThis can be done from <strong>&#8220;Setup | Administration Setup | Security Controls | Remote Site Settings&#8221;\u009d<\/strong><\/p>\n<ol>\n<li><a href=\"https:\/\/www.salesforce.com\">https:\/\/www.salesforce.com<\/a><\/li>\n<li><a href=\"https:\/\/ap1-api.salesforce.com\">https:\/\/ap1-api.salesforce.com<\/a><\/li>\n<\/ol>\n<figure id=\"attachment_2625\" aria-describedby=\"caption-attachment-2625\" style=\"width: 475px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/01\/Salesforce-Remote-Site-Setting.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\" wp-image-2625   \" title=\"Salesforce Remote Site Setting\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/01\/Salesforce-Remote-Site-Setting.png?resize=475%2C173&#038;ssl=1\" alt=\"Salesforce Remote Site Setting\" width=\"475\" height=\"173\" \/><\/a><figcaption id=\"caption-attachment-2625\" class=\"wp-caption-text\">Salesforce Remote Site Setting<\/figcaption><\/figure>\n<p><!--more--><br \/>\nIt is possible that you may need to change first URL it may be <a href=\"https:\/\/www.na1-api.salesforce.com\">https:\/\/www.na1-api.salesforce.com<\/a> or <a href=\"https:\/\/www.na2-api.salesforce.com\">https:\/\/www.na2-api.salesforce.com<\/a><\/p>\n<p>This application will prompt for the Username and Password of the other salesforce account and display the 10 records of the Account.<\/p>\n<figure id=\"attachment_2626\" aria-describedby=\"caption-attachment-2626\" style=\"width: 475px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/01\/Connect-Other-Salesforce-Account.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\" wp-image-2626    \" title=\"Connect Other Salesforce Account - Output Screen\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/01\/Connect-Other-Salesforce-Account.png?resize=475%2C236&#038;ssl=1\" alt=\"Connect Other Salesforce Account - Output Screen\" width=\"475\" height=\"236\" \/><\/a><figcaption id=\"caption-attachment-2626\" class=\"wp-caption-text\">Connect Other Salesforce Account &#8211; Output Screen<\/figcaption><\/figure>\n<p><strong>Apex Code:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic with sharing class FetchAccount {\r\n\r\n    \/\/Login Domain May be test, prerellogin.pre\r\n    String LOGIN_DOMAIN = 'www';\r\n    public String pwd{get;set;}\r\n    public String userName{get;set;}\r\n    public List&lt;Account&gt; acc{get;set;}\r\n    public String errMsg{get;set;}\r\n    public String displayError{get;set;}\r\n\r\n    public FetchAccount()\r\n    {\r\n        displayError = 'none';\r\n    }\r\n\r\n    public void fetch()\r\n    {\r\n        errMsg  = 'Some error occurred, please try again';\r\n        try\r\n        {\r\n        \/\/-----------------------------------\r\n        \/\/ Login via SOAP\/XML web service api\r\n        \/\/-----------------------------------\r\n        HttpRequest request = new HttpRequest();\r\n        request.setEndpoint('https:\/\/' + LOGIN_DOMAIN + '.salesforce.com\/services\/Soap\/u\/22.0');\r\n        request.setMethod('POST');\r\n        request.setHeader('Content-Type', 'text\/xml;charset=UTF-8');\r\n        request.setHeader('SOAPAction', '&quot;&quot;');\r\n        \/\/not escaping username and password because we're setting those variables above\r\n        \/\/in other words, this line &quot;trusts&quot; the lines above\r\n        \/\/if username and password were sourced elsewhere, they'd need to be escaped below\r\n        request.setBody('&lt;Envelope xmlns=&quot;http:\/\/schemas.xmlsoap.org\/soap\/envelope\/&quot;&gt;&lt;Header\/&gt;&lt;Body&gt;&lt;login xmlns=&quot;urn:partner.soap.sforce.com&quot;&gt;&lt;username&gt;' + userName+ '&lt;\/username&gt;&lt;password&gt;' + pwd+ '&lt;\/password&gt;&lt;\/login&gt;&lt;\/Body&gt;&lt;\/Envelope&gt;');\r\n        Dom.XmlNode resultElmt = (new Http()).send(request).getBodyDocument().getRootElement()\r\n          .getChildElement('Body', 'http:\/\/schemas.xmlsoap.org\/soap\/envelope\/')\r\n          .getChildElement('loginResponse', 'urn:partner.soap.sforce.com')\r\n          .getChildElement('result', 'urn:partner.soap.sforce.com');\r\n\r\n        \/\/-------------------------------\r\n        \/\/ Grab session id and server url\r\n        \/\/--------------------------------\r\n        final String SERVER_URL = resultElmt.getChildElement('serverUrl', 'urn:partner.soap.sforce.com') .getText().split('\/services')&#x5B;0];\r\n        final String SESSION_ID = resultElmt.getChildElement('sessionId', 'urn:partner.soap.sforce.com') .getText();\r\n\r\n        \/\/----------------------------------\r\n        \/\/ Load first 10 accounts via REST API\r\n        \/\/---------------------------------\r\n        final PageReference theUrl = new PageReference(SERVER_URL + '\/services\/data\/v22.0\/query\/');\r\n        theUrl.getParameters().put('q','Select a.Phone, a.Name, a.CreatedBy.FirstName, a.CreatedById From Account a limit 10');\r\n        request = new HttpRequest();\r\n        request.setEndpoint(theUrl.getUrl());\r\n        request.setMethod('GET');\r\n        request.setHeader('Authorization', 'OAuth ' + SESSION_ID);\r\n\r\n        String body = (new Http()).send(request).getBody();\r\n\r\n        JSONParser parser = JSON.createParser(body);\r\n\r\n        do{\r\n            parser.nextToken();\r\n        }while(parser.hasCurrentToken() &amp;&amp; !'records'.equals(parser.getCurrentName()));\r\n\r\n        parser.nextToken();\r\n\r\n        acc = (List&lt;Account&gt;) parser.readValueAs(List&lt;Account&gt;.class);\r\n        }\r\n        catch(Exception e)\r\n        {\r\n            displayError = 'block';\r\n        }\r\n\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Visualforce Code:<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;apex:page controller=&quot;FetchAccount&quot; standardStylesheets=&quot;true&quot;&gt;\r\n&lt;style type=&quot;text\/css&quot;&gt;\r\n.errorMsg{\r\n    font-size:0.8 em;\r\n    color:red;\r\n}\r\n&lt;\/style&gt;\r\n&lt;apex:pageBlock &gt;\r\n&lt;apex:form &gt;\r\n&lt;apex:outputLabel value=&quot;UserName : &quot; for=&quot;userName&quot;\/&gt;\r\n&lt;apex:inputText required=&quot;true&quot; id=&quot;userName&quot; value=&quot;{!userName}&quot; \/&gt;\r\n&lt;br \/&gt;\r\n&lt;apex:outputLabel value=&quot;Password : &quot; for=&quot;pwd&quot;\/&gt;\r\n&lt;apex:inputsecret id=&quot;pwd&quot; value=&quot;{!pwd}&quot;\/&gt;\r\n&lt;br \/&gt;\r\n&lt;apex:commandButton id=&quot;getRecords&quot; value=&quot;Get Records&quot; action=&quot;{!fetch}&quot; rerender=&quot;wrapper&quot; status=&quot;waitStatus&quot; \/&gt;\r\n&lt;apex:actionStatus startText=&quot;Requesting...&quot; stopText=&quot;&quot; id=&quot;waitStatus&quot;\/&gt;\r\n&lt;hr \/&gt;\r\n&lt;apex:outputPanel id=&quot;wrapper&quot;&gt;\r\n&lt;div class=&quot;errorMsg&quot; style=&quot;display:{!displayError}&quot;&gt; {!errMsg} &lt;\/div&gt;\r\n&lt;apex:pageBlockTable value=&quot;{!acc}&quot; var=&quot;account&quot; id=&quot;accTable&quot; rowClasses=&quot;odd,even&quot; styleClass=&quot;tableClass&quot;&gt;\r\n\r\n    &lt;apex:column &gt;\r\n        &lt;apex:facet name=&quot;header&quot;&gt;Account Name&lt;\/apex:facet&gt;\r\n         &lt;apex:outputText value=&quot;{!account.name}&quot;\/&gt;\r\n    &lt;\/apex:column&gt;\r\n\r\n    &lt;apex:column &gt;\r\n        &lt;apex:facet name=&quot;header&quot;&gt;Created By&lt;\/apex:facet&gt;\r\n         &lt;apex:outputText value=&quot;{!account.CreatedBy.FirstName}&quot;\/&gt;\r\n    &lt;\/apex:column&gt;\r\n\r\n    &lt;apex:column &gt;\r\n        &lt;apex:facet name=&quot;header&quot;&gt;Phone&lt;\/apex:facet&gt;\r\n         &lt;apex:outputText value=&quot;{!account.Phone}&quot;\/&gt;\r\n    &lt;\/apex:column&gt;\r\n\r\n&lt;\/apex:pageBlockTable&gt;\r\n&lt;\/apex:outputPanel&gt;\r\n&lt;\/apex:form&gt;\r\n&lt;\/apex:pageBlock&gt;\r\n&lt;\/apex:page&gt;\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Calling Rest Web service using Apex &#8211; Getting record from other Salesforce organization or Account or communication between multiple salesforce organization<\/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_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":"","jetpack_post_was_ever_published":false},"categories":[20,24,9,18],"tags":[337,331,336],"class_list":["post-2624","post","type-post","status-publish","format-standard","hentry","category-apex","category-force-com","category-salesforce","category-visualforce","tag-apex","tag-salesforce","tag-visualforce"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":28,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions\/","url_meta":{"origin":2624,"position":0},"title":"Salesforce Interview Questions &#8211; Part 1","author":"Jitendra","date":"May 6, 2010","format":false,"excerpt":"Set of most often asked questions on the salesforce.com developement","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3773,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-faq-part-19\/","url_meta":{"origin":2624,"position":1},"title":"Salesforce interview questions &#8211; Part 19","author":"Jitendra","date":"December 16, 2014","format":false,"excerpt":"Salesforce interview questions for developers and admins around Apex, Visualforce, getting Salesforce object name on basis of Id, Apex API limits","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":2499,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/getting-started-with-apex-debug-logs-in-salesforce\/","url_meta":{"origin":2624,"position":2},"title":"Getting started with Apex debug logs in salesforce","author":"Jitendra","date":"November 26, 2011","format":false,"excerpt":"Introduction to debug logs in salesforce with step by step procedure and screenshot, Difference between Debug log and System log in Salesforce","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Debug Logs in Monitoring section of Administration setup - salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/11\/Debug-Logs-in-Monitoring-section-of-Administration-setup-salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2825,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-7\/","url_meta":{"origin":2624,"position":3},"title":"Salesforce Interview Questions &#8211; Part 7","author":"Jitendra","date":"April 23, 2012","format":false,"excerpt":"Basic concepts and Interview Questions of salesforce, Visualforce, Apex and SOQL","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":6880,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/get-current-user-id-in-salesforce\/","url_meta":{"origin":2624,"position":4},"title":"Get Current User Id in Salesforce","author":"Jitendra","date":"February 2, 2019","format":false,"excerpt":"How to get Current Logged in user Id in Apex, Visualforce, Lightning Component and Formula fields in Salesforce","rel":"","context":"In &quot;Lightning&quot;","block_context":{"text":"Lightning","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/lightning\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2681,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-5\/","url_meta":{"origin":2624,"position":5},"title":"Salesforce Interview Questions &#8211; Part 5","author":"Jitendra","date":"January 24, 2012","format":false,"excerpt":"Basic concepts and Interview Questions of salesforce, Visualforce, Apex and SOQL","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":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2624","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=2624"}],"version-history":[{"count":2,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2624\/revisions"}],"predecessor-version":[{"id":4544,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2624\/revisions\/4544"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}