what is the difference between the hasRole(), hasRoles() and hasRoleExactly() methods in g_user()?

rani3
Kilo Contributor

I am not getting clarity while practicing this.  what is the difference between the hasRole(), hasRoles() and hasRoleExactly() methods in g_user()

1 ACCEPTED SOLUTION

Raghu Ram Y
Kilo Sage

Hello @rani,  

I will make you understand in simple words.

g_user.hasRole(' ')

1. This method return true if the current logged in user has the role which we have provided in (' ')  otherwise false.

2. It will always returns true if the user has the 'admin' role.

Ex: 1. Consider in the above method if you provide "itil" role and some "abc" user is loggedin to the instance and the user having 'itil' role then it returns true

2. If the user having admin role then also it returns true

 

g_user.hasRoles()  " This method  return true if current logged in user has any single role atleast.

Ex:  Consider in the above method if you provide "itil" ,"service desk" roles and some "xyz" user is loggedin to the instance and the user having either of 'itil' or 'service desk' role then it returns true

 

g_user.hasRoleExactly(‘ ’)  This method return true if current logged in user has role we which have provided in (' ')

Ex:  1. This method return true if the current logged in user has the role which we have provided in (' ')  otherwise false.

2. It will returns false even if the user having 'admin' role.

 

If my answer clarifies your doubt then please "Mark it as Correct/Helpful"

View solution in original post

7 REPLIES 7

Murthy Ch
Giga Sage

Hi rani

Here is the Article where explains everything on the same.

 

 

-Murthy

 

Thanks,
Murthy

Community Alums
Not applicable

Here is an amazing article by @Gaurav Shirsat .

https://community.servicenow.com/community?id=community_article&sys_id=2750d18ddb85a0109e691ea6689619cc

Please mark this comment as Helpful/Correct answer.

Cheers,

Hardit Singh

Hitoshi Ozawa
Giga Sage
Giga Sage

Easy way is to just test it out.

User hasRole hasRoles hasRoleExactly
admin true true false
AM (only asset) false true false
UI (itil) true true true
Abel (no role) false false false

Client script:

function onLoad() {

	g_form.setValue('hasrole', g_user.hasRole('itil').toString());
	g_form.setValue('hasroles', g_user.hasRoles('itil', 'asset').toString());
	g_form.setValue('hasroleexactly', g_user.hasRoleExactly('itil').toString());
}

User Admin (admin role):

find_real_file.png

User AM (only asset role):

find_real_file.png

User UI (ITIL role):

find_real_file.png

User Abel (no role):

find_real_file.png

Shamal2
Giga Guru

g_user.hasRole(' ') :-  this method return 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.

 

For more info you can refer this article

https://community.servicenow.com/community?id=community_article&sys_id=2750d18ddb85a0109e691ea668961...

 

Please mark this comment as Helpful/Correct answer.