{"id":6152,"date":"2014-02-27T17:58:25","date_gmt":"2014-02-27T17:58:25","guid":{"rendered":"http:\/\/www.jitendrazaa.com\/blog\/?p=6152"},"modified":"2017-06-12T18:04:13","modified_gmt":"2017-06-12T18:04:13","slug":"call-salesforce-rest-api-from-apex","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/call-salesforce-rest-api-from-apex\/","title":{"rendered":"Call Salesforce REST API from Apex"},"content":{"rendered":"<p style=\"text-align: justify;\">In <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-rest-api-playground\/\">previous <\/a>post, we saw that how Visualforce can be used to call\u00a0<strong>Salesforce Rest API.\u00a0<\/strong>In this short post, I would share a small piece of code to demonstrate\u00a0<strong>how to use Apex to call Salesforce REST API.<\/strong><\/p>\n<p style=\"text-align: justify;\">First and foremost step is to add your Salesforce instance URL in\u00a0<strong>Remote site settings.\u00a0<\/strong>Once that is done, use below sample Apex code to call Salesforce REST API.<\/p>\n<p style=\"text-align: justify;\">I am using API to get metadata information about Salesforce object however that can be replaced by any supported REST API of Salesforce.<!--more--><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/Make sure your Salesforce instance URL is added in remote site settings\r\nString sfdcURL = URL.getSalesforceBaseUrl().toExternalForm(); \r\nString restAPIURL = sfdcURL + '\/services\/data\/v29.0\/sobjects\/';  \r\n  \r\nHttpRequest httpRequest = new HttpRequest();  \r\nhttpRequest.setMethod('GET');   \r\nhttpRequest.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());        \r\nhttpRequest.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); \r\nhttpRequest.setEndpoint(restAPIURL);  \r\nString response = '';\r\ntry {  \r\n\t\t Http http = new Http();   \r\n\t\t HttpResponse httpResponse = http.send(httpRequest);  \r\n\t\t if (httpResponse.getStatusCode() == 200 ) {  \r\n\t\t\t   response = JSON.serializePretty( JSON.deserializeUntyped(httpResponse.getBody()) );  \r\n\t\t } else {  \r\n\t\t\t   System.debug(' httpResponse ' + httpResponse.getBody() );  \r\n\t\t\t   throw new CalloutException( httpResponse.getBody() );  \r\n\t\t }   \r\n} catch( System.Exception e) {  \r\n\t\t System.debug('ERROR: '+ e);  \r\n\t\t throw e;  \r\n}  \r\nSystem.debug(' ** response ** : ' + response );  \r\n<\/pre>\n<p style=\"text-align: justify;\">Key aspect in above code is how session Id is used as a value in <strong>OAuth<\/strong> and <strong>Bearer<\/strong> headers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use Apex to call Salesforce REST API with sample source code<\/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":[9],"tags":[120,170],"class_list":["post-6152","post","type-post","status-publish","format-standard","hentry","category-salesforce","tag-integration","tag-rest-api"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3851,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex\/send-email-with-generated-pdf-as-attachment-from-trigger\/","url_meta":{"origin":6152,"position":0},"title":"Send Email with Generated PDF as attachment from Trigger &#8211; before Winter 16","author":"Jitendra","date":"April 16, 2014","format":false,"excerpt":"There may be scenario in Salesforce that you need to send a Visualforce page rendered as PDF as a part of Email Attachment. This will be very easy if you want to perform this using Controller or Extension class, we just have to call getContentAsPDF() method of PageReference class and\u2026","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":2624,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/getting-record-from-one-salesforce-organization-to-other\/","url_meta":{"origin":6152,"position":1},"title":"Getting record from other Salesforce organization OR communication between multiple salesforce organization","author":"Jitendra","date":"January 17, 2012","format":false,"excerpt":"Calling Rest Web service using Apex - Getting record from other Salesforce organization or Account or communication between multiple salesforce organization","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Salesforce Remote Site Setting","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/01\/Salesforce-Remote-Site-Setting.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":4532,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-to-salesforce-integration-using-named-credentials-in-just-5-lines-of-code\/","url_meta":{"origin":6152,"position":2},"title":"Salesforce to Salesforce integration using Named Credentials in 5 lines","author":"Jitendra","date":"May 29, 2015","format":false,"excerpt":"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:) ? Check my old\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Authenticated Named Credential in Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Authenticated-Named-Credential-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":6975,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-integration-patterns-best-practices-with-video\/","url_meta":{"origin":6152,"position":3},"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":3773,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-faq-part-19\/","url_meta":{"origin":6152,"position":4},"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":6113,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/design-continuation-server-in-salesforce\/","url_meta":{"origin":6152,"position":5},"title":"Design Continuation Server in Salesforce","author":"Jitendra","date":"May 31, 2017","format":false,"excerpt":"How to implement Continuation Server in Salesforce with the help of Action Function and Continuation Object","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Continuation Server in Salesforce - Demo","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Demo.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Demo.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Demo.gif?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Demo.gif?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Demo.gif?resize=1050%2C600&ssl=1 3x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/6152","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=6152"}],"version-history":[{"count":1,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/6152\/revisions"}],"predecessor-version":[{"id":6153,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/6152\/revisions\/6153"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=6152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=6152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=6152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}