- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2018 10:58 PM
var yearAgo = gs.daysAgo(365);
gr = new GlideRecord("sys_template");
gr.addQuery('u_pre_approved', true);
gr.addQuery("u_approving_rfc.cab_date", "<", yearAgo);
gr.query();
gs.print('** DEBUG ** ROWCOUNT :'+gr.getRowCount());
while(gr.next()) {
gs.print('** DEACTIVATING ** '+gr.name);
gr.active=false;
gr.update();
}
The above code is the basis of a Scheduled job I want to run to deactivate PreApproved templates if they expire.
When run as Scripts - Background this is the result :
My query works fine, as the DEBUG gives the correct row count, however the while loop does not seem to run, and the test template remains active.
Moreover : even if I simply the script to
gr = new GlideRecord('sys_template');
gr.addQuery('u_pre_approved', true);
gr.query();
gs.print('ROWCOUNT :'+gr.getRowCount());
while(gr.next()) {
gs.print('** DEACTIVATING ** '+gr.name);
}
I get a similar result, the correct rowcount but no looping over the query results :
Does anyone have any ideas why my glideRecord does not want to loop?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2018 02:10 AM
Hi Ian,
The template table has a 'next' column, so you'll need to use the _next() method instead.
while (gr._next())
Regards,
Nick Pike
Senior Technical Consultant, Platform Services
ServiceNow
(AU) O: +61 3 8626 8608
(AU) M: +61 401 554 962
www.servicenow.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2018 11:03 PM
Hi,
Instead of gr use soomething else in background script and try once with below code,
var temp = new GlideRecord('sys_template');
temp.addQuery('u_pre_approved', true);
temp.query();
gs.print('ROWCOUNT :'+temp.getRowCount());
while(temp.next()) {
gs.print('** DEACTIVATING ** '+temp.name);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2018 11:12 PM
Thanks for the suggestion : I also noticed that I hadn't used a var to declare my glideRecord, which I though might have been the issue.
Unfortunately ..this code :
var tempgr = new GlideRecord('sys_template');
tempgr.addQuery('u_pre_approved', true);
tempgr.query();
gs.print('NEXT :'+tempgr.hasNext());
while(tempgr.next()) {
gs.print('** DEACTIVATING ** '+tempgr.name);
}
produces this result :

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2018 12:41 AM
Hi,
Try below code,It will work.
var tempgr = new GlideRecord('sys_template');
tempgr.addQuery('u_pre_approved', true);
tempgr.query();
gs.print('NEXT :'+tempgr.hasNext());
while (tempgr._next()) {
gs.print('** DEACTIVATING ** '+tempgr.name);
}
Mark answer as Correct if it solves your issue.
Thanks,
Ravi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2018 10:25 AM
@IanGlencross Did you tried my code ?
If it works, Can you please mark answer as Correct so that this thread will be helpful for others as well.