The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
SlightlyLoony
Tera Contributor

Yesterday I introduced the evaluate tag, but didn't really say much about it. This post (and one or two more) we're going to spend some time understanding just what the heck that tag does.

My young friend Whazzup (at right) is intensely interested in this, but I think I detect a little skepticism there. "So what if I can evaluate a script?" he asks. "What good is that gonna do me?"

Well, Wazzup, it's like this: the evaluate tag is one of the wonders of the ServiceNow Jelly world, and it can do a lot for you...

Let's start with yesterday's example:


<?xml version="1.0" encoding="utf-8"?>
<j:jelly>
<g:evaluate>
var colors = ['Red', 'Black', 'Blue', 'Brown', 'CadetBlue', 'DarkGreen', 'DeepPink'];
</g:evaluate>
<j:foreach>

Color: $[SP] ${jvar_color}
</j:foreach>
</j:jelly>

The first thing to understand is where that script is running: it's running on the ServiceNow instance, not in the web browser. There are lots of implications to that. Scripts inside evaluate tags have full access to the information and resources on your ServiceNow instance. On the other hand, they do not have access to the web browser (client) environment: you can't look up HTML elements, show or hide things, etc.

The second thing to understand is when that script gets executed: while the Jelly engine is processing this template. Here's an example that should make that clear:

<?xml version="1.0" encoding="utf-8"?>
<j:jelly>
<g:evaluate>
var user = gs.getUser();
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', user.getID());
gr.query();
var roleIDs = [];
while (gr.next())
roleIDs.push('' + gr.role);
gr = new GlideRecord('sys_user_role');
gr.addQuery('sys_id', roleIDs);
gr.query();
</g:evaluate>

Hello, ${user.getFirstName()} ${user.getLastName()}

Your user ID is: ${user.getID()}

You have these roles:
<j:while>

${gr.name}
</j:while>
</j:jelly>

When I try out this UI Page, here's what I get:

Hello, Completely Loony
Your user ID is: 0e8085600a0005896f731eba56151fe4
You have these roles:
itil_admin
knowledge_admin
admin
content_admin
discovery_admin
knowledge
itil
asset

What's going on here?

Well, inside the evaluate tag you see a piece of ordinary-looking server-side script, the kind you might find in a business rule or a script include. This script is getting the current user from GlideSystem (the gs reference). It then uses this information to query the sys_user_has_role table, which is the many-to-many table that associates roles with users. Then it queries the roles table to find just those roles that the current user has. But it stops at the gr.query() statement, and you don't see the usual while (gr.next()), nor is it doing anything with the information it just queried the database for. Stupid script!

But wait just a second. Look down at the Jelly while tag, in the test attribute. Here's where we're making use of that query: in the while tag we're testing to see if there's another record, and in the line following (the HTML p tag), we're getting and rendering the name of the role. That little "while loop", written in a mix of Jelly, HTML, and JEXL referencing our JavaScript variables does the trick of rendering the HTML we wanted. Well, I wanted it, anyway!

By the way, pay no attention to what my traitorous computer thinks my name is!

I'll leave you today with one word of caution: don't ever use JEXL expressions inside of an evaluate tag. While it works, it has a very nasty side-effect: it consumes lots of a scarce resource (called "PermGen memory") on your instances Java Virtual Machine. There's nothing good about this, so you should avoid it at all costs. There's always another way to get the same job done, and we'll dig into those more in a later post. For now (and forever!) just remember this equation:

evaluate + JEXL == BAD!

So, Whazzup — do you see ways you could use the evaluate tag?

1 Comment