{"id":7147,"date":"2020-07-20T02:29:06","date_gmt":"2020-07-20T06:29:06","guid":{"rendered":"https:\/\/www.jitendrazaa.com\/blog\/?p=7147"},"modified":"2020-07-20T02:29:06","modified_gmt":"2020-07-20T06:29:06","slug":"salesforce-integration-with-nodejs-based-applications-using-canvas","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-integration-with-nodejs-based-applications-using-canvas\/","title":{"rendered":"Salesforce Integration with Nodejs based applications using Canvas"},"content":{"rendered":"\n<p class=\"justify\">Salesforce provides many ways to integrate with external systems like SOAP, Rest, Bulk API, User Interface API and so on. One of useful way to integrate any existing web applications with Salesforce is using Canvas.<\/p>\n\n\n\n<p class=\"justify\">For sake of this post, I&#8217;m using Nodejs application and complete source code can be found <a href=\"https:\/\/github.com\/JitendraZaa\/CanvasDemo\">here<\/a> , on my <a href=\"https:\/\/github.com\/JitendraZaa\/CanvasDemo\">Github repository<\/a>. It can be deployed on Heroku easily, however I used my local computer to run canvas. That also proves point that integration is happening via Browser and therefore canvas application can be hosted on premise and <a href=\"https:\/\/en.wikipedia.org\/wiki\/DMZ_(computing)\">not necessarily on DMZ layer<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1 : Create Connected App in Salesforce<\/h3>\n\n\n\n<p class=\"justify\">Enable OAuth in Connected app and provide any Callback URL. Canvas app does not use callback URL however we DO NEED scope. <\/p>\n\n\n\n<p class=\"justify\">Next step would be enabling canvas app itself connected app along with locations where we would be using it. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"753\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-App-in-Nodejs.png?resize=1024%2C753&#038;ssl=1\" alt=\"\" class=\"wp-image-7149\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-App-in-Nodejs.png?resize=1024%2C753&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-App-in-Nodejs.png?resize=300%2C221&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-App-in-Nodejs.png?resize=768%2C565&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-App-in-Nodejs.png?resize=1536%2C1130&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-App-in-Nodejs.png?resize=1200%2C882&amp;ssl=1 1200w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-App-in-Nodejs.png?w=1678&amp;ssl=1 1678w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\">Step 2 : Wrap Canvas Connected App in Visualforce<\/h3>\n\n\n\n<p class=\"justify\">In this post, we will host canavs app in Visualforce page and then add Visualforce page on Account page. LWC still odes not support Canvas app and I&#8217;m not huge fan of Aura , so VF fits the bill for quick demo.<\/p>\n\n\n\n<p>Visualforce Source Code<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;apex:page standardController=&quot;Account&quot;&gt;\n    &lt;apex:canvasApp applicationName=&quot;Localhost_Connected_App&quot; \n        height=&quot;2000px&quot; width=&quot;750px&quot;\/&gt;\n&lt;\/apex:page&gt;\n<\/pre><\/div>\n\n\n<p class=\"justify\">For more advance control &amp; granular capability, we can inject Lifecycle class in Canvas app, full documentation can be found in <a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.platform_connect.meta\/platform_connect\/canvas_creating_a_new_canvaslifecyclehandler.htm\">canvas developer guide here<\/a>. Below source code I&#8217;ve used in video demo<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/**\n * Sample Apex class to show case Canvas Life Cycle class demo\n *\/\npublic with sharing class CanvasDemoAppLifeCycle \n    implements Canvas.CanvasLifecycleHandler {\n\n        public Set&lt;Canvas.ContextTypeEnum&gt; excludeContextTypes(){\n            Set&lt;Canvas.ContextTypeEnum&gt; excluded = new Set&lt;Canvas.ContextTypeEnum&gt;();\n    \n            \/\/ Code goes here to add items to excluded list\n            \/\/ that should be excluded from Context data\n    \n            return excluded;\n        }\n    \n        public void onRender(Canvas.RenderContext renderContext) {   \n            \/\/ Get the Application and Environment context from the RenderContext\n            Canvas.ApplicationContext app = renderContext.getApplicationContext();\n            Canvas.EnvironmentContext env = renderContext.getEnvironmentContext();\n\n            if (false){ \n                throw new Canvas.CanvasRenderException(&#039;This is sample error message&#039;);\n            } \n\n            Set&lt;String&gt; fields = new Set&lt;String&gt;{&#039;*&#039;,&#039;Name&#039;};\n            \/\/env.addEntityFields(fields);  \n        }\n    }\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Salesforce Integration using Canvas for Nodejs based applications\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/FhMzTt8IShw?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>How to use Canvas Signed Request Authentication with web based applications like Nodejs &#038; how to use Canvas LifeCycle Handler Class<\/p>\n","protected":false},"author":1,"featured_media":7150,"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":[453,266,9],"tags":[343,492,267,212],"class_list":["post-7147","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-integration","category-node-js","category-salesforce","tag-canvas","tag-integration-patterns","tag-nodejs","tag-video-tutorial"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-Integration-e1624579092898.png?fit=700%2C567&ssl=1","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":4807,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-to-salesforce-integration-using-canvas\/","url_meta":{"origin":7147,"position":0},"title":"Salesforce to Salesforce integration using Canvas","author":"Jitendra","date":"September 14, 2015","format":false,"excerpt":"After writing this article Salesforce has enabled CSP (Content Security Policy) which restricts adding Salesforce in iFrame. We can add MyDomain URL as CSP whitelisting and it works only if user already logged into other Salesforce instance. However, if user is not logged into other instance , internally OAuth navigates\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Force.com Canvas Application Demo with Complete Source code","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/09\/Force.com-Canvas-Application-Demo-with-Complete-Source-code.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/09\/Force.com-Canvas-Application-Demo-with-Complete-Source-code.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/09\/Force.com-Canvas-Application-Demo-with-Complete-Source-code.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":6309,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/using-jwt-flow-to-authenticate-nodejs-application-with-salesforce\/","url_meta":{"origin":7147,"position":1},"title":"Using JWT Flow to Authenticate Nodejs application with Salesforce","author":"Jitendra","date":"October 8, 2017","format":false,"excerpt":"Video tutorial and Complete source code to use Salesforce JWT OAuth to authenticate Nodejs Application","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"JWT OAuth","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/10\/JWT-OAuth.png?fit=862%2C604&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/10\/JWT-OAuth.png?fit=862%2C604&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/10\/JWT-OAuth.png?fit=862%2C604&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/10\/JWT-OAuth.png?fit=862%2C604&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":4038,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/dreamforce-2014-session-create-web-based-ide-for-salesforce\/","url_meta":{"origin":7147,"position":2},"title":"Dreamforce 2014 Session &#8211; create web based IDE for Salesforce","author":"Jitendra","date":"November 7, 2014","format":false,"excerpt":"In Dreamforce 2014, I got chance to present demo of creating online IDE for Salesforce using Tooling API. I used Node.js as a language to communicate with Salesforce API on Heroku Platform. Demo Simple Editor Complete source code (Github) Simple Editor Editor with Ace framework Dont forget to share your\u2026","rel":"","context":"In &quot;Heroku&quot;","block_context":{"text":"Heroku","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/heroku\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5094,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/10-salesforce-integration-design-considerations-from-architect-point-of-view-mind-mapping-included\/","url_meta":{"origin":7147,"position":3},"title":"10 Salesforce Integration design considerations from Architect point of view &#8211; Mind Mapping included","author":"Jitendra","date":"December 21, 2015","format":false,"excerpt":"After working on multiple Salesforce implementation project as an Architect, its time to share\u00a0what I learned from those implementations and would strongly suggest to be considered before designing any \"Salesforce Integration\". Below image shows \"integration mind mapping\" used by me. I use it to consider some major aspects\u00a0while discussing integration\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Integration Mind mapping diagram","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/12\/Salesforce-Integration-Mind-mapping-diagram.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/12\/Salesforce-Integration-Mind-mapping-diagram.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/12\/Salesforce-Integration-Mind-mapping-diagram.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/12\/Salesforce-Integration-Mind-mapping-diagram.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":3951,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/creating-salesforce-query-builder-in-node-js-and-angularjs-using-salesforce-rest-api-and-oauth2\/","url_meta":{"origin":7147,"position":4},"title":"Creating Salesforce Query Builder in Node.js and AngularJs using Salesforce REST API and OAuth2 with Heroku Button","author":"Jitendra","date":"August 18, 2014","format":false,"excerpt":"In this post, we will be creating a\u00a0Salesforce Query (SOQL) Builder.\u00a0It uses Google's Angularjs MVC Javascript library and REST API provided by Salesforce. For Authentication, it uses OAuth2. Node.js is used as a primary language and hosted on Heroku. If you want to directly deploy to your Heroku Account, You\u2026","rel":"","context":"In &quot;HTML&quot;","block_context":{"text":"HTML","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/web\/"},"img":{"alt_text":"SOQL Builder Using Node.js AngularJs Salesforce REST API on Heroku","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/08\/SOQL-Builder-Using-Node.js-AngularJs-Salesforce-REST-API-on-Heroku-1024x504.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/08\/SOQL-Builder-Using-Node.js-AngularJs-Salesforce-REST-API-on-Heroku-1024x504.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/08\/SOQL-Builder-Using-Node.js-AngularJs-Salesforce-REST-API-on-Heroku-1024x504.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":5987,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/use-lightning-components-on-external-websites-lightning-out\/","url_meta":{"origin":7147,"position":5},"title":"Use Lightning Components on external websites &#8211; Lightning Out","author":"Jitendra","date":"April 5, 2017","format":false,"excerpt":"How to use Lightning component in node.js (External websites) by using Lightning Out. It shows how to enable CORS and SSL in Node.js with Video and complete source code.","rel":"","context":"In &quot;Lightning&quot;","block_context":{"text":"Lightning","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/lightning\/"},"img":{"alt_text":"Show Lightning Component in Node.js using Lightning Out","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/7147","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=7147"}],"version-history":[{"count":2,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/7147\/revisions"}],"predecessor-version":[{"id":7151,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/7147\/revisions\/7151"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media\/7150"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=7147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=7147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=7147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}