while(gr.next()) does not loop through table record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2017 08:24 AM
Team, I need some help with a script.
Creating a new Business Rule to first return the roles assigned to current group based on a record field change. Some enhancements to follow. But for now the while(gr.next()) does not loop through the table record. This is confirmed by the message "This is gr.next:false" when the Business Rule runs. I have seen best practices for using getValue() but do not believe that is the issue here. Any suggestions on how to correct the logic to next through the record?
Thanks,
JB
processGroupTypeRole();
function processGroupTypeRole() {
var id = current.name;
gs.addInfoMessage('Current Group ' + id + ' is ' + current.name);
var gr = new GlideRecord("sys_group_has_role");
gr.addQuery('group.name', id);
gr.addQuery('role', gr.role.sys_id);
gr.query();
gs.addInfoMessage("This is gr.next: " + gr.next());
while (gr.next()) {
if (gr.group.name == id){
gs.print('Group ' + id + ' has role ' + gr.role.name);
gs.addErrorMessage(gs.getMessage(" Exists role ") + " " + gr.role.name + ' for ' + gr.group.name);
}
}
}
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2017 09:48 AM
I ran a background script. It works fine for me with few changes'
var id = 'Change Management';
// gs.print('Current Group ' + id + ' is ' + Change Management);
var gr = new GlideRecord("sys_group_has_role");
gr.addQuery('group.name', id);
// gr.addQuery('role', gr.role.sys_id);
gr.query();
//gs.print("This is gr.next: " + gr.next());
while (gr.next())
{
gs.print("inside");
if (gr.group.name == id){
gs.print('Group ' + id + ' has role ' + gr.role.name);
gs.addErrorMessage(gs.getMessage(" Exists role ") + " " + gr.role.name + ' for ' + gr.group.name);
}
}
Harish

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2017 11:24 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2017 12:03 PM
your code should look like this
var id =current.name;
var gr = new GlideRecord("sys_group_has_role");
gr.addQuery('group.name', id);
gr.query();
while (gr.next())
{
if (gr.group.name == id){
gs.print('Group ' + id + ' has role ' + gr.role.name);
gs.addErrorMessage(gs.getMessage(" Exists role ") + " " + gr.role.name + ' for ' + gr.group.name);
}
}
Harish