hasroleonly - how to check if a given user has a specific role only and no other roles
Import
·
Jun 03, 2021
·
article
How to find out if a user has a specific role and only that role.
This function takes role name and user sys_id as checks if the user has only that role and no other roles. It returns response as true / false.
True = if user only has the specified role
Fast = if user has no role or any other roles or more than one role.
function hasRoleOnly(roleName, userId) { var returnBol = false; var roleList = getAllRoles(userId); if (roleList.length() > 1 || roleList.length == 0 || JSUtil.nil(roleList)) { returnBol = false; } if (roleList.length == 1) { if (roleName == roleList[0]) { returnBol = true; } } return returnBol;
}
View original source
https://www.servicenow.com/community/now-platform-blog/hasroleonly-how-to-check-if-a-given-user-has-a-specific-role/ba-p/2291931