When scripting in ServiceNow, be careful how you create your variables. If you refer to an object such as a gliderecord field/column, you are creating a pointer to the ever-present current value rather than a static value at a point in time. Perhaps an example will help explain…

In our fictitious example, Ranner is the head of marketing and wants to know the first and last person with a first name starting with “Ra…” Obscure, I know, but it will demonstrate the point.

So we develop a script we can test using the Background Script module in ServiceNow to check a subset of records to make sure our logic is correct. We grab the first value and the last after cycling through the table.

var userGlide = new GlideRecord('sys_user');
userGlide.addEncodedQuery('first_nameSTARTSWITHRA');
userGlide.orderBy('first_name');
userGlide.setLimit('10');
userGlide.query();
 
var i = 0;
var firstUser, lastUser;
 
while (userGlide.next()) {
 
if (i==0) {
firstUser = userGlide.first_name;
gs.info('The firstUser is ' + firstUser );
}
 
gs.info('Iteration ' + i + ' current user = ' + userGlide.first_name + '\t firstUser = ' + firstUser );
 
if (!userGlide.hasNext()) {
lastUser = userGlide.first_name;
gs.info('The last user is ' + lastUser);
}
i++;
}
gs.info('-----------------------------------------------');
 
gs.info('First user is ' + firstUser );
gs.info('Last user is ' + lastUser );

If you look at the output when this runs as a Background Script, you can see there are inconsistent results. The firstUser variable changes every time the gliderecord changes even though it was set only once at the beginning.

*** Script: The firstUser is Ra
*** Script: Iteration 0 current user = Ra firstUser = Ra
*** Script: Iteration 1 current user = Ra-cheal firstUser = Ra-cheal
*** Script: Iteration 2 current user = Raamana firstUser = Raamana
*** Script: Iteration 3 current user = Raamon firstUser = Raamon
*** Script: Iteration 4 current user = Rabi firstUser = Rabi
*** Script: Iteration 5 current user = Rabia firstUser = Rabia
*** Script: Iteration 6 current user = Rabin firstUser = Rabin
*** Script: Iteration 7 current user = Racahael firstUser = Racahael
*** Script: Iteration 8 current user = Rachael firstUser = Rachael
*** Script: Iteration 9 current user = Rachael firstUser = Rachael
*** Script: The last user is Rachael
*** Script: -----------------------------------------------
*** Script: First user is Rachael
*** Script: Last user is Rachael

The reason for this is although we defined our variable at the start, all we did was assign a pointer to the glide record, not a static value. Every time the glide record moves onto a new record, so does the value within our variable.

The solution is to make sure we’re only grabbing a value and not an object from the gliderecord, by adding .getValue() to our script

var userGlide = new GlideRecord('sys_user');
userGlide.addEncodedQuery('first_nameSTARTSWITHRA');
userGlide.orderBy('first_name');
userGlide.setLimit('10');
userGlide.query();
 
var i = 0;
var firstUser, lastUser;
 
while (userGlide.next()) {
 
if (i==0) {
firstUser = userGlide.getValue('first_name');
gs.info('The firstUser is ' + firstUser );
}
 
gs.info('Iteration ' + i + ' current user = ' + userGlide.first_name + '\t firstUser = ' + firstUser );
 
if (!userGlide.hasNext()) {
lastUser = userGlide.getValue('first_name');
gs.info('The last user is ' + lastUser);
}
i++;
}
gs.info('-----------------------------------------------');
 
gs.info('First user is ' + firstUser );
gs.info('Last user is ' + lastUser );

Now our results are correct

*** Script: The firstUser is Ra
*** Script: Iteration 0 current user = Ra firstUser = Ra
*** Script: Iteration 1 current user = Ra-cheal firstUser = Ra
*** Script: Iteration 2 current user = Raamana firstUser = Ra
*** Script: Iteration 3 current user = Raamon firstUser = Ra
*** Script: Iteration 4 current user = Rabi firstUser = Ra
*** Script: Iteration 5 current user = Rabia firstUser = Ra
*** Script: Iteration 6 current user = Rabin firstUser = Ra
*** Script: Iteration 7 current user = Racahael firstUser = Ra
*** Script: Iteration 8 current user = Rachael firstUser = Ra
*** Script: Iteration 9 current user = Rachael firstUser = Ra
*** Script: The last user is Rachael
*** Script: -----------------------------------------------
*** Script: First user is Ra
*** Script: Last user is Rachael

When working with glide records, be sure to use the getValue() to avoid headaches with pointers.

1 comment

Very nice, informative so far i’m overlooking this i’ll make sure i’ll check this from now on wards when i point to GlideRecord.
Thank you :).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.