Is the gs.hasRoleExactly() method work in servicenow scripting or do we have any alternate

tahor
Tera Contributor

Is the gs.hasRoleExactly() method work in servicenow scripting or do we have any alternate method for this?

4 REPLIES 4

Twinkle S
Mega Sage
Mega Sage

Hi @tahor , yes ServiceNow has this method available in scripting. You can try in background script to try it out!

 

If you feel this was helpful, please consider giving thumbs up and if it solved your issue, please mark this correct.
Thanks

Pooja58
Kilo Sage
Hi @tahor ,

Use gs.hasRole() when you are working on the server side (e.g., Business Rules, Script Includes)
Use g_user.hasRoleExactly() when you are working with client-side scripts (e.g., Client Scripts, UI Scripts)

We don't have gs.hasRoleExactly() at server side.

Kindly mark it as helpful/correct if it solves your issue.


Best Regards,

Pooja

maheshkhatal
Mega Sage

@tahor Some additional methods and their differences,

Server-Side Methods:
Server-side scripting in ServiceNow is typically done in Business Rules, Script Includes, Scripted REST APIs, etc. Here are the methods you can use to check user roles:
gs.hasRole(roleName),gs.getUser().hasRole(roleName)
Client-Side Methods:
Client-side scripting in ServiceNow is typically done in Client Scripts, UI Policies, UI Actions, etc. Here are the methods you can use to check user roles:
g_user.hasRole(roleName), g_user.hasRoleExactly(roleName),g_user.getRoles()

Just an example use case for your understanding.

g_user.hasRole('itil') True if the current user has the role specified, false otherwise. ALWAYS returns true if the user has the 'admin' role.

So for admin it will return true

hasRoleExactly('itil') True if the current user has the exact role specified, false otherwise, regardless of 'admin' role.

Will return false for admin.

 

Mark my response as helpful if it really helped you resolve your doubts.

Thanks,

Mahesh.

 

sunil maddheshi
Tera Guru

@tahor 

gs.getUser().hasRoleExactly(role) (Alternative)

  • This method is used via the gs.getUser() object to check if the user has the exact role

 

if (gs.getUser().hasRoleExactly('admin')) {
    gs.info('User has exactly the admin role');
}

 

Please mark correct/helpful if this helps you