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,



A quick question. If it has stopped getting duplicates that means the entries are going fine on the time card check table. So when you say still showing empty are these the old records that got inserted? If not,   then are you   not getting duplicates any more because there is a empty user value record in u time card check.



If no more values are getting inserted. Can you log after the while loop starts for user sys id to see what is happening here




gs.log(grUserRole.user.sys_id);


venkatiyer1
Giga Guru

Also do check for the column name defined hope it is user in your u_time_card_check table.


I misspoke in my last reply so I deleted (but unfortunately you read and responded), I apologize for that. I am now getting the user field populated perfectly but I DO have duplicates.


Avoid duplicates



var grUserRole = new GlideAggregate('sys_user_has_role');


grUserRole.addQuery('role','ef0c18e2372231000ddf26877e41f1d0');


grUserRole.groupBy('user');


grUserRole.query();


while (grUserRole.next()) {


var tcp = new GlideRecord('u_time_card_check');


tcp.initialize();


tcp.user = grUserRole.user.sys_id;


tcp.insert();


}


venkatiyer1
Giga Guru

that is great. So are you still getting duplicates. If yes, then write a business rule on time card to check on user. For sample you can look for business rules have name prevent duplicates. Also you need to remove the existing duplicates by going to that table and seeing for duplicates. If you have a lot of records then we may need to write a fix script and run it once.