How to get all the roles for any user?

Community Alums
Not applicable

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.

9 REPLIES 9

Nikita30
Mega Sage

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

 

}

Sai Kumar B
Mega Sage
Mega Sage

Hi @Arun B  

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

@Arun B  Thanks for marking my answer as helpful, could you please mark it as correct so it would help future readers as well 🙂 

Best regards,
Sai Kumar

Kajal Goti
Mega Guru

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.