Can hasRole() be used inside of a loop?

a99920
Kilo Contributor

I am familiar with using it in reference to the current user using either g_user or gs but I am wanting to use it inside of an addQuery against my user table. Basically the goal is to walk through each user and determine if they have a role (and if they do we will execute a command). I am having issues using this against my variable I create however.

1 ACCEPTED SOLUTION

venkatiyer1
Giga Guru

Hi Joe,



Using the addquery method on sys_user for roles wont fetch the roles for the given user. roles column in sys_user is not referenced. The information of the association between the roles table and user table lies in sys_user_has_role   table or alternatively we can use the shortcut method of hasRole().



So we need to query against the sys_user_has_role  



var role= new GlideRecord('sys_user_has_role   ');



role.addQuery('user', sysid of user);



role.query();


View solution in original post

14 REPLIES 14

venkatiyer1
Giga Guru

Hi Joe,



Using the addquery method on sys_user for roles wont fetch the roles for the given user. roles column in sys_user is not referenced. The information of the association between the roles table and user table lies in sys_user_has_role   table or alternatively we can use the shortcut method of hasRole().



So we need to query against the sys_user_has_role  



var role= new GlideRecord('sys_user_has_role   ');



role.addQuery('user', sysid of user);



role.query();


This makes sense. Thank you!



The subtile difference is that I want to return all users who have the role (not search if one user has the role. I'm trying the following and still seeing issues with my addQuery statement:



var user = new GlideRecord('sys_user_has_role');
user.addQuery('role','timecard_user');
user.query();


while (user.next()) {


  //do some stuff


}



Thoughts/ideas/suggestions???


venkatiyer1
Giga Guru

Hi Joe,


In the code below, instead of 'timecard_user' in the add Query can you replace that with sys_id of timecard_user role. You can get the sys id by going to that role record . Since you are hard coding that sys id value keep it within quotes as in 'sys id value'



So your addQuery statement should be user.addQuery('role', 'sysid');



var user = new GlideRecord('sys_user_has_role');
user.addQuery('role','timecard_user');
user.query();


while (user.next()) {


  //do some stuff


}


That worked!   I'm now getting all of my records back that match the sys_id given for the role.



I still notice 2 issues (if I can be so bold to request more assistance). The biggest issue is that it does not seem to be able to copy the user name over. It is creating the record on my new table as empty. <pasting code below>



The second issue is that I would like this to deduplicate my table I'm writing too (if possible).



var user = new GlideRecord('sys_user_has_role');
user.addQuery('role','ef0c18e2372231000ddf26877e41f1d0');
user.query();


while (user.next()) {
  var tcp = new GlideRecord('u_time_card_check');
      tcp.initialize();
      tcp.user = user.user;   //this is the piece of code that is populating empty
      tcp.insert();



}


venkatiyer1
Giga Guru

HI Joe,



Sure no issues.



First thing is to check whether the user column in u_time_card_check is a reference column to the table User (sys_user).



Then second thing would be to modify the code as below



var grUserRole = new GlideRecord('sys_user_has_role');
grUserRole.addQuery('role','ef0c18e2372231000ddf26877e41f1d0');
grUserRole.query();


while (grUserRole.next()) {
  var tcp = new GlideRecord('u_time_card_check');
      tcp.initialize();
      tcp.user = grUserRole.user.sys_id;
      tcp.insert();
}