- 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
ā02-16-2024 12:28 AM - edited ā02-16-2024 12:31 AM
Hello @Kishor O
- hasRole(' ');
- hasRoles();
- hasRoleExactly(' ');
- hasRoleFromList(ā ',' ',ā ā);
Now we will discuss one by one about these roles:-
Methods and its Significance
g_user.hasRole(' '):- this method returns true if the current logged-in user has the role which we have provided in (' ') section., otherwise false.it will always returns true if the user has the 'admin' role.
g_user.hasRoles():- this method return true if current logged in user has any single role atleast.
g_user.hasRoleExactly(ā ā):- this method return true if current logged in user has role we which have provided in (' ') section.
g_user.hasRoleFromList(' ',' ',ā ā):- this method return true if current logged in user has admin role or the role/roles from the list which we have provided in (' ') section.
Check the below screen shot which I have implemented in my PDI. Follow the Same Procedure for better understanding. I have logged in as Admin.
For server-side use gs object e.g (gs.hasRole(): )
Mark helpful or correct if I answered correctly.

- 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
ā02-18-2025 02:54 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-19-2025 03:08 AM
Hi @Ankita Kolhe
What is your problem statement, if above doesn't work in scoped app, you can fetch the currently logged in user and glide against "sys_user_has_role" table.
Aman Kumar