The CreatorCon Call for Content is officially open! Get started here.

How to execute Jelly script in dynamic block

Robert Simmons
Kilo Expert

I'm new to ServiceNew development and Jelly scripting, so forgive my naivete. 

I'm trying to dynamically build a custom menu within a dynamic block. However, the dynamic content doesn't get rendered.  

I've created the dynamic block with the XML below.  Note toward the bottom, I'm invoking a UI macro called "step_header".

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

	<!-- SETUP STEP HEADER AND LOGO -->

	<table class="step_header" width="100%">
		<tr>
			<!-- Cell containing logo -->
			<td width="180px">
				<a href="https://dev61611.service-now.com/step/home.do">
					<img src="step_header_logo.png"></img>
				</a>
			</td>
			<!-- Cell that will contain menu...to be built dynamically below -->
			<td class="step_nav">

				<!-- CYCLE THROUGH MENU SECTIONS AND BUILD HTML -->
				
				<g:step_header/>
					
			</td>
		</tr>
	</table>
</j:jelly>

Here's the step_header UI macro:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

	<!-- OPEN MENU SECTION TABLE -->

	<g:evaluate>
		var ms = new GlideRecord("menu_section"); <!-- open menu-section table -->
		ms.addQuery("content_block_menu", "step_menu_data");  <!-- filters results to return only menu items associated with 'step_menu_data' -->
		ms.addQuery("active", true); <!-- only returns active records -->
		ms.orderBy("order"); <!-- orders results by order -->
		ms.query(); <!-- queries the table based on the above criteria -->
		
	</g:evaluate>
	
	<!-- CYCLE THROUGH MENU SECTIONS AND BUILD HTML -->			
	<j:if test="${ms.hasNext()}">
		<j:while test="${ms.next()}">
			<div>
				<img src="${ms.image}"/>  <!-- display top menu image -->

				<!-- THIS IS WHERE THE MENU ITEMS WILL BE PROCESSED -->

			</div>
		</j:while>
	</j:if>

</j:jelly>

I originally had the code above contained within the dynamic block XML, but that didn't work either.

What am I doing wrong?

1 ACCEPTED SOLUTION

TrevorK
Kilo Sage

Your jelly is correct - I don't have anything in that table in my developer instance but I modified the glide record slightly and this works fine in a UI page:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

	<!-- OPEN MENU SECTION TABLE -->

	<g:evaluate>
		var ms = new GlideRecord("sys_user"); <!-- open menu-section table -->
		ms.setLimit(10);
		ms.query(); <!-- queries the table based on the above criteria -->
	</g:evaluate>
	
	<!-- CYCLE THROUGH MENU SECTIONS AND BUILD HTML -->			
	<j:if test="${ms.hasNext()}">
		<j:while test="${ms.next()}">
			<div>
				Name: ${ms.first_name}
			</div>
		</j:while>
	</j:if>

</j:jelly>

Again, my goal was to modify your code as little as possible. I just needed to pick a table that I had records in (and so do you). I assume the above works for you.

 

Which leads me to suspect there is a problem with your GlideRecord query. Looking at the fields you query I notice content_block_menu. Looking at the table structure this is a Reference field. Have you tried supplying a sys_id in your query for this?

View solution in original post

2 REPLIES 2

TrevorK
Kilo Sage

Your jelly is correct - I don't have anything in that table in my developer instance but I modified the glide record slightly and this works fine in a UI page:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

	<!-- OPEN MENU SECTION TABLE -->

	<g:evaluate>
		var ms = new GlideRecord("sys_user"); <!-- open menu-section table -->
		ms.setLimit(10);
		ms.query(); <!-- queries the table based on the above criteria -->
	</g:evaluate>
	
	<!-- CYCLE THROUGH MENU SECTIONS AND BUILD HTML -->			
	<j:if test="${ms.hasNext()}">
		<j:while test="${ms.next()}">
			<div>
				Name: ${ms.first_name}
			</div>
		</j:while>
	</j:if>

</j:jelly>

Again, my goal was to modify your code as little as possible. I just needed to pick a table that I had records in (and so do you). I assume the above works for you.

 

Which leads me to suspect there is a problem with your GlideRecord query. Looking at the fields you query I notice content_block_menu. Looking at the table structure this is a Reference field. Have you tried supplying a sys_id in your query for this?

Thanks Trevor.

You pointed me in the right direction.  Since the field I was trying to query was a reference field, I needed to dot walk.  I replaced "content_block_menu" with "content_block_menu.name" and it worked.

Much appreciated.  Bob