<?xml version= "1.0" encoding= "utf-8" standalone= "yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Javascript on Spinning Code</title>
    <link>https://spinningcode.org/tags/javascript/</link>
    <description>Recent content in Javascript on Spinning Code</description> <generator>Hugo -- 0.157.0</generator>
    <language>en-US</language> <lastBuildDate>Sun, 31 Jan 2021 19:37:04 +0000</lastBuildDate> <atom:link href= "https://spinningcode.org/tags/javascript/feed.xml" rel= "self" type= "application/rss+xml" /> <item>
      <title>Salesforce Lightning Web Components with URL Parameters</title>
      <link>https://spinningcode.org/2021/01/salesforce-lightning-web-components-with-url-parameters/</link>
      <pubDate>
        Sun, 31 Jan 2021 19:37:04 +0000
      </pubDate> <guid
        isPermaLink="false">https://spinningcode.org/?p=1579</guid>  <description>A security change invalidated nearly all tutorials on LWC URL parameters. This one fixes that.</description> <content:encoded><![CDATA[<p>A couple weeks ago I needed to create a Salesforce Lightning Web Component (LWC) that pulls values from URL parameters. While the process is very simple it turns out the vast majority of examples on the web are out of date due to a security update Salesforce made sometime last year – and so I spent a frustrating afternoon throwing ideas at the wall until a colleague stumbled into a comment on a blog post that was an incorrect example by a highly trusted expert noting the needed fix.  So, in the hopes of shortening the search for anyone else trying to get this to work, I’m offering an example that works – at least as of this writing.</p>
