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 09:33 AM
Chris,
Add orderBy on sys_created_on and see what the log show now? And is the loop count consistent all the time?
var ci = new GlideRecord('cmdb_ci');
ci.addNotNullQuery('u_service_desk_support_team');
ci.orderBy('sys_created_on');
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);
Thanks,
Abhinay
Please mark Helpful, Like, or Correct depending on the impact of the response
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2016 09:49 AM
I would recommend doing what you just did, but outputting each of the sys_id (or number or any other unique identifier you prefer), and then run the original script and also output the sys_id, then mash them together using an online diff tool to see which ones are showing in the bare bones script vs the full script, and see if that sheds any light as to what might be going on.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2016 09:56 AM
Brian,
I will not recommend printing all the sys_id or other unique identifier for all the records. Instead you can sort by created date see where the loop stops and then go to the cmdb_ci.list and sort the created from a-z and then just look for the record number showed in the log
Thanks,
Abhinay
Please mark Helpful, Like, or Correct depending on the impact of the response
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2016 10:03 AM
How can he be sure that the records are in all displaying in line though? What if the 5k records are not just the first 5k in the created on list sorted by sys_createdon, but rather a specific data set based on the update of the u_has_business_approver field?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2016 07:08 AM
Yes, I just ran as a fix script and got the exact same behavior in the pop-up log.