logo

NJP

Common errors in UI Page / Macro

Import · Apr 29, 2021 · article

Let's admit it, Jelly is not very intuitive.

There are tons of documentation out there on Jelly. But I've got here a few pointers that is often missed while coding in a UI Page/Macro

1. Conditional statements:

The first thing that comes to mind while coding an AND condition is &&. But watch out.

- If you write a condition statement with && in jelly conditional tags (), it will not work.

Use &&... like this:

<j:if test="${jvar_variable1 == 'value1' &amp;&amp; jvar_variable2 == 'value2'}">
Show this!
</j:if>

- Similarly, inside </strong> tags <strong>&amp;&amp;</strong> will not work.</p> <p>Use <strong>${AND} ...</strong> like this:</p> <pre><code>&lt;script&gt; var a = 1; var b = 2; if(a==&#39;1&#39; ${AND} b==&#39;2&#39;) alert(&#39;true&#39;); else alert(&#39;false&#39;); &lt;/script&gt; </code></pre> <p>- In short, <strong>&amp;</strong> cannot be used in the HTML area, use <strong>&amp;</strong> instead.</p> <p><strong>2. getValue</strong></p> <p>When accessing a field value through a GlideRecord object, the common practice is obj.fieldValue. This doesn&#39;t return anything in Jelly.</p> <p>Always use getValue() or getDisplayValue(), like:</p> <pre><code>&lt;j:if test=&quot;${jvar_incObj.next()}&quot;&gt; ${jvar_incObj.getValue(&#39;short_description&#39;)} &lt;/j:if&gt; </code></pre> <p><strong>3. Logging</strong></p> <p>Without logging, our developer life&#39;s would be tough. And without jelly logging, it would be miserable...</p> <p>gs.info only works for javascript variables(inside <g:evaluate>).</p> <p>To log a jelly value, use ${jellyVariableName}. For example:</p> <pre><code>&lt;j:set var=&quot;jvar_myJellyVariable&quot; value=&quot;I love Jelly&quot;/&gt; ${jvar_myJellyVariable} </code></pre> <p><strong>4. Caching</strong></p> <p>UI Pages and Macros have caching issues.</p> <p>If you are using them on your forms through formatters, or dictionary attributes, you might see old data rendered until you clear your cache.</p> <p>I have encountered this many times, and was forced to replace <g:evaluate> scripts to GlideAjax-Script Include designs.</p> <p>If this article helped you, mark helpful.</p> <p>Also, feel free to share your learnings in the comments below</p> <p>Long Live ServiceNow!</p>

View original source

https://www.servicenow.com/community/now-platform-articles/common-errors-in-ui-page-macro/ta-p/2328387