My query returns rows , but while(gr.next()) does not loop through them ... why?

IanGlencross
Kilo Expert
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 :find_real_file.png

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  :

find_real_file.png

Does anyone have any ideas why my glideRecord does not want to loop?

1 ACCEPTED SOLUTION

Nick Pike
ServiceNow Employee
ServiceNow Employee

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

View solution in original post

12 REPLIES 12

Nick Pike
ServiceNow Employee
ServiceNow Employee

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

Agree with Nick here and have gotten burnt by this, with the sys_template table you must use _next() instead.

Thanks Nick.  As soon as you said it, I had a face-palm moment.