How do I determine a user in a script, when the user has no roles?

Jacob Saaby Ni1
Giga Expert

Hi everyone.

When creating view rules, I'd normally go for hasRole to specify a view for an ITIL user for example.

But, how do I target users who have no roles, in my scripts?

Is it hasRole('NULL'), hasRole($null) or perhaps hasRole('')?

Or is it better to use a fourth way?

1 ACCEPTED SOLUTION

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Here:



how to check for null roles using gs.hasRole() function



gs.getUser().getUserRoles().toArray().length==0 //this means current user does not have any roles.


View solution in original post

5 REPLIES 5

Simon46
ServiceNow Employee
ServiceNow Employee

Hello Jacob,



I believe the function you're looking for is "hasRoles()".



hasRoles()
Returns true if the current user has any role.


http://wiki.servicenow.com/index.php?title=GlideUser_(g_user)#hasRoles



Kind regards,


Simon Liang


Thanks, but I wanted to opposite. When the user doesn't have ANY roles


Hello Jacob,



Then you would need to negate your condition:



if (!user.hasRoles()) {


    // do something


}



Kind regards,


Simon Liang


sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Here:



how to check for null roles using gs.hasRole() function



gs.getUser().getUserRoles().toArray().length==0 //this means current user does not have any roles.