while loop only executing for one record (background script)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2012 12:48 PM
Trying to develop a script to run as a background script to remove inherited itil roles from users who no longer need it (by virtue of their not having logged on for some time). The problem I'm having is that it works just fine with the inconvenient exception that it only removes the role from one user, even though multiple records are found.
The only clue I can find is that when I remove the line 'gr2.deleteRecord();' it does in fact complete the first loop through all records. But with that line in, it only runs through the first loop for one record. What am I missing?
gs.log('>>>BMA: starting process for usersNoLogin last 90 days');
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('role.name','itil');
gr.addQuery('user.active','true');
gr.addQuery('user.u_company_code', '!=', '0594').addOrCondition('user.u_company_code','');
gr.addQuery('user.last_login_time','<', gs.daysAgo(90)).addOrCondition('user.last_login_time','');
gr.query();
gs.log('>>>BMA: total number of usersNoLogin last 90 days: '+gr.getRowCount());
//initialize counter
var numberUpdated=0;
while(gr.next()){
var thisUserID = gr.user.sys_id;
var thisUserName = gr.user.user_name;
//get the groups the user is a member of
var gr2 = new GlideRecord('sys_user_grmember');
gr2.addQuery('user', thisUserID);
gr2.query();
//loop through all of the users groups
while(gr2.next()){
//make sure the group is not their public group
if(gr2.group!='c43c87bcff421000631771f4497efef1' && gr2.group!='683ccbbcff421000631771f4497efefa'){
//delete the group membership
gs.log('>>>BMA: removing user '+thisUserName+' from group '+gr2.group.name);
gr2.deleteRecord();
numberUpdated++;
}
}
}
gs.log('>>>BMA: process for usersNoLogin last 90 days complete, '+numberUpdated+' records removed');
- 6,369 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2012 01:15 PM
If you change your "gr" variable to something else, do you get better results?
If the sys_user_grmember table has a "delete" business rule that uses the variable "gr" outside of a function, your variable could be getting hijacked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2012 06:06 AM
CapaJC, a brilliant suggestion! Changing the variable name indeed solves the problem. Thank you!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-13-2017 06:14 AM
Great suggestion! Really saved me some heartache!
I ran into the same problem trying to do a delete on the sys_user_grmember table. It looks like the root cause is a business rule named 'Group Member Delete'.
It sounds like a bug to me that a gr named within that scope would affect the scope higher. All business rules, especially OOB ones should be enclosed in functions to avoid this variable scope issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2018 11:47 PM
Really it worked great.