How hasRole, hasRoles, hasRoleExactly Differentiates?

Hello,

Welcome to ServiceNow Spectaculars.

In this article, we provide the differences between three similar functions of the glide system class of ServiceNow. It’s important to understand the difference between these three functions, i.e., hasRole vs. hasRoles vs. hasRoleExactly.

gs.hasRole()gs.hasRoles()gs.hasRoleExactly()
This method checks if a user has a specific role assigned to them.When you need to validate whether a user has multiple roles, hasRoles comes to the rescue.In some cases, you may require strict role matching. 
It’s perfect for scenarios where you want to verify if a user possesses a particular role before granting access to certain features or functionality.This function allows you to verify if a user holds any of the specified roles. It’s handy when you want to grant access based on a set of roles rather than a single one.This is where HasRoleExactly shines It checks if a user has the exact set of roles provided, ensuring that no additional roles are present.
Use hasRole to perform role-based checks for individual rolesUtilize hasRoles when validating multiple roles.Use hasRoleExactly when you need precise role-based authorization.

gs.hasRole() example

gs.hasRoles() exampe

gs.hasRoleExactly()

Leave a Reply