{"id":7569,"date":"2022-05-08T00:51:30","date_gmt":"2022-05-08T04:51:30","guid":{"rendered":"https:\/\/www.jitendrazaa.com\/blog\/?p=7569"},"modified":"2022-05-08T00:57:10","modified_gmt":"2022-05-08T04:57:10","slug":"using-microsoft-adaptive-cards-in-lwc","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/using-microsoft-adaptive-cards-in-lwc\/","title":{"rendered":"Using Microsoft Adaptive Cards in LWC"},"content":{"rendered":"\n<p class=\"justify\">There could be a situation in your project where UI components in your application need to be defined at run time where Salesforce may or may not have control. We can solve this situation in LWC using CSS or <em><a href=\"https:\/\/developer.salesforce.com\/docs\/component-library\/documentation\/en\/lwc\/lwc.create_conditional\">template:if<\/a><\/em> directive.<\/p>\n\n\n\n<p class=\"justify\">There is another alternative to the above problem statement, which can be an extremely lightweight framework from Microsoft &#8211; <a href=\"https:\/\/docs.microsoft.com\/en-us\/adaptive-cards\/sdk\/rendering-cards\/javascript\/getting-started\">Adaptive cards<\/a>. <\/p>\n\n\n\n<p class=\"justify\">In nutshell, Adaptive cards take a JSON input and convert it to HTML UI elements. In this blog post, I would be sharing simple source code to depict how it works.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"justify\">However, before I proceed, one issue I faced was &#8211; how to exchange data between LWC components &amp; Adaptive cards components.  we cant use <strong>this<\/strong> variable in the adaptive cards library. I tried multiple ways to get the context of LWC and eventually I ended up updating <a href=\"https:\/\/github.com\/JitendraZaa\/lwc-adaptive-cards\/blob\/master\/force-app\/main\/default\/staticresources\/adaptivecards.js\">adaptive.js<\/a> file itself by adding the below lines of code. <\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/JitendraZaa\/9b7f8dd16e78fa348a0c2a86ba01f819.js\"><\/script>\n\n\n\n<p class=\"justify\">This is the final output of how it would look &amp; you can <a href=\"https:\/\/github.com\/JitendraZaa\/lwc-adaptive-cards\">find the complete source code here in my git repository<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"616\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2022\/05\/adaptivecard-demo-in-LWC.png?resize=1024%2C616&#038;ssl=1\" alt=\"\" class=\"wp-image-7577\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2022\/05\/adaptivecard-demo-in-LWC.png?resize=1024%2C616&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2022\/05\/adaptivecard-demo-in-LWC.png?resize=300%2C181&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2022\/05\/adaptivecard-demo-in-LWC.png?resize=768%2C462&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2022\/05\/adaptivecard-demo-in-LWC.png?resize=1536%2C925&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2022\/05\/adaptivecard-demo-in-LWC.png?resize=1568%2C944&amp;ssl=1 1568w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2022\/05\/adaptivecard-demo-in-LWC.png?w=1658&amp;ssl=1 1658w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption>The outcome of Adaptive Cards in LWC<\/figcaption><\/figure>\n\n\n\n<p class=\"justify\">In the above screenshot, one text box is in LWC &amp; everything else is rendered by the adaptive card. On the button click of the adaptive cards component (Learn more button), I&#8217;m displaying a toast message with the value from LWC demonstrating two-way communication between LWC &amp; adaptive cards. <\/p>\n\n\n\n<p class=\"justify\">You can run this demo code within 5 minutes by <a href=\"https:\/\/github.com\/JitendraZaa\/lwc-adaptive-cards#readme\">following the instructions mentioned here<\/a>.<\/p>\n\n\n\n<p class=\"justify\">In the below code, magic happens by payload declared in variable card at line 53 of javascript. <\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/JitendraZaa\/e5e8792658851fe64b68ce3e0413baf9.js\"><\/script>\n\n\n\n<p class=\"justify\">You can play <a href=\"https:\/\/adaptivecards.io\/designer\/\">with adaptive cards here to<\/a> decide what the JSON payload structure would look like. <\/p>\n\n\n\n<p class=\"justify\">Feel free to let me know your thoughts or feedback about using adaptive cards with LWC.<\/p>\n\n\n\n<p><strong>References :<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/adaptive-cards\/sdk\/rendering-cards\/javascript\/getting-started\">Adaptive cards documentation<\/a><\/li><li><a href=\"https:\/\/adaptivecards.io\/designer\/\">Adaptive cards playground \/ designer<\/a><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to use Microsoft adaptive cards framework in Salesforce Lightning Web Components<\/p>\n","protected":false},"author":1,"featured_media":7579,"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":[523,108,464,145,331,480],"class_list":["post-7569","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","tag-adaptive-card","tag-git","tag-lwc","tag-microsoft-2","tag-salesforce","tag-scratch-org"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2022\/05\/Adaptive-Cards-in-Salesforce-e1651985546813.png?fit=600%2C619&ssl=1","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":7140,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/using-custom-label-in-lightning-web-components-lwc\/","url_meta":{"origin":7569,"position":0},"title":"Using Custom Label in Lightning Web Components &#8211; LWC","author":"Jitendra","date":"April 4, 2020","format":false,"excerpt":"Quick Code Snippet on how to use custom label in Lightning Web Components - LWC","rel":"","context":"In &quot;Lightning Web Components&quot;","block_context":{"text":"Lightning Web Components","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/lightning-web-components\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7044,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/limitations-of-lwc\/","url_meta":{"origin":7569,"position":1},"title":"Limitations of LWC","author":"Jitendra","date":"April 22, 2020","format":false,"excerpt":"Considerations & Limitations of Lightning Web Component in Salesforce","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"LWC Not Supported","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/04\/LWC-Not-Supported.png?fit=1200%2C569&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/04\/LWC-Not-Supported.png?fit=1200%2C569&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/04\/LWC-Not-Supported.png?fit=1200%2C569&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/04\/LWC-Not-Supported.png?fit=1200%2C569&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/04\/LWC-Not-Supported.png?fit=1200%2C569&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":6908,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/point-and-click\/use-lightning-web-components-in-flow\/","url_meta":{"origin":7569,"position":2},"title":"Use Lightning Web Components in Flow","author":"Jitendra","date":"October 21, 2019","format":false,"excerpt":"Endless limit for your Salesforce application by combining Flow with Lightning Web Components (LWC)","rel":"","context":"In &quot;Lightning Web Components&quot;","block_context":{"text":"Lightning Web Components","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/lightning-web-components\/"},"img":{"alt_text":"Using LWC in Flow","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/10\/Using-LWC-in-Flow-e1571713113388.png?fit=1200%2C651&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/10\/Using-LWC-in-Flow-e1571713113388.png?fit=1200%2C651&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/10\/Using-LWC-in-Flow-e1571713113388.png?fit=1200%2C651&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/10\/Using-LWC-in-Flow-e1571713113388.png?fit=1200%2C651&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/10\/Using-LWC-in-Flow-e1571713113388.png?fit=1200%2C651&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":6880,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/get-current-user-id-in-salesforce\/","url_meta":{"origin":7569,"position":3},"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":7587,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/scan-barcode-in-lwc-natively\/","url_meta":{"origin":7569,"position":4},"title":"Scan Barcode in LWC Natively","author":"Jitendra","date":"June 6, 2022","format":false,"excerpt":"Demo & Source code of using Barcode API in Salesforce LWC","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Barcode in LWC","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2022\/06\/Barcode-in-LWC.png?fit=1119%2C722&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2022\/06\/Barcode-in-LWC.png?fit=1119%2C722&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2022\/06\/Barcode-in-LWC.png?fit=1119%2C722&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2022\/06\/Barcode-in-LWC.png?fit=1119%2C722&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2022\/06\/Barcode-in-LWC.png?fit=1119%2C722&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":7072,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/share-javascript-code-in-lightning-web-component-to-read-url-parameters\/","url_meta":{"origin":7569,"position":5},"title":"Share JavaScript Code in Lightning Web Component to Read URL Parameters","author":"Jitendra","date":"May 4, 2020","format":false,"excerpt":"Building Reusable LWC Component to read query string from URL","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":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/7569","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=7569"}],"version-history":[{"count":9,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/7569\/revisions"}],"predecessor-version":[{"id":7585,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/7569\/revisions\/7585"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media\/7579"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=7569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=7569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=7569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}