Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get user all roles

Kumar Raj
Giga Contributor

Hi 

I have requirements : there is a Custom table A and its extending to User table and then i want to check those users roles. Which is in Custom table and How many roles have each user. There is a possibility to repeat the user name in custom table.

Please help me .

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

try with below script. its server side script, you can execute it on business rule .

 

var fn = current.<field name> ;  // which hold the user name 

var gr = new GlideRecord('sys_user_has_role');

gr.addQuery('user.name',fn);

gr.query();

gs.log('row count is '+ gr.getRowCount());

 

while(gr.next()){

 

 

if(gr.role.getDisplayValue() != 'approver_req' || gr.getRowCount() == 0){

var grl = new GlideRecord('sys_user_has_role');

grl.initialize();

grl.role = 'approver_req';

grl.user = fn;

grl.insert();

 

}

}

 

If my answer helped you, kindly mark it as correct and helpful.

View solution in original post

14 REPLIES 14

Thanks For your response.

Ok I am Trying through BR. But its getting row count is 0, i have tried with both BR . after,before .

find_real_file.png

No Update Because these 4 user has all ready but other users has not updated:

find_real_file.png

These record are on that table. 

find_real_file.png

Thanks

so it means "Abel tuter" has not any role . can you add script which you have used. so i can check if any thing missed there

only i have added the custom field name. u_user_name

var fn = current.u_user_name;  // which hold the user name 

u_user_name is reference type field ? 

if yes then update the addQuery part. 

 

gr.addQuery('user',fn);

 

 

Hi Harsh,

Thanks for your support.

Instead of role name i used the sys_id of that role.

grl.role = 'ae52cbcf1b43001060c41067bc4bcb19';

Suppose user have that role by the group. so it will assigned that role to that user also.

is there way to filter.