- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2020 01:26 AM
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 .
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2020 03:24 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2020 02:02 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2020 02:42 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2020 03:23 AM
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
Thanks and Regards,
Kunal.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2022 08:51 AM
This script didn't work for me. I get an error that line 2 (var dialog) isn't defined.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2020 03:24 AM
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.