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

Refer the attached screenshot

find_real_file.png

Output window: 

Depending upon caller selected the roles will be diasplayed.

find_real_file.png 

Please Mark correct/Helpful answer if it helps you.

Thanks and Regards,

Kunal.

Hi Kunal,

 I dont want to show in Dilog window . Just  i want to Know the How many roles have user and after that i have another role like (approver_req). if user is having that roles than no need to update that user else update the user with this approver_req role.

This is the main requirement.

Thanks kunal your immediate response. plz help me.

Ok.

Use Before Insert/Update Business rule on your custom table.

and In script section 

var gr = new GlideRecord(sys_user_has_role);
gr.addQuery('user',current.caller_id);
gr.query();
if(gr.next())
{
gs.log("Role Of user"+gr.role);
if(gr.role =='approver_req')
{
current.setAbortAction(true);
}
else
{
gr.role = 'approver_req';
gr.update();
}
}

Refer this link

https://community.servicenow.com/community?id=community_question&sys_id=7066c7e1db1cdbc01dcaf3231f96...

 

Thanks and Regards,

Kunal.

This script didn't work for me. I get an error that line 2 (var dialog) isn't defined.

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.