gs.hasRole('anyrole') return true for admin

Jagadeesh_Maddi
Tera Contributor

can anyone please tell me why gs.hasRole('anyrole') is giving true for the admin and not for the other and also how to override this case that it must apply for admin too can we actually do that ? 

2 ACCEPTED SOLUTIONS

Chaitanya ILCR
Mega Patron

Hi @Jagadeesh_Maddi ,

yes it's returns true for admin even when the role doesn't exist and it's the expected behaviour

 

GlideSystem - Global--> User has at least one of the passed-in roles. Also returns true if the user has the administrator role. 

 

to check if the user actually have the role you can use this 

 

gs.getUser().hasAssignedRole()

ChaitanyaILCR_0-1768923727785.png

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

View solution in original post

Mohammed8
Giga Sage

Hi @Jagadeesh_Maddi 

Admin has other roles too so it returns true.

Server side we don't have gs.hasRoleExactly hence this cant be used.

Here is community answer which can be used as workaround:

https://www.servicenow.com/community/developer-forum/gs-hasroleexactly-in-server-side-script/m-p/282...

 

Thanks and Regards,

Mohammed Zakir

View solution in original post

4 REPLIES 4

GlideFather
Tera Patron

Hi @Jagadeesh_Maddi,

 

because hasRole() is not evaluating whether it is directly assigned or inherited...

 

if you will use hasRoleExactly('yourRole'); then it will most likely be false, unless it is assigned manually or from group membership and not inherited from having admin...

 

Let me know if that worked for you

_____
No AI was used in the writing of this post. Pure #GlideFather only

Ankur Bawiskar
Tera Patron

@Jagadeesh_Maddi 

admin inherits every role and hence it gives you true

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Chaitanya ILCR
Mega Patron

Hi @Jagadeesh_Maddi ,

yes it's returns true for admin even when the role doesn't exist and it's the expected behaviour

 

GlideSystem - Global--> User has at least one of the passed-in roles. Also returns true if the user has the administrator role. 

 

to check if the user actually have the role you can use this 

 

gs.getUser().hasAssignedRole()

ChaitanyaILCR_0-1768923727785.png

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

Mohammed8
Giga Sage

Hi @Jagadeesh_Maddi 

Admin has other roles too so it returns true.

Server side we don't have gs.hasRoleExactly hence this cant be used.

Here is community answer which can be used as workaround:

https://www.servicenow.com/community/developer-forum/gs-hasroleexactly-in-server-side-script/m-p/282...

 

Thanks and Regards,

Mohammed Zakir