<p>To be fair the <a href="https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.use_navigate_add_params_url">official docs are correct</a> but it is easy to look passed an important detail: <em>if you do not put a namespace on your value the parameter will be deleted.</em></p>
<p>That change was the security update, before that you could have any value as your parameter name now you have to have __ (two underscores) in the name.  Officially the docs say that in the left side of those underscores you should have the namespace of your package or a “c” for unpackaged code. As far as I can tell at least in sandboxes and trailhead orgs you can have anything you want as long as there are characters before and after the __ (which kinda makes sense since package developers need to be able to write and test their JavaScript before they build their package).</p>
<p>So your final URLs will look something like:
<code>https://orgname.my.salesforce.com/lightning/r/Contact/0034x000009Xy5gAAC/view?c__myUrlParameter=12345</code></p>
<h2 id="basic-lwc">Basic LWC</h2>
<p>Now with that main tip out of the way on to a full example.</p>
<p>My assumption going into this is that you know how to create a very basic Hello World quality LWC. If not, start with the <a href="https://trailhead.salesforce.com/content/learn/projects/quick-start-lightning-web-components/create-a-hello-world-lightning-web-component">Trailhead Hello World example project</a>.</p>
<ol>
<li>
<p>Create a new component to work with, mine will be very simple to help keep the details clean, but you can fold this into more interesting code bases.</p>
</li>
<li>
<p>Update the component’s meta.xml file to set <code>isExposed</code> to true, and at least a target of <code>lighning__RecordPage</code> (although any target will do if you know how to use it), and configure the target to connect to <code>Contact</code> (although again any settings you know how to use are fine here).</p>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#75715e">&lt;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34; ?&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;LightningComponentBundle</span> <span style="color:#a6e22e">xmlns=</span><span style="color:#e6db74">&#34;http://soap.sforce.com/2006/04/metadata&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">&lt;apiVersion&gt;</span>50.0<span style="color:#f92672">&lt;/apiVersion&gt;</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">&lt;isExposed&gt;</span>true<span style="color:#f92672">&lt;/isExposed&gt;</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">&lt;description&gt;</span>Example Lightning Web Componant to read URL parameters.<span style="color:#f92672">&lt;/description&gt;</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">&lt;targets&gt;</span>
</span></span><span style="display:flex;"><span>       <span style="color:#f92672">&lt;target&gt;</span>lightning__RecordPage<span style="color:#f92672">&lt;/target&gt;</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">&lt;/targets&gt;</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">&lt;targetConfigs&gt;</span>
</span></span><span style="display:flex;"><span>       <span style="color:#f92672">&lt;targetConfig</span> <span style="color:#a6e22e">targets=</span><span style="color:#e6db74">&#34;lightning__RecordPage&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>           <span style="color:#f92672">&lt;objects&gt;</span>
</span></span><span style="display:flex;"><span>               <span style="color:#75715e">&lt;!-- This is setup to run on contact but you could use any sObject--&gt;</span>
</span></span><span style="display:flex;"><span>               <span style="color:#f92672">&lt;object&gt;</span>Contact<span style="color:#f92672">&lt;/object&gt;</span>
</span></span><span style="display:flex;"><span>           <span style="color:#f92672">&lt;/objects&gt;</span>
</span></span><span style="display:flex;"><span>       <span style="color:#f92672">&lt;/targetConfig&gt;</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">&lt;/targetConfigs&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/LightningComponentBundle&gt;</span>
</span></span></code></pre></div><ol start="3">
<li>In your JS file beyond the main <code>LighningElement</code> you need to add imports for <code>wire</code>, <code>track</code>, and <code>CurrentPageReference</code> from the navigation library:</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-js" data-lang="js"><span style="display:flex;"><span><span style="color:#66d9ef">import</span> { <span style="color:#a6e22e">LightningElement</span>, <span style="color:#a6e22e">wire</span>, <span style="color:#a6e22e">track</span> } <span style="color:#a6e22e">from</span> <span style="color:#e6db74">&#34;lwc&#34;</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">import</span> { <span style="color:#a6e22e">CurrentPageReference</span> } <span style="color:#a6e22e">from</span> <span style="color:#e6db74">&#34;lightning/navigation&#34;</span>;
</span></span></code></pre></div><ol start="4">
<li>Add a tracked value you want to display inside the main class:</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-js" data-lang="js"><span style="display:flex;"><span><span style="color:#66d9ef">export</span> <span style="color:#66d9ef">default</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Parameter_reader</span> <span style="color:#66d9ef">extends</span> <span style="color:#a6e22e">LightningElement</span> {
</span></span><span style="display:flex;"><span>  <span style="color:#960050;background-color:#1e0010">@</span><span style="color:#a6e22e">track</span> <span style="color:#a6e22e">displayValue</span>;
</span></span></code></pre></div><ol start="5">
<li>Next use the wire decorator to connect CurrentPageReference’s <code>getStateParameters</code> to your own code to get an use the URL parameters:</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-js" data-lang="js"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">@</span><span style="color:#a6e22e">wire</span>(<span style="color:#a6e22e">CurrentPageReference</span>)
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">getStateParameters</span>(<span style="color:#a6e22e">currentPageReference</span>) {
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">if</span> (<span style="color:#a6e22e">currentPageReference</span>) {
</span></span><span style="display:flex;"><span>   <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">urlValue</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">currentPageReference</span>.<span style="color:#a6e22e">state</span>.<span style="color:#a6e22e">c__myUrlParameter</span>;
</span></span><span style="display:flex;"><span>   <span style="color:#66d9ef">if</span> (<span style="color:#a6e22e">urlValue</span>) {
</span></span><span style="display:flex;"><span>     <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">displayValue</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">`URL Value was: </span><span style="color:#e6db74">${</span><span style="color:#a6e22e">urlValue</span><span style="color:#e6db74">}</span><span style="color:#e6db74">`</span>;
</span></span><span style="display:flex;"><span>   } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>     <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">displayValue</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">`URL Value was not set`</span>;
</span></span><span style="display:flex;"><span>   }
</span></span><span style="display:flex;"><span> }
</span></span></code></pre></div><p>From the code sample above you can see that we’re getting the values from currentPageReferences’s state child object, and then attaching them to our tracked value we created in step four.</p>
<ol start="6">
<li>Update the HTML file to display your value ideally leveraging the SLDS along the way:</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;template&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">&lt;div&gt;</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">&lt;lightning-card</span> <span style="color:#a6e22e">title=</span><span style="color:#e6db74">&#34;Url Sample&#34;</span> <span style="color:#a6e22e">icon-name=</span><span style="color:#e6db74">&#34;custom:custom14&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>     <span style="color:#f92672">&lt;div</span> <span style="color:#a6e22e">class=</span><span style="color:#e6db74">&#34;slds-m-around_medium&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>       <span style="color:#f92672">&lt;p&gt;</span>{displayValue}<span style="color:#f92672">&lt;/p&gt;</span>
</span></span><span style="display:flex;"><span>     <span style="color:#f92672">&lt;/div&gt;</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">&lt;/lightning-card&gt;</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">&lt;/div&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/template&gt;</span>
</span></span></code></pre></div><ol start="7">
<li>
<p>Deploy all this code to your org.</p>
</li>
<li>
<p>Go to a contact record, and edit the page. Add your new competent to the side bar. Save and activate the page.</p>
</li>
<li>
<p>Return to the record page, the component should appear and say &ldquo;URL Value was not set&rdquo;.</p>
</li>
<li>
<p>In the address bar add to the end of the url: <code>?c__myUrlParameter=Hello</code>, and reload the page, the component should now read &ldquo;URL Value was Hello&rdquo;.</p>
</li>
</ol>
<p><figure>
  <a href="/wp-content/uploads/2021/01/Screen-Shot-2021-01-31-at-12.48.47-PM.png" target="_blank" rel="noopener noreferrer">

    
    
    
    
    

    
    











