Jelly Script: how to display List field entries in a line-per-line format

tahnalos
Kilo Sage

I have two different List fields, one reference and one not on my UI Page. The Reference one is a list of people impacted by an Incident.  Right now, the UI page is displaying this:

 

<example 1>

Impacted Users: Walter White,Jesse Pinkman,Gustavo Fring,Saul Goodman,Lalo Salamanca

 

We want to have the UI page to display things like this:

 

<example 2>

Impacted Users:
Walter White
Jesse Pinkman
Gustavo Fring
Saul Goodman
Lalo Salamanca

 

However, we may want to display this as a Table entry as we would like to eventually expand things to include the user's support group like below:

 

<example 3>
Impacted Users:
Walter White          Heisenberg Inc
Jesse Pinkman        Heisenberg Inc
Gustavo Fring         Los Pollos Hermanos
Saul Goodman        Saul Goodman and Associates
Lalo Salamanca       El Michoacano

 

Any help would be appreciated.

1 ACCEPTED SOLUTION

Rather than building a string of your HTML in JavaScript code nested within an evaluate tag, you can use Jelly to build your markup.

The idea is to first use <g:evaluate> to grab the data you want to be included in your output HTML and store that in some sort of data structure. The g:evaluate part of your script can essentially be the "preprocessing" part, which is responsible for obtaining some sort of data that you want to display. That data can then be looped over using a <j:forEach> to build your HTML <table>

 

For example:

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
      <!-- Add your first <g:evaluate> here but change it to a <g2:evaluate> --->

	<g2:evaluate jelly="true">
		var stakeList = jelly.jvar_incidentgr.u_impacted_user_list.getDisplayValue().split(', ');
	</g2:evaluate>
	<p>Names</p>
	<table>
		<tbody>
		<j2:forEach var="jvar_impactedUser" items="$[stakeList]">
			<tr>
				<td>$[jvar_impactedUser]</td>
			</tr>
		</j2:forEach>
		</tbody>
	</table>
</j:jelly>

 

You can make the stakeList array hold more sophisticated data, such as objects to hold related data. You can then add additional columns to each row (<tr>) in your table by adding an additional <td> to output any other associated data. You can refer to MDN's article on styling tables if you want to add additional cellpadding/spacing or a border. 

 

In the above example, I'm using a g2:evalaute as it will recompute the HTML each time the UI page is loaded, as opposed to a g:evaluate that caches the rendered HTML output and doesn't update it on subsequent reloads. It's up to you decide which is best for you, but if you're using g2/j2, then you need to use $[] instead of ${} when accessing variables. 

 

In addition, I suggest checking out Chuck Tomasi's YouTube guide on Jelly scripting, the links can be found at the top of the Jelly Scripting documentation in the ServiceNow docs.

View solution in original post

9 REPLIES 9

tahnalos
Kilo Sage

I also need to clarify: the intent is to not use the field but to dump getDisplayValue and parse it into HTML, so want to know how I can accomplish this.

tahnalos
Kilo Sage

So I need to provide an update as to what I am trying to do.

 

I am getting around to understanding the <g:evaluate> function and I have come up with the following:

 

tahnalos_0-1677544393404.png

The first Evaluate function gets the Incident record, and the second is used to extract the impacted user list.  I then use an array split to then populate a string field.

 

If I use that function in regular servicenow code, then the \n is interpreted as a newline.  But \n does not work in XML so wondering what other XML friendly option that I can do here?

Rather than building a string of your HTML in JavaScript code nested within an evaluate tag, you can use Jelly to build your markup.

The idea is to first use <g:evaluate> to grab the data you want to be included in your output HTML and store that in some sort of data structure. The g:evaluate part of your script can essentially be the "preprocessing" part, which is responsible for obtaining some sort of data that you want to display. That data can then be looped over using a <j:forEach> to build your HTML <table>

 

For example:

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
      <!-- Add your first <g:evaluate> here but change it to a <g2:evaluate> --->

	<g2:evaluate jelly="true">
		var stakeList = jelly.jvar_incidentgr.u_impacted_user_list.getDisplayValue().split(', ');
	</g2:evaluate>
	<p>Names</p>
	<table>
		<tbody>
		<j2:forEach var="jvar_impactedUser" items="$[stakeList]">
			<tr>
				<td>$[jvar_impactedUser]</td>
			</tr>
		</j2:forEach>
		</tbody>
	</table>
</j:jelly>

 

You can make the stakeList array hold more sophisticated data, such as objects to hold related data. You can then add additional columns to each row (<tr>) in your table by adding an additional <td> to output any other associated data. You can refer to MDN's article on styling tables if you want to add additional cellpadding/spacing or a border. 

 

In the above example, I'm using a g2:evalaute as it will recompute the HTML each time the UI page is loaded, as opposed to a g:evaluate that caches the rendered HTML output and doesn't update it on subsequent reloads. It's up to you decide which is best for you, but if you're using g2/j2, then you need to use $[] instead of ${} when accessing variables. 

 

In addition, I suggest checking out Chuck Tomasi's YouTube guide on Jelly scripting, the links can be found at the top of the Jelly Scripting documentation in the ServiceNow docs.

Thanks for your input, I will look into this.  Due to the nature of this report, it is only supposed to be accessed once the incident is over, so I don't expect g2/j2 to apply here.  I'm going to give your approach a shout and see how it is handled.  Thanks.