What It Does
The hasRole() method performs a role membership check for the current user session. It queries the user's role assignments through the sys_user_has_role table and any roles inherited through group membership. The method handles both direct role assignments and inherited roles from groups the user belongs to.
Internally, ServiceNow maintains a role cache for each user session to avoid repeated database queries. When you call hasRole(), the platform first checks this cache. If the cache is empty or stale, ServiceNow builds the complete role list by traversing user-to-role assignments, group memberships, and inherited roles from parent groups.
The method returns true if the user has the specified role, false if they don't. It also returns false if the role name doesn't exist in the system or if you pass an empty string or null. The method never returns null or throw exceptions — it always returns a boolean.
Users with the admin role always return true for any role check, regardless of their actual role assignments. This is ServiceNow's built-in admin bypass behavior.
The role check is case-sensitive and matches against the role's name field, not the label or sys_id. Related methods in the GlideSystem class include getRoles() which returns all roles for the current user, and getUser() which returns the current user's GlideUser object with its own role-checking methods.
When to Use This
Use hasRole() when you need to control script execution based on the current user's permissions. Common scenarios include Business Rules that should only fire for certain roles, Script Includes that need role-based logic, or Scheduled Jobs that verify the session user before performing sensitive operations. This is the standard method for role checking in server-side scripts.
Don't use hasRole() for checking roles of users other than the current session user — use GlideUser.hasRole() on a specific user object instead. Avoid using this for UI control — client-side role checks can be bypassed and should be supplemented with ACLs. Also avoid checking for the admin role specifically, since admin users return true for all role checks anyway.
Return Value
Returns a boolean value — true if the current user has the specified role or is an admin, false in all other cases. The method handles error conditions gracefully by returning false for invalid role names, empty strings, or null values. You can safely use the return value directly in conditional statements without null checking.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.
Platform Behavior & Side Effects
- No database writes occur — this is a read-only operation that only queries role assignments
- Uses cached role information for performance — subsequent calls in the same session are very fast
- No Business Rules, notifications, or audit records are triggered by role checking
- Works identically in Business Rules (before/after), Script Includes, and Scheduled Jobs
- Does not trigger ACL evaluation — this is a direct role membership check
- Role cache may be rebuilt if user group memberships have changed during the session
- Admin users bypass the actual role lookup entirely — admin check happens first and returns true immediately