- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-16-2024 12:21 AM
Is the gs.hasRoleExactly() method work in servicenow scripting or do we have any alternate method for this?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-16-2024 12:45 AM
Hi @Kishor O
we have g_user.hasRoleExactly which is only accessible in client side.
gs is accessible only in server side scripting, you need build a explicit logic by yourself for server side script.
function hasRoleExactly(role) {
var au = new ArrayUtil();
var roles = gs.getSession().getRoles() + '';
var roleArray = roles.split(",");
var isAuthorized = au.contains(roleArray, role);
return isAuthorized;
}
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-07-2025 06:26 AM
Hi, this is a function which you can put into a Script Include and call from anywhere, also in Scoped Apps:
hasRoleExactly: function(role, ignoreInherited){
var grUserRoles = new GlideRecord('sys_user_has_role');
grUserRoles.addQuery('user', gs.getUserID());
grUserRoles.addQuery('role.name', role);
if(ignoreInherited) grUserRoles.addQuery('inherited', false);
grUserRoles.setLimit(1);
grUserRoles.query();
return grUserRoles.hasNext();
}
You can use it in condition fields like this:
new appUtils().hasRoleExactly('catalog_editor');
Or if you want to only check for roles which are directly added and not inherited:
new appUtils().hasRoleExactly('catalog_editor', true);