The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Samaksh Wani
Giga Sage
Giga Sage

Jelly Scripting is used to create our own UI pages in ServiceNow.

 

Jelly is comprised of Java- and XML-based scripting and a transformation engine used to turn XML into executable code. The output is usually HTML and JavaScript code that is used by the browser to render UI elements on a page.

 

Jelly Scripting in ServiceNow is used in  Application Modules :-

 

  1. UI Macros.
  2. UI Pages.

 

Jelly InBuilt Tags:-

   

IF Syntax in Jelly :-

<g:evaluate var="jvar_name" object="true">
   var name = "Samaksh"
</g:evaluate>

<j:if test="${jvar_name=='Samaksh'}">
   True
</j:if>

 

Disclaimer :- test is the parameter to check the execution of if block.

 

While Syntax in Jelly :-

while loop iterates while the condition is true.

 

<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()}">
  <div>${jvar_now_GR.getValue('number')}</div>  // Jelly inside HTML Tags used to render the ServiceNow table records.
</j:while>

 

ForEach Syntax in Jelly :-

 

The forEach() array method loops through any array, executing a provided function once for each array element in ascending index order.

 

<j:evaluate>
   var words = ["HRSD", "CSM", "GRC"];
</j:evaluate>

<j:forEach var="jvar_words" items="${words}">
  <p> ${jvar_words} </p>
</j:forEach>

 

Switch Syntax in Jelly :-

The switch case statement is used for executing one condition from multiple conditions

 

<j:set var="jvar_name" value="Samaksh">
 </j:set>

 <j:switch on="${jvar_name}">
  <j:case value="Akshay">
   <p>Welcome, Akshay!</p>
  </j:case>
  <j:case value="Raksha">
   <p>Welcome, Raksha!</p>
  </j:case>
  <j:default>
   <p>Welcome, ${jvar_name}!</p>
  </j:default>
 </j:switch>

 

In this Case Output will be :- Welcome, Samaksh.

 

Set Syntax in Jelly :-

 

It will set the specific value into the new variable, which can be globally in overall jelly script.

 

<j:set var="jvar_incident_number" value="${jvar_now_GR.getValue('number')}"/>

_________________________________________________________________________________________________________

Thanks for Reading 🙂

Happy Learning!!😄

 

Plz mark the article as helpful, if you find it helpful.

 

Comments
Marcos Kassak
Kilo Sage
Kilo Sage

Great post @Samaksh Wani, thanks for sharing!

 

I remember a few years back when this would be much needed! Never found great documentation like this.

PravinHirekudi
Tera Contributor

Suberb infor on Jelly Script Samaksh!

Venkanna G
Tera Contributor

Great post @Samaksh Wani, thanks for sharing!

hardikparpelli
ServiceNow Employee
ServiceNow Employee

Thanks for sharing.

 

Came across this while searching a little more: https://docs.servicenow.com/bundle/washingtondc-application-development/page/script/general-scriptin...

KV_CTS
Tera Contributor

Great that other community users have commented on this topic. Hence, I am going to give a different example to add clarity (if needed by others)


Step 1: Create a UI macro

Step 2: Create a UI formatter

In formatter field, usually you put in the value as: UI macro name + .xml
for example : MyUIMacro.xml

Step 3: Use it in the form layout (Incident or whichever you wish to) and add a formatter (usually under a tab)

Step 4: Check the newly configured formatter in the tab

Please mark my reply as Helpful/Correct, if applicable. Thanks! Might take a second for you and will help me 🙂

 

harleylisa
Tera Contributor

How do you code a switch statement where multiple cases have the same action?

KV_CTS
Tera Contributor

@harleylisa  - Hello there, since your question is quite limited, if I understand the question as it is intended, then, an example could be as follows. Note that in Jelly, there is no native switch statement like in JavaScript. However, you can simulate switch-case behavior using <j:choose>, <j:when>, and <j:otherwise>, which is similar to an if-else if-else chain.

 

 

<j:choose>
<j:when test="${category == 'hardware' || category == 'software' || category == 'network'}">
<j:out>This is an IT-related request.</j:out>
</j:when>
<j:when test="${category == 'facilities'}">
<j:out>This is a facilities request.</j:out>
</j:when>
<j:otherwise>
<j:out>Category not recognized.</j:out>
</j:otherwise>
</j:choose>

Please mark my reply as Helpful/Correct, if applicable. Thanks! Might take a second for you and will help me 🙂

Version history
Last update:
‎09-16-2023 12:23 AM
Updated by:
Contributors