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

Hi @Arun B  

 

does my answer resolved your query?

 

If my answer solve your query please mark as correct so that the thread will appear as resolved for other users who may have a similar query in the future.

 

Not getting the output

-O-
Kilo Patron
Kilo Patron

Why are you trying to replace OOB optimized role based ACLs with a performance hog script that does basically the same?

Just list the roles that should grant the access defined in the ACL in the Requires role embedded list of the ACL's definition.

Also your requirement is a little strange, given that lately no user has no roles at all. All users should have at least the snc_internal (or snc_external) role. So the condition that the array of user roles has no items will never be achieved - rendering your check pointless.

Unless your are not providing key information about the requirement.

krajat5
Tera Contributor
a = [];

var userRole = new GlideRecord('sys_user_has_role');
gi.addQuery('user',gs.getUserID());
gi.query();

while (gi.next()) {
    a.push(userRole .getDisplayValue('role'));
}

gs.print(a);

Rafael Batistot
Tera Sage

Hi @Community Alums,

Would you consider this code for your requirement? It will return all roles of the logged-in user.

var role = [];

var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', gs.getUserID());
gr.query();

while (gr.next()) {
    role.push(gr.getDisplayValue('role'));
}

gs.log(role, 'Role Info');

 

RafaelBatistot_0-1752054781141.png