GlideRecord in scheduled job won't iterate through query

chrissanford
Kilo Explorer

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?

15 REPLIES 15

rajeevlochan82
Mega Guru

The script looks fine. Can you just chane your log to below code and look for logs with source = 'TestSch'.....just looking to avoid the scenario where there may be some other scheduled job using the same log message.




gs.log('Row count: ' + ci.getRowCount(), 'TestSch');


gs.log('Loop count: ' + count , 'TestSch');


I added 'TestSch' as the second parameter to the log statements, but the source still showed up as '*** Script' in the log outputs. I am certain that both statements are from my script though. They are the only two statements starting with 'Row count' and 'Loop count' in the log for today and they both were created right after I hit 'Execute now' on the scheduled job.


bcronrath
Kilo Guru

Just to verify - do you get the same exact behavior if you paste that into a fix script and click "run now" after saving?    


yes, and also, maybe remove this part of your code to leave it bare bones just to see if it will simply iterate through the records without the other code there...



---------------------------------------------------------------------------------------


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();


---------------------------------------------------------------------------------------



and just for kicks, can you rename your "count" variable to something else, like "cntCIs"