How to get all the roles for any user?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2021 12:23 AM
I have to get all the roles for a user in an array. if the array is empty then I can declare the answer variable in ACL as false. can somebody give the code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2021 12:27 AM
var role = new GlideRecord("sys_user_has_role");
role.addQuery("user","*****");
role.query();
if(role.next())
{
//add the code to pass the role of user
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2021 12:47 AM
Hi
Try this
var arr=[];
var userRole = new GlideRecord("sys_user_has_role");
userRole.addQuery("user","user sys_id");
userRole.query();
while(userRole.next()) //It'll iterate to all possible next records
{
arr.push(userRole.role.toString());
}
Please mark my answer as helpful/correct, if applicable.
Best regards,
Sai Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2021 11:10 PM
Best regards,
Sai Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2021 01:26 AM
Hi Arun,
Below is the script, You can check in background script
var fetchUserRole= new GlideRecord('sys_user_has_role');
fetchUserRole.addEncodedQuery('user=b522bd84f7695410ec1c41b84851e0be');//Please use Sys_id of one of your user
fetchUserRole.query();
var allRole=[];
while(fetchUserRole.next())
{
allRole.push(fetchUserRole.getDisplayValue('role'));
}
gs.info("User having roles :"+allRole);
This how you can get all roles associated to the particular user
If my answer solve your query please mark appropriate response as correct so that the thread will appear as resolved for other users who may have a similar query in the future.