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

Kunal Varkhede
Tera Guru

Hi,

 

Is there any reference field in custom table that refer to user table?

If Custom field is there then use  GlidedialogWindow api in Client Script

var gr = "user="+caller_id; //write Query here (if user)
var dialog = new GlideDialogWindow("show_list");
dialog.setTitle("Get Role Of User"); //Set the dialog title
dialog.setPreference("table", "sys_user_has_role"); //Pass the table name
dialog.setPreference("sysparm_query",gr);//Pass the query
dialog.render();

Thanks,

Kunal.

Yes there is one field which is referring the User table 

Hi Kunal,

based on that fields i want to check the users roles

Based on that field write onChange() client script use GlideDialogWindow() api.

Make sure that table would be sys_user_has_role

var gr = "user="+newValue; //newValue is reference field of user table.

 

Mark Correct/Helpful answer if it help you.

Thanks and Regards,

Kunal.