<noscript>
  <img class="rcf-image" src="/wp-content/uploads/2021/01/Screen-Shot-2021-01-31-at-12.48.47-PM.png" alt="A screenshot of the sample component displaying the provided text of &amp;ldquo;hello&amp;rdquo;." loading="lazy" />
</noscript>

<img class="rcf-image lazyload show-if-js"
     data-srcset="/wp-content/uploads/2021/01/Screen-Shot-2021-01-31-at-12.48.47-PM.png 526w"
     data-src="/wp-content/uploads/2021/01/Screen-Shot-2021-01-31-at-12.48.47-PM.png"
     src="data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAAA/CAIAAACnwFc8AAANe0lEQVR4nOxc6XLbSJKurAsnT8luu2NievcJdt//MTZifu2f/eHpkNeSzBNHnROoBCCQAmXJbVnSmF9EtymgqpCVmZUnSO69J2e8HOhLE/CrgzvnXpqGXxq8qmpCIHx&#43;2BbB2MVvmi949EjyODL6kT/Wch49&#43;v6fj2TO/WEPr0z4/99uH7Ob&#43;094JA&#43;eJIGXwlP5PTr98fo4HMY/fdm1kz0J/thDg/ZKOw0eIquZ2K/ajSRwR9ZwYjfYHxMWxvfPHd7Hi7iKJx5IIM93D4RuTdKucEDPIaXDpw33CGPHe7j98UgFxs3CyP6O9jIA35Rhvx7p9t02YPhUgPFn&#43;S6E8u2OYTD&#43;bquDzYP33vmeewd7aWZ1THGDHbS3whLed/rRjCHD6&#43;G50Mn4G7oIhNBu8kBzun11mwI4aRZh1CYEan3LmxOPPmQm98CcO2AK4J6HbEORH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IkEzDMxTYUU5zDoeTEiAEpJLOg8lxfTOJKvtI/xb4N/BQAA//96s&#43;GS&#43;OdZtwAAAABJRU5ErkJggg=="
     data-sizes="auto"
     width="526" alt="A screenshot of the sample component displaying the provided text of &amp;ldquo;hello&amp;rdquo;."
     loading="lazy" />


</a>

  
  
</figure></p>
<h2 id="what-about-sending-the-value-to-apex">What about sending the value to APEX?</h2>
<p>Now, let’s go one step further and send this parameter over the APEX and post a response.</p>
<ol>
<li>Create an APEX class, and create a public static method using the <code>AuraEnabled</code> decorator.</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-java" data-lang="java"><span style="display:flex;"><span> <span style="color:#a6e22e">@AuraEnabled</span>(cacheable<span style="color:#f92672">=</span><span style="color:#66d9ef">true</span>)
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> String <span style="color:#a6e22e">reflectValue</span>(String value) {
</span></span><span style="display:flex;"><span>     <span style="color:#75715e">// Really you should do something useful here.</span>
</span></span><span style="display:flex;"><span>     <span style="color:#66d9ef">return</span> value;
</span></span><span style="display:flex;"><span> }
</span></span></code></pre></div><p>In this case we’re starting with a method that just passes back the same string it was handed, but obviously you can do whatever you want here.</p>
<p><em>BE CAREFUL ABOUT SECURITY!</em></p>
<p>If you take an ID as your parameter make sure you are thinking about what happens when someone sends an ID for an object they should not see, is for an object other than the type you expected, and other similar things. The platform can help you but security is your job here, take it seriously!</p>
<ol start="2">
<li>
<p>Create <em>good</em> tests for your class, and deploy the code.</p>
</li>
<li>
<p>Import the new function into your JS file:</p>
</li>
</ol>
<p><code>import reflectValue from &quot;@salesforce/apex/valueReflection.reflectValue&quot;;</code></p>
<p>Update the <code>getStateParameter</code> handler we wrote before to call this function as a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">JavaScript promise</a>:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-js" data-lang="js"><span style="display:flex;"><span>  <span style="color:#a6e22e">getStateParameters</span>(<span style="color:#a6e22e">currentPageReference</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> (<span style="color:#a6e22e">currentPageReference</span>) {
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">urlValue</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">currentPageReference</span>.<span style="color:#a6e22e">state</span>.<span style="color:#a6e22e">c__myUrlParameter</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">if</span> (<span style="color:#a6e22e">urlValue</span>) {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">reflectValue</span>({ <span style="color:#a6e22e">value</span><span style="color:#f92672">:</span> <span style="color:#a6e22e">urlValue</span> })
</span></span><span style="display:flex;"><span>          .<span style="color:#a6e22e">then</span>((<span style="color:#a6e22e">result</span>) =&gt; {
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">displayValue</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">`URL Value was: </span><span style="color:#e6db74">${</span><span style="color:#a6e22e">result</span><span style="color:#e6db74">}</span><span style="color:#e6db74">`</span>;
</span></span><span style="display:flex;"><span>          })
</span></span><span style="display:flex;"><span>          .<span style="color:#66d9ef">catch</span>((<span style="color:#a6e22e">error</span>) =&gt; {
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">displayValue</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">`Error during processing: </span><span style="color:#e6db74">${</span><span style="color:#a6e22e">error</span><span style="color:#e6db74">}</span><span style="color:#e6db74">`</span>;
</span></span><span style="display:flex;"><span>          });
</span></span><span style="display:flex;"><span>      } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">displayValue</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">`URL Value was not set`</span>;
</span></span><span style="display:flex;"><span>      }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>  }
</span></span></code></pre></div><ol start="4">
<li>That’s it! Deploy your code and reload the page, and your values should pass through to APEX, come back and get displayed.</li>
</ol>
<p><a href="https://github.com/acrosman/lwc_example_url_parameters">The complete SFDX project for this example is up on Github.</a></p>
]]></content:encoded> </item> <item>
      <title>Simple Electron Starter</title>
      <link>https://spinningcode.org/2020/08/simple-electron-starter/</link>
      <pubDate>
        Sun, 02 Aug 2020 22:30:31 +0000
      </pubDate> <guid
        isPermaLink="false">https://spinningcode.org/?p=1450</guid>  <description>A simple project template for starting Electron Projects with a good security baseline.</description> <content:encoded><![CDATA[<p>Earlier today I push my <a href="https://github.com/acrosman/ElectronSimpleStarter">Electron Simple Starter</a> to Github. It has dependencies only to Electron, Electron Debug, and ESlint (but no specific settings, you can add those yourself). All the basic pieces are in place to encourage good security practices. It will run without warnings or errors, and puts in place all plumbing you need for their current inter-process communication, default overrides, and process sandboxing to help you write a secure app.</p>
