- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2021 09:41 PM
I am not getting clarity while practicing this. what is the difference between the hasRole(), hasRoles() and hasRoleExactly() methods in g_user()
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2021 01:13 AM
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2021 09:46 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2021 10:06 PM
Here is an amazing article by
https://community.servicenow.com/community?id=community_article&sys_id=2750d18ddb85a0109e691ea6689619cc
Please mark this comment as Helpful/Correct answer.
Cheers,
Hardit Singh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2021 12:49 AM
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):
User AM (only asset role):
User UI (ITIL role):
User Abel (no role):
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2021 01:00 AM
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
Please mark this comment as Helpful/Correct answer.