logo

NJP

Introduction to Jelly Script in ServiceNow #1

Import · Sep 16, 2023 · article

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 :-

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 :slightly_smiling_face:

Happy Learning!!:grinning_face_with_smiling_eyes:

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

View original source

https://www.servicenow.com/community/developer-articles/introduction-to-jelly-script-in-servicenow-1/ta-p/2673612