Is the gs.hasRoleExactly() method work in servicenow scripting or do we have any alternate
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-19-2025 03:39 AM - edited ā03-19-2025 11:29 PM
Is the gs.hasRoleExactly() method work in servicenow scripting or do we have any alternate method for this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-19-2025 06:25 AM
Hi @tahor , yes ServiceNow has this method available in scripting. You can try in background script to try it out!
If you feel this was helpful, please consider giving thumbs up and if it solved your issue, please mark this correct.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-19-2025 07:16 AM
Hi @tahor ,
Use gs.hasRole() when you are working on the server side (e.g., Business Rules, Script Includes)
Use g_user.hasRoleExactly() when you are working with client-side scripts (e.g., Client Scripts, UI Scripts)
We don't have gs.hasRoleExactly() at server side.
Kindly mark it as helpful/correct if it solves your issue.
Best Regards,
Pooja
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-19-2025 07:25 AM
@tahor Some additional methods and their differences,
Server-Side Methods:
Server-side scripting in ServiceNow is typically done in Business Rules, Script Includes, Scripted REST APIs, etc. Here are the methods you can use to check user roles:
gs.hasRole(roleName),gs.getUser().hasRole(roleName)
Client-Side Methods:
Client-side scripting in ServiceNow is typically done in Client Scripts, UI Policies, UI Actions, etc. Here are the methods you can use to check user roles:
g_user.hasRole(roleName), g_user.hasRoleExactly(roleName),g_user.getRoles()
Just an example use case for your understanding.
g_user.hasRole('itil') True if the current user has the role specified, false otherwise. ALWAYS returns true if the user has the 'admin' role.
So for admin it will return true
hasRoleExactly('itil') True if the current user has the exact role specified, false otherwise, regardless of 'admin' role.
Will return false for admin.
Mark my response as helpful if it really helped you resolve your doubts.
Thanks,
Mahesh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-19-2025 07:57 AM
gs.getUser().hasRoleExactly(role) (Alternative)
- This method is used via the gs.getUser() object to check if the user has the exact role
if (gs.getUser().hasRoleExactly('admin')) {
gs.info('User has exactly the admin role');
}
Please mark correct/helpful if this helps you