g_user.harRole() , g_user.hasRoleExactly() and g_user.hasRoles()

Vinayak10
Kilo Guru

Hii folks 

i am confused in g_user.harRole() , g_user.hasRoleExactly() and g_user.hasRoles() . what is exactly difference between this 3 methods?

5 REPLIES 5

Nithin12
Tera Expert

Hi Vinayak,

 

All these three are used for checking whether the user has role are not with slight difference.

g_user.hasRole():True if the current user has the role specified, false otherwise. ALWAYS returns true if the user has the 'admin' role.

Usage: g_user.hasRole('itil')

 

g_user.hasRoleExactly():True if the current user has the exact role specified, false otherwise, regardless of 'admin' role.

Usage: g_user.hasRoleExactly('itil')

 

g_user.hasRoles():True if the current user has at least one role specified, false otherwise.

Usage: g_user.hasRoles('itil','admin')

 

 

 

Hope this clears you.

 

#Copied form this link:

https://www.servicenowguru.com/scripting/user-object-cheat-sheet/ 

 

Thanks,

Nithin.

 

https://servicenowguru.com/scripting/user-object-cheat-sheet/

 

see if this post helps:

kimkal
Tera Contributor

 g_user.hasRoles('itil','admin') is not a correct way to use.

 

hasRoles(Boolean includeDefaults)
Returns true if the current user has any role.

var yesRole = g_user.hasRoles();

The SN Nerd
Giga Sage
Giga Sage

The ServiceNow API is documented at developer.service-now.com, including g_user.

There is actually 4 methods:

hasRole()

hasRoleExactly()

hasRoleFromList()

hasRoles()

 

 


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022