- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2019 09:14 PM
Hi,
Is there a server equivalent of g_user.hasRoleExactly
gs.hasRole returns true for admin and userHasRoleExactly works only client side
Regards,
Sreedhar
Solved! Go to Solution.
- Labels:
-
Incident Management
- 5,892 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2019 09:52 PM
Hi Sreedhar,
you don't have server side function for that; use below code and store it in some script include; then you can call this and re-use it like from every server side script
hasRoleExactlyServerSide: function(){
var au = new ArrayUtil();
var roles = gs.getSession().getRoles() + '';
var roleArray = roles.split(",");
var isRolePresent = au.contains(roleArray, role);
return isRolePresent;
}
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2019 09:52 PM
Hi Sreedhar,
you don't have server side function for that; use below code and store it in some script include; then you can call this and re-use it like from every server side script
hasRoleExactlyServerSide: function(){
var au = new ArrayUtil();
var roles = gs.getSession().getRoles() + '';
var roleArray = roles.split(",");
var isRolePresent = au.contains(roleArray, role);
return isRolePresent;
}
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2020 11:28 AM
Hello,
I'm a little late to the party here, I know. I like your solution, Ankur, but I think you could make it even simpler.
You don't have to do the whole array work that you did.
function hasRoleExactly(roleName){
if (gs.getSession().getRoles().toString().indexOf(roleName)>0){
return true;
} else {
return false;
}
}
I know that's a little overly complex, but I like my code to be readable, and that, to me, makes it clear what it is doing. If you wanted to go really simple, where you want to do
if (gs.getUser().hasRoleExactly(roleName)){ // <--function doesn't exist
//do some code
}
you could do
if (gs.getSession().getRoles().toString().indexOf(roleName)>0){
//do something
}
and not even worry about creating a script include or a function for it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2023 08:52 PM
Thanks. That last bit worked a treat for me in the condition field of a business rule.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2023 04:10 AM
You're welcome! I'm glad it helped you!