<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lightning Web Component &#8211; Jitendra Zaa</title>
	<atom:link href="https://www.jitendrazaa.com/blog/tag/lightning-web-component/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.jitendrazaa.com/blog</link>
	<description>AI, Salesforce, ServiceNow &#38; Enterprise Tech Guides</description>
	<lastBuildDate>Mon, 06 Jun 2022 20:19:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
<site xmlns="com-wordpress:feed-additions:1">87744916</site><atom:link rel="search" type="application/opensearchdescription+xml" title="Search Jitendra Zaa" href="https://www.jitendrazaa.com/blog/wp-json/opensearch/1.1/document" />	<item>
		<title>Scan Barcode in LWC Natively</title>
		<link>https://www.jitendrazaa.com/blog/salesforce/scan-barcode-in-lwc-natively/</link>
					<comments>https://www.jitendrazaa.com/blog/salesforce/scan-barcode-in-lwc-natively/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Mon, 06 Jun 2022 20:01:20 +0000</pubDate>
				<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Barcode]]></category>
		<category><![CDATA[Lightning Web Component]]></category>
		<category><![CDATA[LWC]]></category>
		<category><![CDATA[Video Tutorial]]></category>
		<guid isPermaLink="false">https://www.jitendrazaa.com/blog/?p=7587</guid>

					<description><![CDATA[Demo &#038; Source code of using Barcode API in Salesforce LWC]]></description>
										<content:encoded><![CDATA[
<p>Around 7 years back I have written <a href="https://www.jitendrazaa.com/blog/salesforce/barcode-decoder-in-salesforce1-very-fast-and-simple/">this post</a> which shows how Barcode scan can be supported in Visualforce and <a href="https://www.jitendrazaa.com/blog/salesforce/barcode-scanner-component-in-salesforce-lightning-video/">in aura component</a> with the help of an external javascript library <a href="https://serratus.github.io/quaggaJS/examples/">quagga</a>.</p>



<p>Salesforce recently released a <a href="https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_lightning_barcodescanner">new Barcode API </a>which now supports barcode scanning natively on LWC for mobile devices.  As it&#8217;s supported natively, we don&#8217;t need to worry about maintaining 3rd party Javascripts, Locker services, performance issues, etc. </p>



<p>At the time of writing this blog post, <a href="https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_lightning_barcodescanner_constants">around 10 different types</a> of barcodes are supported. </p>



<span id="more-7587"></span>



<p>Below source code demonstrate how simple it is to use in LWC. </p>



<ul class="wp-block-list"><li>import <em>lightning/mobileCapabilities </em></li><li>initiate scanner variable using method <em>getBarcodeScanner()</em></li><li>set <em>configuration</em> suggesting which barcodes are supported</li><li>use <em>begineCapture() </em>method top launch mobile camera</li><li>read the result or handle error</li></ul>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Using Native Barcode API in LWC - Salesforce" width="500" height="281" src="https://www.youtube.com/embed/Su-qV10uVuw?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>
</div></figure>



<script src="https://gist.github.com/JitendraZaa/577d1061637bdef7c9f7d70e99893166.js"></script>



<p><strong>Resources :</strong></p>



<ul class="wp-block-list"><li><a href="https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_lightning_barcodescanner_constants">Supported QR Code Types</a></li><li><a href="https://qr.io/?gclid=CjwKCAjw7IeUBhBbEiwADhiEMb2QVyMnnYJffsI1Ym6uVxiC4z5AarAtW2FYpx4xBLhKC1Yjh57iwBoCirMQAvD_BwE">QR Code Generator</a></li><li><a href="https://poscatch.com/barcode-generator/">Barcode Generator</a></li></ul>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/salesforce/scan-barcode-in-lwc-natively/feed/</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">7587</post-id>	</item>
		<item>
		<title>Fix Error &#8220;Cannot assign to read only property&#8221; and print Proxy Object of LWC</title>
		<link>https://www.jitendrazaa.com/blog/salesforce/how-to-show-content-of-proxy-object-in-lightning-web-component/</link>
					<comments>https://www.jitendrazaa.com/blog/salesforce/how-to-show-content-of-proxy-object-in-lightning-web-component/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Wed, 29 Jul 2020 15:56:30 +0000</pubDate>
				<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[code snippet]]></category>
		<category><![CDATA[Lightning Web Component]]></category>
		<category><![CDATA[LWC]]></category>
		<guid isPermaLink="false">https://www.jitendrazaa.com/blog/?p=7157</guid>

					<description><![CDATA[Quick code snippet to read content of Proxy object in Lightning web component &#038; resolve error Cannot assign to read only property]]></description>
										<content:encoded><![CDATA[
<p class="justify">As its <a href="https://www.jitendrazaa.com/blog/tag/code-snippet/">code-snippet</a> post, its going to be very short but frequently used.</p>



<p class="justify">Salesforce <a href="https://developer.salesforce.com/docs/component-library/documentation/en/lwc/reference_decorators">decorator @track</a> is reactive , which means if value in JavaScript changes, equivalent HTML UI would also change. You don&#8217;t need to mark primitive data types as track, as its enabled by default for them. However for complex structure / object we need to mark <a href="https://developer.salesforce.com/docs/component-library/documentation/en/lwc/reference_decorators">variable as @track</a>.</p>



<p class="justify">Whenever any JavaScript object marked as <a href="https://developer.salesforce.com/docs/component-library/documentation/en/lwc/reference_decorators">@track</a>, Salesforce wraps that object resulting in proxy object. If you try to print that on console, you would not be able to see its value.</p>



<p class="justify">That being said, there is no change in way you access it, it&#8217;s exactly like any other Javascript object we use. So, if you want to see content of proxy object, just call below method and pass your proxy object.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
//Get Javascript object equivalent to Proxy
    proxyToObj(obj){
        return JSON.parse(JSON.stringify(obj));
    }
</pre></div>


<p class="justify">Another way is, just keep another variable without <a href="https://developer.salesforce.com/docs/component-library/documentation/en/lwc/reference_decorators">@track</a> decorator to see content. However problem would be &#8211; you need to make sure to keep it in sync every time content of original <a href="https://developer.salesforce.com/docs/component-library/documentation/en/lwc/reference_decorators">@track</a> object changes. </p>



<h3 class="wp-block-heading">Fixing error &#8211; Cannot assign to read only property</h3>



<p>Above error occurs when we try to change public property on LWC marked as <strong>@api</strong></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
@api someVar
.... some code

somemethod(event){
    //error on below line - Cannot assign to read only property
     someVar.isSelected = true;
}
</pre></div>


<p>Above code block would throw an error as we are trying. to change property on object declared by @api </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
@api someVar
.... some code

somemethod(event){
    //clone @api variable using method we just created
    let tmpObj = this.proxyToObj(someVar);
     tmpObj.isSelected = true;
    //now assign this tmp variable back to original
   someVar = tmpObj;
}
</pre></div>


<p>To fix above error, use can do something like </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/salesforce/how-to-show-content-of-proxy-object-in-lightning-web-component/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">7157</post-id>	</item>
		<item>
		<title>Change Icon Color in Lightning Web Component</title>
		<link>https://www.jitendrazaa.com/blog/salesforce/change-icon-color-in-lightning-web-component/</link>
					<comments>https://www.jitendrazaa.com/blog/salesforce/change-icon-color-in-lightning-web-component/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Wed, 01 Jul 2020 00:56:40 +0000</pubDate>
				<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Fill Property]]></category>
		<category><![CDATA[Icon]]></category>
		<category><![CDATA[Lightning Web Component]]></category>
		<category><![CDATA[LWC]]></category>
		<guid isPermaLink="false">https://www.jitendrazaa.com/blog/?p=7129</guid>

					<description><![CDATA[Why fill css property not working for LWC Icon]]></description>
										<content:encoded><![CDATA[
<p>Lightning Web Component provides hundreds of standard icon to <a href="https://www.lightningdesignsystem.com/icons/">choose from this list</a>. And Lightning Web Components like <a href="https://developer.salesforce.com/docs/component-library/bundle/lightning-icon/example">lightning-icon</a> or <a href="https://developer.salesforce.com/docs/component-library/bundle/lightning-button-icon/example">button-icon</a> can be used to choose these icons.</p>



<p>If we want to change background color of these icons then only colors we have are green, yellow, red or gray as per their variant success, warning , error or inverse.</p>



<p>If you ever try to change color of these icons using fill property, you would be disappointed. Lightning Web Component used <a href="https://www.jitendrazaa.com/blog/salesforce/introduction-to-html-web-components/">shadow DOM to encapsulate components from outer world</a>, same principal here restricts us changing icon background color. </p>



<p><a href="https://developer.salesforce.com/docs/component-library/tools/playground/PlgxPx-8Q/1/edit">Try  yourself  [Not Working]</a></p>



<span id="more-7129"></span>



<p>This is what I tried that didn&#8217;t worked</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;template&gt;
    &lt;lightning-icon class=&#039;redColor&#039; icon-name=&quot;utility:connected_apps&quot; alternative-text=&quot;Connected&quot; variant=&quot;inverse&quot; size=&quot;large&quot;
            title=&quot;large size&quot;&gt;&lt;/lightning-icon&gt;
&lt;/template&gt;
</pre></div>


<p>CSS File</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: css; title: ; notranslate">
.redColor{
    fill : #FF0000;
}
</pre></div>


<h3 class="wp-block-heading">Solution &#8211; Custom Background Color for LWC Icons</h3>



<p><strong>Step 1 :</strong></p>



<p>Right click on Icon where you want to change background color and click on inspect</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="360" height="418" src="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2020/06/View-Source-code-of-LWC-Icon-as-svg.png?resize=360%2C418&#038;ssl=1" alt="" class="wp-image-7130" srcset="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2020/06/View-Source-code-of-LWC-Icon-as-svg.png?w=360&amp;ssl=1 360w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2020/06/View-Source-code-of-LWC-Icon-as-svg.png?resize=258%2C300&amp;ssl=1 258w" sizes="(max-width: 360px) 100vw, 360px" /><figcaption>View Source code of LWC Icon in Chrome</figcaption></figure></div>



<p><strong>Step 2 :</strong></p>



<p>Copy svg tag and paste in your editor and add css. DOnt forget to add div tag as parent to control icons size.</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1346" height="349" src="https://i2.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2020/06/Copy-SVG-HTML-code-of-LWC-Icon.png?fit=840%2C218&amp;ssl=1" alt="" class="wp-image-7131" srcset="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2020/06/Copy-SVG-HTML-code-of-LWC-Icon.png?w=1346&amp;ssl=1 1346w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2020/06/Copy-SVG-HTML-code-of-LWC-Icon.png?resize=300%2C78&amp;ssl=1 300w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2020/06/Copy-SVG-HTML-code-of-LWC-Icon.png?resize=1024%2C266&amp;ssl=1 1024w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2020/06/Copy-SVG-HTML-code-of-LWC-Icon.png?resize=768%2C199&amp;ssl=1 768w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2020/06/Copy-SVG-HTML-code-of-LWC-Icon.png?resize=1200%2C311&amp;ssl=1 1200w" sizes="(max-width: 1000px) 100vw, 1000px" /><figcaption>Copy SVG equivalent of LWC Icon in Chrome browser</figcaption></figure>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;template&gt;
    &lt;div class=&quot;iconContainer redColor&quot;&gt;
        &lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; id=&quot;connected_apps&quot;&gt;
        &lt;path d=&quot;M11 14.4l-1.8 8.1c-.1.5.5.8.8.4l9.7-12c.3-.3.1-.7-.3-.7h-5.2c-.4 0-.6-.5-.4-.7L18.4 2c.2-.5-.1-1.1-.6-1.1H9.6c-.5 0-.9.3-1.1.8L4.7 12.9c-.2.5.1.9.6.9h5.3c.2 0 .5.3.4.6z&quot;&gt;
        &lt;/path&gt;&lt;/svg&gt;
    &lt;/div&gt; 
&lt;/template&gt;
</pre></div>


<p>and CSS File</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
.redColor{
    fill : #FF0000;
}
.iconContainer{
    width : 50px;
    height:50px;
    display:inline-block;
}
</pre></div>


<p><a href="https://developer.salesforce.com/docs/component-library/tools/playground/PlgxPx-8Q/2/edit">Try Yourself &#8211; Working Playground URL</a></p>



<h3 class="wp-block-heading">Conclusion :</h3>



<p>It may not be exactly that you are looking for, however you can do almost any operation that <a href="https://developer.salesforce.com/docs/component-library/bundle/lightning-icon/example">lightning-icon</a> or <a href="https://developer.salesforce.com/docs/component-library/bundle/lightning-button-icon/example">button-icon</a> can do, just add onclick event handler, css properties etc and you are good to go.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/salesforce/change-icon-color-in-lightning-web-component/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">7129</post-id>	</item>
		<item>
		<title>Implement Infinite Scrolling for lightning-datatable</title>
		<link>https://www.jitendrazaa.com/blog/salesforce/implement-infinite-scrolling-for-lightning-data-table/</link>
					<comments>https://www.jitendrazaa.com/blog/salesforce/implement-infinite-scrolling-for-lightning-data-table/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Wed, 10 Jun 2020 04:06:35 +0000</pubDate>
				<category><![CDATA[Lightning Web Components]]></category>
		<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[datatable]]></category>
		<category><![CDATA[Imperative Apex]]></category>
		<category><![CDATA[Lightning Datatable]]></category>
		<category><![CDATA[Lightning Web Component]]></category>
		<category><![CDATA[LWC]]></category>
		<category><![CDATA[Wire]]></category>
		<guid isPermaLink="false">https://www.jitendrazaa.com/blog/?p=7101</guid>

					<description><![CDATA[When to use wire vs Imperative approach to call Apex in Lightning Web Component]]></description>
										<content:encoded><![CDATA[
<p class="justify">If you are thinking this post is about <strong>implementing infinite scrolling behavior</strong> for <strong><a href="https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/example">lightning-datatable</a></strong> component, then you only got half part right. Main purpose of this blog post is to compare side by side<strong> @wire vs Imperative</strong> way of calling Apex and decide which one to use when?</p>



<p>If you are looking for <a href="https://www.jitendrazaa.com/blog/salesforce/getting-started-with-lightning-web-component/">basics of LW</a>C or <a href="https://www.jitendrazaa.com/blog/salesforce/point-and-click/use-lightning-web-components-in-flow/">how to use LWC in flow</a>, <a href="https://www.jitendrazaa.com/blog/salesforce/data-exchange-between-aura-lightning-web-components-lwc-and-visualforce/">Lightning Message Service</a>, <a href="https://www.jitendrazaa.com/blog/salesforce/gravatar-reusable-lightning-web-component/">how to call Apex</a> then <a href="https://www.jitendrazaa.com/blog/tag/lightning-web-component/">follow this link</a>.</p>



<p>In this post, you would learn</p>



<ul class="wp-block-list"><li>How to call Apex in LWC using <strong>@wire</strong></li><li>How to call Apex in LWC by <strong>Imperative</strong> approach</li><li>How to call Apex on component load event equivalent to init in Aura</li><li>Why would you choose to call Apex either by <strong>@wire</strong> or <strong>Imperative</strong> Apex approach</li><li>How to bind <strong>wrapper class</strong> in <strong><a href="https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/example">Lightning-datatable</a></strong> component</li><li>How to enable infinite scrolling in <strong><a href="https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/example">Lightning-datatable</a></strong> by making server call</li></ul>



<span id="more-7101"></span>



<p>Lets start with building demo <strong> lightning-datatable</strong> component with infinite scroll effect ,  below code is self explanatory so I would skip code explanation part. <a href="https://jit29-developer-edition.na85.force.com/blogdemo/s/lwcdemo">Click here for live demo of this component and test end result yourself.</a></p>



<script src="https://gist.github.com/JitendraZaa/e45244d3f6095b3cd1d46616c0178c2b.js"></script>



<p class="justify">Initially , it seems everything working as expected, however after some scroll , try to search any employee name. You would observe that Salesforce never made 2nd Apex call as <strong>cacheable=true</strong> was mentioned in Apex. Salesforce is trying to save resources. You cannot use wire method if <strong>cacheable is not true</strong>. This is great caching feature, unfortunately it didn&#8217;t sufficed our need. </p>



<p class="justify">Let&#8217;s do one more attempt, this time we would use <strong>Imperative approach</strong> and use <strong>connectedCallback</strong> method to make Apex call on component load. @wire decorator did this automatically on component load, this time we have to make explicit call. Another change , we have done is, we marked method as <strong>cacheable=false</strong>. <a href="https://jit29-developer-edition.na85.force.com/blogdemo/s/lwcdemo">Click here for live demo of this component and test end result yourself.</a></p>



<script src="https://gist.github.com/JitendraZaa/dab45e5c47829b0d96d9f78aa7583574.js"></script>



<p>Use below code to wrap both approach in single component</p>



<script src="https://gist.github.com/JitendraZaa/b3ccd976d72e417730396a8bc6fb5e69.js"></script>



<h3 class="wp-block-heading">Problem &#8211; It is making continuous server calls even when no one is scrolling</h3>



<p class="justify"><strong>Cause 1</strong> : Parent component needs to restrict height. If parent Div does not have height in pixel then it will keep calling Apex method. <a href="https://salesforce.stackexchange.com/questions/279776/lwc-lightning-datatable-infinite-scrolling-not-working">Read more about issue here</a></p>



<p class="justify"><strong>Cause 2 </strong>: Let&#8217;s say height is 300px but we have less component that does not fit 300px then this component would still make server call continuously. In that case height needs to be automatically adjusted or disable infinite scrolling if total record in component is not enough to fill height.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/salesforce/implement-infinite-scrolling-for-lightning-data-table/feed/</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">7101</post-id>	</item>
		<item>
		<title>Share JavaScript Code in Lightning Web Component to Read URL Parameters</title>
		<link>https://www.jitendrazaa.com/blog/salesforce/share-javascript-code-in-lightning-web-component-to-read-url-parameters/</link>
					<comments>https://www.jitendrazaa.com/blog/salesforce/share-javascript-code-in-lightning-web-component-to-read-url-parameters/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Tue, 05 May 2020 01:25:31 +0000</pubDate>
				<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Lightning Web Component]]></category>
		<category><![CDATA[LWC]]></category>
		<guid isPermaLink="false">https://www.jitendrazaa.com/blog/?p=7072</guid>

					<description><![CDATA[Building Reusable LWC Component to read query string from URL]]></description>
										<content:encoded><![CDATA[
<p>In this post, we would create a Lightning Web Component that can be shared across other LWC as a JavaScript library.</p>



<p>We are going to create LWC Component to acts as a utility method to read query parameter , as shown below </p>



<span id="more-7072"></span>



<script src="https://gist.github.com/JitendraZaa/3d9f34d2fd7b0277768aa4a60590eb4e.js"></script>



<p>As you can see in above code, we just need to create a JavaScript file &amp; meta file for LWC to act as Javascript library. Also, note how function is defined using keyword export. You can have multiple export methods in same LWC that would be used as library in your project. </p>



<p>To use above method in any LWC component, follow below syntax </p>



<script src="https://gist.github.com/JitendraZaa/46a8860e99e2e96699e26a7fd89c4f98.js"></script>



<p> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/salesforce/share-javascript-code-in-lightning-web-component-to-read-url-parameters/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">7072</post-id>	</item>
		<item>
		<title>Gravatar &#8211; Reusable Lightning Web Component</title>
		<link>https://www.jitendrazaa.com/blog/salesforce/gravatar-reusable-lightning-web-component/</link>
					<comments>https://www.jitendrazaa.com/blog/salesforce/gravatar-reusable-lightning-web-component/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Sat, 02 May 2020 04:55:45 +0000</pubDate>
				<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Lightning Web Component]]></category>
		<category><![CDATA[LWC]]></category>
		<guid isPermaLink="false">https://www.jitendrazaa.com/blog/?p=7058</guid>

					<description><![CDATA[Avoid asking your users to upload pictures - Use Globally Recognized Avatar , Reusable Lightning Web Component ]]></description>
										<content:encoded><![CDATA[
<p class="justify">Wouldn&#8217;t it would be great if we can use existing avatar of user on basis of email id instead of asking user to upload their profile pic ?</p>



<p class="justify"><a href="https://en.gravatar.com/">Gravatar</a> is Globally recognized avatar used by millions of user and many websites like WordPress, github, stackoverflow etc. </p>



<h3 class="wp-block-heading"><a href="https://jit29-developer-edition.na85.force.com/blogdemo/s/lwcdemo">Click here for Live Demo &#8211; Gravatar Component on public community</a></h3>



<p class="justify">In this post, we would create a reusable lightning component which would receive below three parameters to construct avatar</p>



<ol class="wp-block-list"><li>Email address to render avatar</li><li>Size of avatar</li><li>Should image be rendered as rounded corner</li></ol>



<p class="justify">Gravatar would need MD5 equivalent of email address. There is no native Javascript library to compute MD5 hash, either we have to use Javascript library or we can use Apex to compute MD5 using <a href="https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_crypto.htm">crypto</a> class. I preferred Apex class instead of any other library a shown below</p>



<span id="more-7058"></span>



<h3 class="wp-block-heading"><strong>Apex Class &#8211; Using Crypto class to generate MD5 digest equivalent to email</strong></h3>



<script src="https://gist.github.com/JitendraZaa/44763fef2896e631a599d33cad33c652.js"></script>



<p><strong>Lightning Web Component [LWC] for Gravatar</strong></p>



<script src="https://gist.github.com/JitendraZaa/7f1253ef9ab2b0234fb2cba14118e194.js"></script>



<h3 class="wp-block-heading">Setting properties on Lightning Web Component to be used on App builder or Community</h3>



<script src="https://gist.github.com/JitendraZaa/a150273d886aaec8959f1fb351b6ea11.js"></script>



<p class="justify">Make sure to add target <em>lightningCommunity__Default</em> along with <em>lightningCommunity__Page</em> if this LWC is supposed to be used on Community.  Otherwise you would receive below error</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>The &#8216;property&#8217; tag isn&#8217;t supported for lightningCommunity__Page</p><cite>use lightningCommunity__Default to allow setting up property in Community</cite></blockquote>



<p>Also, if above LWC is supposed to be used on Lightning community, we need to add Gravatar host in Content Security Policy trusted website otherwise we would get error</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Access to resources from an unapproved, external host violates your Content Security Policy (CSP). To get access to these resources, try whitelisting the host in CSP Trusted Sites in Salesforce Setup</p></blockquote>



<h3 class="wp-block-heading">Demo Component to showcase how to use Gravatar</h3>



<p>You would not need demo component as advance as this. I&#8217;ve created this component to showcase </p>



<ol class="wp-block-list"><li>Live preview of LWC Component</li><li>How binding works in LWC [Hint &#8211; its not as easy as aura, you would need events]]</li></ol>



<script src="https://gist.github.com/JitendraZaa/5d37530330140dbb4e2ccdcce2a5b590.js"></script>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/salesforce/gravatar-reusable-lightning-web-component/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">7058</post-id>	</item>
		<item>
		<title>Using Custom Label in Lightning Web Components &#8211; LWC</title>
		<link>https://www.jitendrazaa.com/blog/salesforce/using-custom-label-in-lightning-web-components-lwc/</link>
					<comments>https://www.jitendrazaa.com/blog/salesforce/using-custom-label-in-lightning-web-components-lwc/#respond</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Sat, 04 Apr 2020 15:52:00 +0000</pubDate>
				<category><![CDATA[Lightning Web Components]]></category>
		<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[code snippet]]></category>
		<category><![CDATA[Lightning Web Component]]></category>
		<category><![CDATA[LWC]]></category>
		<guid isPermaLink="false">https://www.jitendrazaa.com/blog/?p=7140</guid>

					<description><![CDATA[Quick Code Snippet on how to use custom label in Lightning Web Components - LWC]]></description>
										<content:encoded><![CDATA[
<p>Below is code snippet on using custom labels in Lightning Web Components.</p>



<p>Assume, we have custom label named <em>welcomeText</em> and <em>exitText</em></p>



<p>In Javascript file of LWC, we have to follow below syntax </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
// CustomlabelExample.js
import { LightningElement } from &#039;lwc&#039;;
 
// Import custom labels
import welcomeText from &#039;@salesforce/label/c.welcomeText&#039;;
import exitText from &#039;@salesforce/label/c.exitText&#039;;


export default class CustomLabelExample extends LightningElement { 

    // Expose the labels to use in the template.
    label = {
        welcomeText,
        exitText
    };
}
</pre></div>


<p>Notice, how we imported label and used <strong>c.</strong> <strong> </strong>if there is no namespace. </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
import welcomeText from &#039;@salesforce/label/c.welcomeText&#039;;
</pre></div>

<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;!-- CustomlabelExample.html --&gt;
&lt;template&gt;
    Hi , {label.welcomeText} !!! 
   &lt;!-- some more code --&gt; 
   Sorry to see you going, {label.exitText}
&lt;/template&gt;
</pre></div>


<p><strong>Reference :</strong></p>



<ul class="wp-block-list"><li><a href="https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.create_labels">Official Documentation </a></li></ul>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/salesforce/using-custom-label-in-lightning-web-components-lwc/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">7140</post-id>	</item>
		<item>
		<title>Use Lightning Web Components in Flow</title>
		<link>https://www.jitendrazaa.com/blog/salesforce/point-and-click/use-lightning-web-components-in-flow/</link>
					<comments>https://www.jitendrazaa.com/blog/salesforce/point-and-click/use-lightning-web-components-in-flow/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Tue, 22 Oct 2019 02:56:44 +0000</pubDate>
				<category><![CDATA[Lightning Web Components]]></category>
		<category><![CDATA[Point and Click]]></category>
		<category><![CDATA[Flow]]></category>
		<category><![CDATA[Lightning Web Component]]></category>
		<category><![CDATA[LWC]]></category>
		<category><![CDATA[Winter 20]]></category>
		<guid isPermaLink="false">https://www.jitendrazaa.com/blog/?p=6908</guid>

					<description><![CDATA[Endless limit for your Salesforce application by combining Flow with Lightning Web Components (LWC)]]></description>
										<content:encoded><![CDATA[
<p class="justify">As part of Winter 20, we can embed <strong>Lightning Web Components</strong> in Flow. Imagine, endless possibilities for Rapid Application Development (RAD) and ease of maintaining application. </p>



<p class="justify">Few examples where this could come handy are like <a href="https://www.youtube.com/watch?v=NW8i8SEeayg">embedding lookup components (no more workaround)</a>, showing data table, capturing signature, slide shows and so on. </p>



<p class="justify">It&#8217;s time to jump into water. In this blog post, I would be creating Simple Lightning Web Component (LWC). This LWC Component would be able to get value from flow, pass value to flow, navigate to Next screen and hookup into flow <strong>validation</strong> engine to restrict next page navigation if there is error in LWC input.</p>



<p>Note : <a href="https://www.jitendrazaa.com/blog/salesforce/embed-lightning-component-in-flow/">Read this blog post to learn how to embed Aura Component in Flow</a>.</p>



<span id="more-6908"></span>



<p>Below LWC component, only has one text box</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;template&gt;
    &lt;lightning-card title=&quot;Lightning Web Component For Flow&quot; icon-name=&quot;custom:custom14&quot;&gt;
        &lt;div class=&quot;slds-m-around_medium&quot;&gt;
                &lt;lightning-input label=&quot;Message To Send&quot; type=&quot;text&quot; value={_txtBoxVal} onchange={handleChange}&gt;&lt;/lightning-input&gt;
                &lt;br /&gt; 
                &lt;lightning-button label=&quot;Publish Attribute&quot; onclick={handleClick}&gt;&lt;/lightning-button&gt;
                &lt;lightning-button label=&quot;Navigate to Next&quot; variant=&quot;brand&quot; onclick={handleNext} &gt;&lt;/lightning-button&gt; 
        &lt;/div&gt;
    &lt;/lightning-card&gt;
&lt;/template&gt;
</pre></div>


<p>Javascript of LWC component looks like </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
import { LightningElement , track, api} from &#039;lwc&#039;;
import {FlowAttributeChangeEvent, FlowNavigationNextEvent} from &#039;lightning/flowSupport&#039;;
  

export default class Lwc_TextBox extends LightningElement {
   @track _txtBoxVal = &#039;&#039;;
   @api availableActions = &#x5B;];
    

    @api 
    get txtBoxVal(){
        return this._txtBoxVal;
    }

    set txtBoxVal(val){
        this._txtBoxVal = val;
    }

    handleChange(event) { 
        this._txtBoxVal = event.target.value; 
    }

    //Change attribute on Flow
    handleClick(event) {   
        const attributeChangeEvent = new FlowAttributeChangeEvent(&#039;txtBoxVal&#039;, this._txtBoxVal); 
        this.dispatchEvent(attributeChangeEvent);   
    }

    //Hook to Flow&#039;s Validation engine
    @api
    validate() {
        if(!this._txtBoxVal.includes(&#039;oracle&#039;)) { 
            return { isValid: true }; 
            }  
        //If the component is invalid, return the isValid parameter as false and return an error message. 
        return { 
            isValid: false, 
            errorMessage:  &#039;You cannot have string oracle in String&#039; 
            };  
    }

     //Go to Next screen of Flow
    handleNext(event){ 
       const nextNavigationEvent = new FlowNavigationNextEvent();
       this.dispatchEvent(nextNavigationEvent); 
    } 
}
</pre></div>


<p class="justify">In above Javascript, one method informs parent Flow that attribute changed. Another Javascript method navigates to next page. There is another Javascript method, which hooks up to flow validation engine saying that text box cannot have word &#8216;Oracle&#8217;.</p>



<p>Metadata file of LWC component looks like</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;LightningComponentBundle xmlns=&quot;http://soap.sforce.com/2006/04/metadata&quot; fqn=&quot;lwc_TextBox&quot;&gt;
    &lt;apiVersion&gt;47.0&lt;/apiVersion&gt;
    &lt;isExposed&gt;true&lt;/isExposed&gt;
    
    &lt;targets&gt;
        &lt;target&gt;lightning__FlowScreen&lt;/target&gt;
    &lt;/targets&gt;

    &lt;targetConfigs&gt;
        &lt;targetConfig targets=&quot;lightning__FlowScreen&quot;&gt;
            &lt;property name=&quot;txtBoxVal&quot; type=&quot;String&quot;  /&gt; 
        &lt;/targetConfig&gt;
    &lt;/targetConfigs&gt;

&lt;/LightningComponentBundle&gt;
</pre></div>


<p class="justify">In LWC metadata, we are informing Salesforce platform that this component can be used in flow. </p>



<p>Its time to create <strong>Flow</strong> in Salesforce , below image shows how final flow will look like</p>



<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="437" src="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/LWC-in-Flow-Summary.png?resize=1024%2C437&#038;ssl=1" alt="LWC Component in Flow" class="wp-image-6911" srcset="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/LWC-in-Flow-Summary.png?resize=1024%2C437&amp;ssl=1 1024w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/LWC-in-Flow-Summary.png?resize=300%2C128&amp;ssl=1 300w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/LWC-in-Flow-Summary.png?resize=768%2C328&amp;ssl=1 768w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/LWC-in-Flow-Summary.png?resize=1200%2C512&amp;ssl=1 1200w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/LWC-in-Flow-Summary.png?w=1950&amp;ssl=1 1950w" sizes="auto, (max-width: 1000px) 100vw, 1000px" /><figcaption>LWC in Flow &#8211; Summary</figcaption></figure>



<p>Screen 1 would look like</p>



<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="420" src="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/LWC-Flow-Screen-1.png?resize=1024%2C420&#038;ssl=1" alt="LWC Flow Screen 1" class="wp-image-6912" srcset="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/LWC-Flow-Screen-1.png?resize=1024%2C420&amp;ssl=1 1024w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/LWC-Flow-Screen-1.png?resize=300%2C123&amp;ssl=1 300w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/LWC-Flow-Screen-1.png?resize=768%2C315&amp;ssl=1 768w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/LWC-Flow-Screen-1.png?resize=1200%2C492&amp;ssl=1 1200w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/LWC-Flow-Screen-1.png?w=2000&amp;ssl=1 2000w" sizes="auto, (max-width: 1000px) 100vw, 1000px" /><figcaption>LWC Flow Screen 1</figcaption></figure>



<p class="justify">In above screen, we are simply showing current value in variables and then changing those variables via text components. Next Assignment components are reading text box and assigning values in variable. </p>



<p class="justify">Next Screen contains LWC component and setting informing flow that which flow variable needs to be updated if attribute in flow changes </p>



<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="495" src="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.35.16-PM.png?resize=1024%2C495&#038;ssl=1" alt="Setting Output attribute from LWC to Flow" class="wp-image-6914" srcset="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.35.16-PM.png?resize=1024%2C495&amp;ssl=1 1024w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.35.16-PM.png?resize=300%2C145&amp;ssl=1 300w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.35.16-PM.png?resize=768%2C371&amp;ssl=1 768w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.35.16-PM.png?resize=1200%2C580&amp;ssl=1 1200w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.35.16-PM.png?w=2000&amp;ssl=1 2000w" sizes="auto, (max-width: 1000px) 100vw, 1000px" /><figcaption>Setting Output attribute from LWC to Flow</figcaption></figure>



<p>Output :  Screen 1</p>



<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="410" src="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.37.07-PM.png?resize=1024%2C410&#038;ssl=1" alt="" class="wp-image-6915" srcset="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.37.07-PM.png?resize=1024%2C410&amp;ssl=1 1024w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.37.07-PM.png?resize=300%2C120&amp;ssl=1 300w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.37.07-PM.png?resize=768%2C308&amp;ssl=1 768w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.37.07-PM.png?resize=1200%2C481&amp;ssl=1 1200w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.37.07-PM.png?w=1592&amp;ssl=1 1592w" sizes="auto, (max-width: 1000px) 100vw, 1000px" /><figcaption>Flow Output &#8211; Screen 1</figcaption></figure>



<p>Output : Screen 2</p>



<figure class="wp-block-image"><img data-recalc-dims="1" loading="lazy" decoding="async" width="1024" height="438" src="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.37.38-PM-1.png?resize=1024%2C438&#038;ssl=1" alt="" class="wp-image-6916" srcset="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.37.38-PM-1.png?resize=1024%2C438&amp;ssl=1 1024w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.37.38-PM-1.png?resize=300%2C128&amp;ssl=1 300w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.37.38-PM-1.png?resize=768%2C329&amp;ssl=1 768w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.37.38-PM-1.png?resize=1200%2C514&amp;ssl=1 1200w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Screen-Shot-2019-10-21-at-9.37.38-PM-1.png?w=1584&amp;ssl=1 1584w" sizes="auto, (max-width: 1000px) 100vw, 1000px" /><figcaption>Flow Output &#8211; Screen 2</figcaption></figure>



<p><strong>Resources :</strong></p>



<p><a href="https://developer.salesforce.com/docs/component-library/bundle/lightning-flow-support/documentation">LWC attributes for Flow</a></p>



<p><a href="https://developer.salesforce.com/docs/component-library/documentation/lwc/use_build_for_flow_screens">Using LWC in Flow</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/salesforce/point-and-click/use-lightning-web-components-in-flow/feed/</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">6908</post-id>	</item>
		<item>
		<title>Event Handling between Aura, Lightning Web Components (LWC) and Visualforce</title>
		<link>https://www.jitendrazaa.com/blog/salesforce/data-exchange-between-aura-lightning-web-components-lwc-and-visualforce/</link>
					<comments>https://www.jitendrazaa.com/blog/salesforce/data-exchange-between-aura-lightning-web-components-lwc-and-visualforce/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Wed, 16 Oct 2019 04:04:14 +0000</pubDate>
				<category><![CDATA[Lightning]]></category>
		<category><![CDATA[Lightning Web Components]]></category>
		<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Visualforce]]></category>
		<category><![CDATA[Lightning Message Channel]]></category>
		<category><![CDATA[Lightning Message Service]]></category>
		<category><![CDATA[Lightning Web Component]]></category>
		<category><![CDATA[LMS]]></category>
		<category><![CDATA[LWC]]></category>
		<category><![CDATA[Winter 20]]></category>
		<guid isPermaLink="false">https://www.jitendrazaa.com/blog/?p=6920</guid>

					<description><![CDATA[How to use Lightning Message Services for event handling between Lightning Web Components, Aura Components and Visualforce]]></description>
										<content:encoded><![CDATA[
<p class="justify">Few months back, <a href="https://www.jitendrazaa.com/blog/salesforce/lightning-web-component-event-handling-pub-sub/">I wrote an article</a> on how pub sub model can be used to communicate between Lightning Web Components.  In that blog post, we used external library to pass event from child to Lightning Web Components.</p>



<h2 class="wp-block-heading">Lightning Message Service</h2>



<p class="justify">In <a href="https://www.jitendrazaa.com/blog/tag/winter-20/">Winter 20</a>, Salesforce released <strong>Lightning Message Service </strong>which can be used to exchange data between Visualforce, Aura Component and Lightning Web Components.  Unlike previous process, we don&#8217;t need to import any external library like pub or sub. </p>



<p class="justify">In this blog post, I would be creating Visualforce, Aura Component and <strong>Lightning Message Service </strong>and exchanging message between all of them using Lightning Message Service.</p>



<span id="more-6920"></span>



<h2 class="wp-block-heading">Lightning Message Channel</h2>



<p class="justify">At the heart of <strong>Lightning Message Service</strong>, we have <strong>Lightning Message Channel</strong>.  Its basically name of Schema which will hold actual message. For sake of this blog post, create file <em>LMSDemoWin.messageChannel-meta.xml</em> in folder messageChannels.   </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;LightningMessageChannel xmlns=&quot;http://soap.sforce.com/2006/04/metadata&quot;&gt;
   &lt;masterLabel&gt;LMS Demo&lt;/masterLabel&gt;
   &lt;isExposed&gt;true&lt;/isExposed&gt;
   &lt;description&gt;Winter 20 - LMS Demo.&lt;/description&gt; 
&lt;/LightningMessageChannel&gt;
</pre></div>


<h2 class="wp-block-heading">Deploy Lightning Message Channel</h2>



<p class="justify">Run below SFDX command to deploy this message channel on your Salesforce Org, run push command to deploy message channel on Scratch Orgs.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: powershell; title: ; notranslate">
sfdx force:source:deploy -p &quot;force-app/main/default/messageChannels/&quot;
</pre></div>


<p class="justify">Once, Lightning Message Channel created, let&#8217;s start by creating our components and first we will create below Visualforce Page.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;apex:page&gt;
    &lt;!-- Begin Default Content REMOVE THIS --&gt;
    &lt;h1&gt;Lightning Message Services - Demo - Winter 20&lt;/h1&gt;
    &lt;div&gt;
        &lt;p&gt;Message To Send&lt;/p&gt;
        &lt;input type=&quot;text&quot; id=&quot;vfMessage&quot; /&gt;
        &lt;button onclick=&quot;publishMessage()&quot;&gt;Publish&lt;/button&gt; 
        &lt;br/&gt; 
        &lt;button onclick=&quot;subscribeMC()&quot;&gt;Subscribe&lt;/button&gt; 
        &lt;button onclick=&quot;unsubscribeMC()&quot;&gt;Unsubscribe&lt;/button&gt; 
        &lt;br/&gt;
        &lt;p&gt;Messages Received:&lt;/p&gt;
        &lt;textarea id=&quot;txtMessages&quot; rows=&quot;2&quot; style=&quot; width:100%;&quot; /&gt;
    &lt;/div&gt;
    &lt;script&gt; 
        // Load the MessageChannel token in a variable
        var lmsDemoChannel = &quot;{!$MessageChannel.LMSDemoWin__c}&quot;;
        var subscriptionToMC;
       function publishMessage() {
            const payload = {
                source: &quot;Visualforce&quot;,
                messageBody: document.getElementById(&#039;vfMessage&#039;).value
            };
            sforce.one.publish(lmsDemoChannel, payload);
        }
        function subscribeMC() {
            if (!subscriptionToMC) {
                subscriptionToMC = sforce.one.subscribe(lmsDemoChannel, onMCPublished);
            }
        }
        function unsubscribeMC() {
            if (subscriptionToMC) {
                sforce.one.unsubscribe(subscriptionToMC);
                subscriptionToMC = null;
            }
        }
        function onMCPublished(message) {
            var textArea = document.querySelector(&quot;#txtMessages&quot;);
            textArea.innerHTML = message ? &#039;Message: &#039; + message.messageBody + &#039; From: &#039; + message.source : &#039;no message payload&#039;;
        } 
    &lt;/script&gt;
&lt;/apex:page&gt;
</pre></div>


<p class="justify">In above code, we have few functions to subscribe , Unsubscribe and publish Lightning Message Channel. <em>We can follow any schema for payload however that has to be followed by all clients as well. In our case, we are passing source and actual message body.</em></p>



<p>Next component we would build would be Aura Component</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;aura:component description=&quot;testMessageAura&quot; implements=&quot;flexipage:availableForAllPageTypes&quot; access=&quot;global&quot;&gt;
    &lt;aura:attribute type=&quot;String&quot; name=&quot;myMessage&quot;/&gt;
    &lt;aura:attribute type=&quot;String&quot; name=&quot;receivedMessage&quot;/&gt;
    &lt;lightning:messageChannel type=&quot;LMSDemoWin__c&quot; aura:id=&quot;lmsDemohannel&quot; onMessage=&quot;{!c.handleReceiveMessage}&quot;/&gt;

    &lt;lightning:card title=&quot;Aura Component&quot; iconName=&quot;custom:custom18&quot;&gt;
        &lt;div class=&quot;slds-m-around_medium&quot;&gt;
            &lt;lightning:input type=&quot;text&quot; value=&quot;{!v.myMessage}&quot; label=&quot;Message To Send&quot;/&gt;
            &lt;lightning:button label=&quot;Publish&quot; onclick=&quot;{! c.handleClick}&quot;/&gt;
            &lt;br/&gt;
            &lt;br/&gt;
            &lt;p&gt;Latest Received Message&lt;/p&gt;
            &lt;lightning:formattedText value=&quot;{!v.receivedMessage}&quot;/&gt;
        &lt;/div&gt;
    &lt;/lightning:card&gt;
&lt;/aura:component&gt;	

</pre></div>


<p>Controller of Aura Component</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
({
    handleClick: function(component, event, helper) {
        let myMessage = component.get(&quot;v.myMessage&quot;);
        const payload = {
            source: &quot;Aura Component&quot;,
            messageBody: myMessage
        };
        component.find(&quot;lmsDemohannel&quot;).publish(payload);
    },
    handleReceiveMessage: function (component, event, helper) {
        if (event != null) {
            const message = event.getParam(&#039;messageBody&#039;);
            const source = event.getParam(&#039;source&#039;);

            component.set(&quot;v.receivedMessage&quot;, &#039;Message: &#039; + message + &#039;. Sent From: &#039; + source);
        }
    }
});
</pre></div>


<p class="justify">As you can see in above Aura Component, we are not doing anything special to subscribe or unsubscribe <strong>Lightning Message Channel</strong>. Aura Component by default handles those operations once we declare them. </p>



<p>Last component we would be creating is Lightning Web Component </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;template&gt;
    &lt;lightning-card title=&quot;LWC&quot; icon-name=&quot;custom:custom18&quot;&gt;
        &lt;div class=&quot;slds-m-around_medium&quot;&gt;
            &lt;lightning-input label=&quot;Message To Send&quot; type=&quot;text&quot; value={_msg} onchange={handleChange}&gt;&lt;/lightning-input&gt;
            &lt;lightning-button label=&quot;Publish&quot; onclick={handleClick}&gt;&lt;/lightning-button&gt;
            &lt;br&gt;
            &lt;lightning-button label=&quot;Subscribe&quot; onclick={handleSubscribe}&gt;&lt;/lightning-button&gt;
            &lt;lightning-button label=&quot;Unsubscribe&quot; onclick={handleUnsubscribe}&gt;&lt;/lightning-button&gt;
            &lt;p&gt; Message Received&lt;/p&gt;
            &lt;lightning-formatted-text value={receivedMessage}&gt;&lt;/lightning-formatted-text&gt;
        &lt;/div&gt;
    &lt;/lightning-card&gt;
&lt;/template&gt;
</pre></div>


<p>Javascript of LWC</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
import { LightningElement, track} from &#039;lwc&#039;;
import { publish,createMessageContext,releaseMessageContext, subscribe, unsubscribe } from &#039;lightning/messageService&#039;;
import lmsDemoMC from &quot;@salesforce/messageChannel/LMSDemoWin__c&quot;;
export default class LMS_MessageSenderReceiverLWC extends LightningElement {
    @track _msg = &#039;&#039;;
    @track receivedMessage = &#039;&#039;;
    channel;
    context = createMessageContext();

    constructor() {
        super();
    }
   
    handleSubscribe() {
        const parentPage = this;
        this.channel = subscribe(this.context, lmsDemoMC, function (event){
            if (event != null) {
                const message = event.messageBody;
                const source = event.source;
                parentPage.receivedMessage = &#039;Message: &#039; + message + &#039;. Sent From: &#039; + source;
            }
        });
    }

    handleUnsubscribe() {
        unsubscribe(this.channel);
    }

    handleChange(event) { 
        this._msg = event.target.value;
    }

    handleClick() {  
        const payload = {
            source: &quot;Lightnign Web Component&quot;,
            messageBody: this._msg
        }; 
        publish(this.context, lmsDemoMC, payload);
    } 

    disconnectedCallback() {
        releaseMessageContext(this.context);
    }
}
</pre></div>


<p class="justify">Once we embed all three components (Visualforce, Aura and Lightning Web Components), we would be able to see Lightning Message Service in action as shown in below image :</p>



<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="516" height="1024" src="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Lightning-Message-Service-in-Salesforce.png?resize=516%2C1024&#038;ssl=1" alt="" class="wp-image-6921" srcset="https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Lightning-Message-Service-in-Salesforce.png?resize=516%2C1024&amp;ssl=1 516w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Lightning-Message-Service-in-Salesforce.png?resize=151%2C300&amp;ssl=1 151w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Lightning-Message-Service-in-Salesforce.png?resize=768%2C1523&amp;ssl=1 768w, https://i0.wp.com/www.jitendrazaa.com/blog/wp-content/uploads/2019/10/Lightning-Message-Service-in-Salesforce.png?w=840&amp;ssl=1 840w" sizes="auto, (max-width: 516px) 100vw, 516px" /><figcaption>Lightning Message Service in Salesforce</figcaption></figure></div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/salesforce/data-exchange-between-aura-lightning-web-components-lwc-and-visualforce/feed/</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">6920</post-id>	</item>
		<item>
		<title>Lightning Web Component Event Handling &#8211; Pub Sub</title>
		<link>https://www.jitendrazaa.com/blog/salesforce/lightning-web-component-event-handling-pub-sub/</link>
					<comments>https://www.jitendrazaa.com/blog/salesforce/lightning-web-component-event-handling-pub-sub/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Fri, 21 Jun 2019 20:31:58 +0000</pubDate>
				<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[Lightning Web Component]]></category>
		<category><![CDATA[LWC]]></category>
		<guid isPermaLink="false">https://www.jitendrazaa.com/blog/?p=6848</guid>

					<description><![CDATA[How to handle events in Lightning Web Components between nested and non nested Components ]]></description>
										<content:encoded><![CDATA[
<p>It is very common to come across use case of communicating between <a href="https://www.jitendrazaa.com/blog/tag/lightning-web-component/">Lightning Web Components</a>.</p>



<p>If you are not interested on code explanation and directly want to jump on live demo , <a href="https://developer.salesforce.com/docs/component-library/tools/playground/4gS3Ies8h/27/edit">check this playground</a>. </p>



<h3 class="wp-block-heading">Communicate between Nested Lightning Web Components</h3>



<p>This is one of most simplest and common use case, where parent <a href="https://www.jitendrazaa.com/blog/tag/lightning-web-component/">LWC</a> wants to react on event produced by child <a href="https://www.jitendrazaa.com/blog/tag/lightning-web-component/">LWC</a>. </p>



<p>In above image, <em>Model 3 </em>is child (nested component) of <em>Tesla. </em></p>



<script src="https://gist.github.com/JitendraZaa/c87c616df3fbf1112a9523a31c50abca.js"></script>



<span id="more-6848"></span>



<p>In above<em>, Model 3 </em>component, custom event <em>modelclick </em>is raised using class <em><strong>CustomEvent</strong>.</em></p>



<p>Lets, see how Parent Component <em><strong>Tesla </strong></em>is defined.</p>



<script src="https://gist.github.com/JitendraZaa/f75b14dc45d9ef5001f53974b7934c4e.js"></script>



<p>In above parent component <em><strong>tesla</strong></em>, event raised by child component is handled just like any other events in Javascript (bubble event). In <em><strong>onmodelclick</strong> </em>function , Tesla is also raising new event with same name &#8211; <em><strong>modelclick</strong>. </em></p>



<p><strong>That concludes, how communication happens between Parent child Lightning Web Components. </strong></p>



<h3 class="wp-block-heading">Lightning Web Component Event handling &#8211; Pub Sub Library</h3>



<p>In above image, if we want Tesla event to be handled by Honda Component, traditional bubble based event handling will not work.</p>



<p>You can either use <a href="https://github.com/trailheadapps/lwc-recipes/tree/master/force-app/main/default/lwc/pubsub">Salesforce library of Pub Sub</a>, or Simple Library produced by <a href="https://github.com/TheVishnuKumar/one-pub-sub-lwc">Vishnu</a>.</p>



<p>I&#8217;m going to use library created by Vishnu. Below is Javascript code for pub-sub library</p>



<script src="https://gist.github.com/JitendraZaa/ffc43606fafb682365ec28177962fe81.js"></script>



<p>Now, we have added above javascript as part of LWC, lets create a Lightning Web Component named Vehicle which will fire pub-sub event.</p>



<script src="https://gist.github.com/JitendraZaa/bd9183a99e4e23507bafb0f951ced038.js"></script>



<p>Note, how pubsub js is imported on line 2 of Vehicle.js. On line 7, we are firing event using pub-sub library.  </p>



<p>Lets create a Lightning Web Component which will handle event generated by pub sub library. </p>



<script src="https://gist.github.com/JitendraZaa/8f31763afbcd129d37475b4a089156e0.js"></script>



<p>As seen in above code, register method is used to handle event. Also note, how unique event identifier is used to fire and register event.</p>



<p>Feel free to comment and share your feedback. Happy coding !!!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/salesforce/lightning-web-component-event-handling-pub-sub/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">6848</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: www.jitendrazaa.com @ 2026-04-18 00:51:30 by W3 Total Cache
-->