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

Rushit Patel2
Tera Guru

Hi,



make sure you are adding proper queries. i.e line 8 and line 9.



on line 9 you can not use gr.role.sys_id. you can not use gr variable as it is not queried yet at that point of time


also after query() line add



gs.addInfoMessage("count:-->" + gr.getRowCount());



to check how many rows retured by your query.



(please mark helpful/like/correct if it helps)


Hi Rushit,



I removed the line 9   below from the script and this helps with the while(gr.next()) loop.



gr.addQuery('role', gr.role.sys_id);



For the count I get a total of 2. So it looks like the query() is running now.



Thanks,


Jahanzeb


josevalente
Kilo Explorer

Hello Jahanzeb,



I believe you can't build your role query exactly like that. You're querying role based on gr, but gr will only be available after you query. So effectively the query you're building is "role is empty" (or undefined). So that's why gr.next() doesn't loop (there's nothing to loop).



gr.addQuery('role', gr.role.sys_id);  



If I understood it correctly and you just want to access the roles a group has, remove this bit and should work ok.



Best regards,
Jose Valente


Hi Jose,



I removed the line 9   below from the script and this helps with the while(gr.next()) loop.



gr.addQuery('role', gr.role.sys_id);



That is correct, goal is to first   get access the roles a group has before modifying any records. What is interesting is, I would expect the while(gr.next()) loop to provide a message for all of the roles this group has, but this only provides a message for one of the roles.



Thanks,


Jahanzeb