AnirudhKumar
Mega Sage
Mega Sage

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 (<j:if>), it will not work.

Use &amp;&amp;... like this:

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

- Similarly, inside <script> tags && will not work.

Use ${AND} ... like this:

<script>
	var a = 1;
	var b = 2;

	if(a=='1' ${AND}  b=='2')
	  alert('true');
	else
	  alert('false');
</script>

- In short, & cannot be used in the HTML area, use &amp; instead.

 

 

2. getValue

 

When accessing a field value through a GlideRecord object, the common practice is obj.fieldValue. This doesn't return anything in Jelly.

Always use getValue() or getDisplayValue(), like:

<j:if test="${jvar_incObj.next()}">
	${jvar_incObj.getValue('short_description')}
</j:if>

 

 

3. Logging

 

Without logging, our developer life's would be tough. And without jelly logging, it would be miserable...

gs.info only works for javascript variables(inside <g:evaluate>).

To log a jelly value, use ${jellyVariableName}. For example:

<j:set var="jvar_myJellyVariable" value="I love Jelly"/>
${jvar_myJellyVariable}

 

 

4. Caching

 

UI Pages and Macros have caching issues.

If you are using them on your forms through formatters, or dictionary attributes, you might see old data rendered until you clear your cache.

I have encountered this many times, and was forced to replace <g:evaluate> scripts to GlideAjax-Script Include designs.

 

 

If this article helped you, mark helpful.

Also, feel free to share your learnings in the comments below

 

 

Long Live ServiceNow!

Version history
Last update:
‎04-29-2021 08:59 AM
Updated by: