Check if a user for a given userID has snc_internal role from scoped application

DILEEPKUMAS7622
ServiceNow Employee
ServiceNow Employee

In global application scope, we can check if a user has a role using the below API:
new GlideUser().getUserByID(userId).hasRole(roleName)

But for scoped applications, i'm not able to find a similar method. For a given userID and in my application scope, i want to check if it has snc_internal role or not?

5 REPLIES 5

Community Alums
Not applicable

Hi @DILEEPKUMAS7622 ,

You can query the sys_user_has_role table to retrieve a list of roles for the user.

Sample Code:

var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user.user_name', gs.getUserDisplayName());
gr.query();
while(gr.next())
{
gs.addInfoMessage(gr.role.name); //prints the role name
}

 

 

Yeah, that is the last option and i would not want to hit the DB directly. If you see in global scope, the methods like new GlideUser().getUserByID(userId).hasRole(roleName) rely on cache and fetch the data from cache. I'm looking for a somewhat similar approach

Ankur Bawiskar
Tera Patron
Tera Patron

@DILEEPKUMAS7622 

why not use gs.hasRole('roleName')

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

gs.hasRole() just checks for current user i guess. I want to check, for a given userID, does it have snc_internal role or not