GlideRecord in scheduled job won't iterate through query
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2016 12:07 PM
I am trying to write a scheduled job that will set a custom field called 'has business approver' based on whether or not a business change reviewer exists for a CI. I want to do this for all CI's in CMDB with a service desk support team. For some reason, the while loop does not seem to iterate through all the CI's in the query but only a select few. Here is my code:
var ci = new GlideRecord('cmdb_ci');
ci.addNotNullQuery('u_service_desk_support_team');
ci.query();
gs.log('Row count: ' + ci.getRowCount());
var count = 0;
while (ci.next())
{
count = count + 1;
var change_reviewer = new GlideRecord('u_ci_change_reviewers');
change_reviewer.addQuery('u_configuration_item', ci.sys_id);
change_reviewer.addQuery('u_business_approver', true);
change_reviewer.query();
if (change_reviewer.hasNext()) {
ci.u_has_business_approver = true;
}
else {
ci.u_has_business_approver = false;
}
ci.update();
}
gs.log('Loop count: ' + count);
After execution, the following is output to the log:
Row count: 101503
Loop count: 5179
The 101503 is how many CI's with a service desk support team actually exist. Why is the loop only running for 5179 CI's?
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2016 07:15 AM
Looks like that worked, I commented it out and the loop ran the right number of times. I can't figure out what that code would be doing to break out of the loop early.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2016 07:59 AM
Did you also change the name of the count variable or just remove that other code?
-Jon
Sent from my iPhone
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2016 08:23 AM
I changed the name of the count variable. Here is my code after changes:
var ci = new GlideRecord('cmdb_ci');
//ci.addQuery('u_sox_requirements_apply','0');
//ci.addQuery('install_status', '102');
ci.addNotNullQuery('u_service_desk_support_team');
ci.query();
gs.log('Row count: ' + ci.getRowCount(), 'TestSch');
var cntCIs = 0;
while (ci.next())
{
cntCIs = cntCIs + 1;
// var change_reviewer = new GlideRecord('u_ci_change_reviewers');
// change_reviewer.addQuery('u_configuration_item', ci.sys_id);
// change_reviewer.addQuery('u_business_approver', true);
// change_reviewer.query();
// if (change_reviewer.hasNext()) {
// ci.u_has_business_approver = true;
// }
// else {
// ci.u_has_business_approver = false;
// }
// ci.update();
}
gs.log('Loop count: ' + cntCIs, 'TestSch');
After further experimentation, it looks like the loop still runs correctly if I uncomment everything except the last 'ci.update()' statement. It appears the update statement is causing the loop to break early.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2016 09:02 AM
Chris, for kicks, can you also try to rename the "ci" object to something like grCI? just wondering if there is a conflicting name or something...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2016 09:33 AM
I see this all the time when I don't place my code inside a function declaration and then call the function.
myCodeHere();
function myCodeHere(){
//copy your code in here.
}
Jonathan is correct in his suggestion to avoid using variable names like "gr" and "ci". They are very commonly used variables.