<p>Off and on I&rsquo;ve been playing at writing simple programs in JavaScript using <a href="https://www.electronjs.org/">Electron</a>. As a long-time web developer the idea of writing a web app that can be compiled to an native application across a web swath of major operating systems has massive appeal.</p>
<p>But when I started to write Electron apps to scratch various itches, I was quickly annoyed at the number of security warnings I got when following the project tutorials. The PHP community used to ignore bad security in tutorials to the <a href="https://web.archive.org/web/20110412103745/http://w3fools.com">detriment of web</a>, so it bugs me to see that behavior crop up in other places. With my most recent side project – a <a href="https://github.com/acrosman/electronForce">Salesforce API exploration tool</a> – I finally decided I was overdue in figuring out how to resolve all the warnings the basic quick start from the main project triggers. Using a combination of <a href="https://github.com/reZach/secure-electron-template">this secure electron project template</a> and <a href="https://www.electronjs.org/docs/tutorial/security">the main project&rsquo;s security tutorial</a> I finally got there.</p>
<p>Then I wanted to scratch a different itch, which hasn&rsquo;t really gone anywhere, cause all the work to get started in a secure way felt like a mountain to climb again. The secure electron template is too opinionated for me to use directly for a small toy project, and <a href="https://github.com/acrosman/electronForce">ElectronForce</a> has all kinds of other code already in place, so I spun my wheels for awhile. Then I finally bit the bullet and extracted the bits I needed for the next project. Once I realized I had a fairly clean baseline, I figured I would probably want it again soon (I create projects frequently to explore an idea or scratch an itch) so I created a new project template that sets the baseline and is fairly unopinionated. My goal is to have something I can grab to start writing a simple application quickly.</p>
<p>While I&rsquo;ve made some effort to secure this project baseline, security is always the project developer&rsquo;s responsibility – you are still responsible for your project&rsquo;s security. Please feel free to use my template, but understand that you still have to follow best practices to keep your app secure and those will change over time. The Electron project will inevitably evolve and change their security system again, and I will not promise to keep up. Also this is a template, not a library, when some future Electron adds features I didn&rsquo;t use, you&rsquo;ll need to update your project.</p>
<p>If some specific piece of this template confuses you, please feel free to ask either here or on Github. I can try to explain as best I am able, and maybe you&rsquo;ll inspire another post sometime in the future to cover it in depth.</p>
]]></content:encoded> </item> </channel>
</rss>
