gs.hasRole() syntax for multiple roles

Adamya Singh Pa
Tera Contributor

Hi,

I wanted to apply an OR condition for multiple roles using gs.hasRole() method.

However, I have a confusion about the syntax for the same :

gs.hasRole('role1' , 'role2' ); or gs.hasRole('role1,role2');

I am getting different results from these two syntaxes. Let me know what the difference is, and which is the correct one to check whether role1 OR role2 is present for the user.

Thanks!

1 ACCEPTED SOLUTION

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Adamya Singh Pa ,

GunjanKiratkar_0-1678438151662.png

 

Note : The hasRole() returns true, if the user has the role specified (e.g. hasRole(role)) OR the admin role.

Reference :- https://servicenowguru.com/scripting/user-object-cheat-sheet/ 

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

View solution in original post

6 REPLIES 6

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Adamya Singh Pa ,

GunjanKiratkar_0-1678438151662.png

 

Note : The hasRole() returns true, if the user has the role specified (e.g. hasRole(role)) OR the admin role.

Reference :- https://servicenowguru.com/scripting/user-object-cheat-sheet/ 

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Pavankumar_1
Mega Patron

Hi @Adamya Singh Pa ,

second one is correct. refer below

 

if(gs.hasRole('role1,role2,role3')){
//If user has anyof the role it will return true 
}

or
if(gs.hasRole('role1') || gs.hasRole('role2')){
//If user has anyof the role it will return true 
}

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Ankur Bawiskar
Tera Patron
Tera Patron

@Adamya Singh Pa 

you cannot pass 2 parameters in that function.

it can accept only 1 role name

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Are you sure about that? I came across a couple of articles with multiple roles as input.