ServiceNow's role system has a critical blind spot that trips up most developers: the sys_user_has_role table only contains directly assigned roles, not roles inherited through group membership. A user might have full admin access through group inheritance, but querying sys_user_has_role will show nothing. You need both the table query for direct assignments and gs.getUser().getRoles() for the complete effective role set.
When to use this
- Building role audit reports that need to show both direct and inherited assignments
- Implementing custom access controls that need the complete role picture
- User onboarding/offboarding workflows that track role assignment sources
- Debugging permission issues where you need to trace role inheritance paths
When NOT to use this
- Don't call this in a Business Rule that fires on user updates β you'll create infinite loops when role changes trigger user updates
- Don't use this for simple role checks β use
gs.hasRole()instead for better performance - Don't run this in batch operations over hundreds of users β the
gs.getUser()calls will kill performance - Don't use this from client-side scripts β wrap it in a GlideAjax call instead
Key behaviors and gotchas
- The
sys_user_has_roletable only shows direct role assignments β group-inherited roles exist only in memory - The
gs.getUser()method requires a username, not a sys_id β you must look up the username first - Role inheritance calculations respect ACLs β you might see different results based on who's running the script
- Inactive users still show in
sys_user_has_rolebutgs.getUser()returns limited data for them - Domain separation affects both direct and inherited role visibility β cross-domain roles may be filtered out
Never assume sys_user_has_role gives you the complete picture. I've seen production security bugs where developers only checked direct roles and missed users with admin access through group inheritance. Always validate against gs.getUser().getRoles() for security-sensitive operations.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros β scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.