The CreatorCon Call for Content is officially open! Get started here.

while(gr.next()) does not loop through table record

JahanzebB
Mega Guru

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

    }

  }

}      

7 REPLIES 7

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


    }  


  }  


find_real_file.png


Regards
Harish

You can't do this:


find_real_file.png



You're basically calling gr.next() twice.



Comment out line 11


Harish KM
Kilo Patron
Kilo Patron

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


    }


  }


Regards
Harish