While(gr.next()) is not looping through records

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2012 06:59 AM
We have a custom table called IMAC that extends the change request table. We have setup the same system for auto closing of these records the same way the base line does for incidents.
I have copied the script from the incident business rule to a business rule on this table (with the obvious changes to tables and values in the script), setup the scheduled job that runs once an hour. The problem I am experiencing is that this script runs, but only grabs one record at a time instead of looping through the while statement.
Script
var ps = gs.getProperty('glide.ui.autoclose.time');
var pn = parseInt(ps, 10);
if (pn > 0) {
var gr = new GlideRecord('change_request_imac');
gr.addQuery('state', 10);
gr.addQuery('sys_updated_on', '<', gs.daysAgo(pn));
gr.addQuery('u_imac_type', '!=', 'Termination');
gr.addQuery('u_imac_type', '!=', 'Transfer');
gr.addQuery('u_imac_type', '!=', 'Legal Request');
gr.query();
while(gr.next()) {
gr.state = 9;
// gr.comments = 'IMAC automatically closed after ' + pn + ' days in the Resolved state.';
gr.active = false;
gr.update();
}
}
If I duplicate the query on the table, I receive over 20 that match.
If I run the script, it will only set one record to closed (state = 9).
I can run it immediately again, and it will close one more.
The script matches the incident script and that one works fine.
What could be causing this while(gs.next()) to not loop through the records the query has found?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2012 08:18 AM
Not sure, but you might want to try wrapping the script in a function like so:
runScript();
function runScript() {
var ps = gs.getProperty('glide.ui.autoclose.time');
var pn = parseInt(ps, 10);
if (pn > 0) {
var gr = new GlideRecord('change_request_imac');
gr.addQuery('state', 10);
gr.addQuery('sys_updated_on', '<', gs.daysAgo(pn));
gr.addQuery('u_imac_type', '!=', 'Termination');
gr.addQuery('u_imac_type', '!=', 'Transfer');
gr.addQuery('u_imac_type', '!=', 'Legal Request');
gr.query();
while(gr.next()) {
gr.state = 9;
// gr.comments = 'IMAC automatically closed after ' + pn + ' days in the Resolved state.';
gr.active = false;
gr.update();
}
}
}
I have seen some weird things happen because without the function wrapping, the "gr" object could get changed by some other code as it is global.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2012 08:28 AM
It could also be another Business Rule munging the "gr" variable during the update of the first change_request_imac record.
You could try turning on Business Rule debugging and see what rules are running, and where they seem to stop.
Or just change your variable name from gr to something else, and do what Jim recommended (put it in a function).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2012 10:47 AM
Thanks for the suggestions.
First, I wrapped the script in a function as suggested.
Second, I changed it to a scheduled job under system definition instead of system scheduler so that the script is in the scheduled job rather than a business rule.
This seems to work like a charm now.