Jelly tags
Summarize
Summary of Jelly tags
Jelly is a scripting language used in ServiceNow to convert XML into HTML, enabling dynamic web content generation within the platform. It is integral for customizing UI Pages, Forms, Lists, and other rendered components. This guide covers key Jelly tags that ServiceNow customers can use to control page logic, variable handling, server-side scripting, and UI behavior.
Show less
Key Features
- Control Flow Tags:
- if: Executes content if a condition is true. Does not support else or elseif; instead, use
choose/when/otherwisefor branching logic. - while: Executes a block repeatedly while a condition is true, useful for iterating over GlideRecord query results.
- choose/when/otherwise: Implements if-elseif-else style logic for multiple conditions in a readable block structure.
- if: Executes content if a condition is true. Does not support else or elseif; instead, use
- Variable Management Tags:
- set: Assigns a value to a variable, often prefixed with
jvarfor consistency. - setif: Conditional variable assignment similar to a ternary operator, setting a variable based on a test condition.
- set: Assigns a value to a variable, often prefixed with
- Server-side Scripting:
- evaluate: Executes server-side JavaScript within Jelly, allowing access to GlideRecord and other server APIs, returning results into Jelly variables for further use.
- UI and Translation Support:
- macroinvoke: Calls UI Macros defined in the database, enabling reuse of UI components with parameters passed as variables.
- messages: Caches translation messages on pages to optimize localization performance.
- Debugging and Security:
- breakpoint: Logs current Jelly variables and their values to the system log, aiding debugging.
- noescape: Disables automatic HTML output escaping for contained expressions, but use cautiously to avoid security vulnerabilities.
- Next Experience UI Support:
- ifpolaris, then, else: Conditional tags to render content based on whether the Next Experience UI is enabled, facilitating UI adaptation.
Practical Use and Benefits
For ServiceNow customers, Jelly tags provide extensive control over how dynamic content is displayed and interacted with on the platform. By leveraging these tags, you can:
- Implement complex conditional logic and loops to tailor UI components dynamically based on data and context.
- Integrate server-side scripts seamlessly with UI rendering, enabling powerful data-driven interfaces.
- Reuse UI components efficiently with macros and simplify localization with message caching.
- Debug Jelly scripts effectively by inspecting variable states.
- Adapt UI presentations for different platform experiences, ensuring compatibility with the Next Experience UI.
Understanding and applying these Jelly tags helps customize and extend ServiceNow interfaces, improving user experience and operational efficiency.
Use Jelly to turn XML into HTML.
Watch these introductory videos to learn about using Jelly in the ServiceNow AI Platform.
Jelly Tags
- if
- Description: The
iftag is just what it looks like, aniftag. This is like anifstatement in any programming language, but keep in mind that there is noelseiftag and noelsetag. If you want to create that kind of structure, try thechoose/when/otherwisesyntax. - Parameters:
test- The condition to evaluate in order to determine if the block will execute. - Example:
<g:evaluate var="jvar_now_GR" object="true"> var now_GR = new GlideRecord("incident"); now_GR.addQuery("active", true); now_GR.query(); now_GR; </g:evaluate> <j:if test="${!jvar_now_GR.hasNext()}"> We did not find any active incidents. </j:if> <j:if test="${jvar_now_GR.next()}"> We found ${jvar_now_GR.getRowCount()} active incidents. </j:if>
- Description: The
- while
- Description: The
whiletag does a while loop. - Parameters:
test- The condition to evaluate in order to determine if the statement will loop through. This should be an expression enclosed in${}or$[]that evaluates to true or false. - Example:
<g:evaluate var="jvar_now_GR" object="true"> var now_GR = new GlideRecord("incident"); now_GR.addQuery("active", true); now_GR.query(); now_GR; </g:evaluate> <j:while test="${jvar_now_GR.next()}"> <a href="incident.do?sys_id=${jvar_now_GR.getValue('sys_id')}">${jvar_now_GR.getValue('number')}</a> </j:while>
- Description: The
- set
- Description: The
settag sets a variable. - Parameters:
var- The variable to set. Often the system prefixes these variables withjvar_for consistency.value- The value to setvarto. This is often an expression enclosed in${}or$[].defaultValue- If the value results to null or empty, this value is put into thevar.
- Example:
<j:set var="jvar_incident_number" value="${jvar_now_GR.getValue('number')}"/>
- Description: The
- set_if
- Description: The
set_iftag sets a variable based on a test. This tag is similar to theternaryoperator in other programming languages (var = <test> ? <if_true> : <if_false>). - Parameters:
var- The variable to set. Often the system prefixes these variables withjvar_for consistency.test- The condition to evaluate in order to determine if the statement will evaluate the true value or the false value. This should be an expression enclosed in${}or$[]that evaluates to true or false.true- The value to set the variable to iftestevaluates totrue. This parameter is optional, so if the field is blank, and if test evaluates to true, the variable is left blank.false- The value to set the variable to iftestevaluates to false. This parameter is optional, so if the field is blank, and if test evaluates to false, the variable will be left blank.
- Description: The
- choose
- Description: The
choosetag starts a choose block of code. This is similar to theif-elseif-elsekind of syntax in most programming languages. With achoosetag, you can usewhenandotherwisetags to specify other blocks of code. - Parameters: None
- Example:
<j:choose> <j:when test="${jvar_now_GR.getRowCount() ${AMP}lt; 1}">We found multiple records!</j:when> <j:when test="${jvar_now_GR.next()}">We found record ${jvar_now_GR.getValue('number')}</j:when> <j:otherwise>Sorry, we could not find the record you specified.</j:otherwise> </j:choose>
- Description: The
- when
- Description: The
whentag is used within a choose block to indicate a condition. This tag is similar to anifor anelseifin that it specifies a condition, executes the inner content, and then implies a break at the end to leave theif-elseifconstruct. - Parameters:
test- The condition to evaluate in order to determine if the statement will loop through. This should be an expression enclosed in${}or$[]that evaluates to true or false. - Example:
<j:choose> <j:when test="${jvar_now_GR.getRowCount() ${AMP}lt; 1}">We found multiple records!</j:when> <j:when test="${jvar_now_GR.next()}">We found record ${jvar_now_GR.getValue('number')}</j:when> <j:otherwise>Sorry, we could not find the record you specified.</j:otherwise> </j:choose>
- Description: The
- otherwise
- Description: The
otherwisetag is used within achoose/when/otherwiseblock, and is like theelseordefaultcase. - Parameters: None
- Example:
<j:choose> <j:when test="${jvar_now_GR.getRowCount() ${AMP}lt; 1}">We found multiple records!</j:when> <j:when test="${jvar_now_GR.next()}">We found record ${jvar_now_GR.getValue('number')}</j:when> <j:otherwise>Sorry, we could not find the record you specified.</j:otherwise> </j:choose>
- Description: The
Glide Tags
- evaluate
- Description: The
evaluatetag evaluates JavaScript code (server side), and makes variables visible to future code. Unlike other tags, theevaluatetag evaluates the content that is inside the tag as server side JavaScript.The context is the same as that of script includes in the system. Other script includes, global business rules, GlideRecord, GlideSystem, and Jelly variables (prefixed with jelly. if the parameter
jelly="true"is set) are available. - Parameters:
var- The name of the variable that will be set to the result of the script.object- If set totrue, the result of the expression is treated as an object instead of a primitive variable (string or integer variable values).jelly- If set totrue, allows Jelly context variables to be referenced in the script.expression- This is an expression to be executed for the value to put invar. The expression can be either of two places. First, it can be an attribute on theevaluatetag itself. Otherwise, the content between the beginning tag and ending tag is the expression. The last line of the expression is the actual value passed intovar.
- Example:
<g:evaluate var="jvar_now_GR" object="true"> var now_GR = new GlideRecord("incident"); now_GR.addQuery("active", "true"); now_GR.query(); now_GR; // this is the variable put into the variable jvar_now_GR </g:evaluate><g:evaluate var="jvar_now_GR" object="true" expression=" var now_GR = new GlideRecord('incident'); now_GR.addQuery('active', 'true'); now_GR.query(); now_GR; // this is the variable put into the variable jvar_now_GR" />
- Description: The
- messages
- Description: The
messagestag helps with translation. When gs.getMessage() is called anywhere on a page, there are two possible places where the translation is found. First, the page checks a local cache of translations. Second, the page makes an AJAX call to the server to find the translation. Whatg:messagesdoes is allow pages to cache certain messages. - Parameters: None
- Example:
<g:messages> Yes No Cancel </g:messages>
- Description: The
- breakpoint
- Description: When the
breakpointtag is called, it prints a list of all the variables in Jelly at the current moment, with their respective values. If a variable is specified, it prints the requested variable and its value. The output is placed in the system log. - Parameters:
var- (Optional) The variable to log the value for. Ifvaris not specified, then all variables are dumped into the log. - Example:
<g:breakpoint /><g:breakpoint var="sysparm_view"/>
- Description: When the
- no_escape
- Description: The system, by default, uses escaped output as a security
measure. Output placed inside of
no_escapetags is not escaped before output. Be careful when using these tags, because if user input is displayed here it can open a security vulnerability on the page. - Parameters: None
- Example (phase 1) – Disables automatic output escaping of all contained ${}
expressions:
<g:no_escape> ${jvar_raw_html_data} </g:no_escape> - Example (phase 2) – Use
NOESCto disable escaping for the specified string. This implies the expression must evaluate to a string.<g:no_escape>$[NOESC:jvar_expr]</g:no_escape>For information on phase 1 and phase 2 evaluation, refer to the Jelly introduction videos listed at the beginning of this section.
- Description: The system, by default, uses escaped output as a security
measure. Output placed inside of
- macro_invoke
- Description: The
macro_invoketag calls a UI macro that you have specified in the database. You may also call a UI macro by specifying it in the tag name. For example, if you had a UI macro named my_macro, you could call that macro with the tag<g:my_macro/>. For information, see UI macros. - Parameters:
macro- The name of the UI macro to execute. If your tag name isg:macro_invoke, then the macro attribute specifies the name of the macro. If the tag name includes the name of the macro, then there is no need to include a macro attribute.- Other attributes - For each attribute you specify, a variable with that name
will be available in the context of the UI macro, prefixed with
jvar_.
- Example:
<!-- Will invoke the contents of the UI macro named "sample_macro", which will have the variable jvar_message available within it--> <g:macro_invoke macro="sample_macro" message="This is a sample macro variable." /><!-- Will invoke the contents of the UI macro named "sample_macro", which will have the variable jvar_message available within it--> <g:sample_macro message="This is a sample macro variable." />
- Description: The
- if_polaris
- Description: The
if_polaristag checks if Next Experience is enabled for the current page. It must include at least one of the child tags<g:then />org:else />. - Parameters: None
- Example:
<g:if_polaris> <g:then><g:inline template="polaris_nav"/></g:then> <g:else><g:inline template="classic_nav"/></g:else> </g:if_polaris> <g:if_polaris> <g:then><a href="…">Click here to see what’s new!</a></g:then> </g:if_polaris> <g:if_polaris> <g:else>Core UI only code here!</g:else> </g:if_polaris>
- Description: The
- then
- Description: The
thentag is used within anif_polarisblock to set the page content when Next Experience is enabled. - Parameters: None
- Example:
<g:if_polaris> <g:then><g:inline template="polaris_nav"/></g:then> <g:else><g:inline template="classic_nav"/></g:else> </g:if_polaris> <g:if_polaris> <g:then><a href="…">Click here to see what’s new!</a></g:then> </g:if_polaris> <g:if_polaris> <g:else>Core UI only code here!</g:else> </g:if_polaris>
- Description: The
- else
- Description: The
elsetag is used within anif_polarisblock to set the page content when Next Experience isn't enabled. - Parameters: None
- Example:
<g:if_polaris> <g:then><g:inline template="polaris_nav"/></g:then> <g:else><g:inline template="classic_nav"/></g:else> </g:if_polaris> <g:if_polaris> <g:then><a href="…">Click here to see what’s new!</a></g:then> </g:if_polaris> <g:if_polaris> <g:else>Core UI only code here!</g:else> </g:if_polaris>
- Description: The