How To Use gs.getUser().isMemberOf()

When scripting in ServiceNow, you’ll frequently have to create conditional statements around users and their group membership. Based upon what group a certain user is in, we want to either allow or even disallow certain …

Buy The 'ServiceNow Developer Manual' Here!

Snowball - ServiceNow Developer Manual

Newsletter Signup

Want to get better at ServiceNow?
(One email per month, no fluff - only value)

isMemberOf()



Buy The "ServiceNow Developer's Manual" Now

We've packed over a decade of ServiceNow Development advice, into a single e-book.



When scripting in ServiceNow, you’ll frequently have to create conditional statements around users and their group membership.

Based upon what group a certain user is in, we want to either allow or even disallow certain behaviors and levels of access.

Specifically, when looking at a user’s groups, you can easily tell if a user is a member of a certain group using a built-in ServiceNow method, called isMemberOf().

How To Tell If A User Is A Member Of A Specific Group

When using isMemberOf(), keep in mind that you can only use this on the server side in ServiceNow. So you can use it in business rules, script includes, ACL’s, etc.

First, you need to access the User Object in ServiceNow.

You do this by writing:

gs.getUser(); // The User Object

Now that we have the user object accessible, we’ll then use the isMemberOf() method.

isMemberOf() will only ever return true, or false.

If the user is a member of the group being passed as the parameter, then it will return true, if the user is NOT a member of the group, it will return false.

The isMemberOf(type string, Group Name) method takes one parameter, and that is a group name in a string.

So you’d pass something like, “Service Desk” in as the parameter.

Which would look like this:

gs.getUser().isMemberOf("Service Desk");

If you want to test this run this in a background script.

gs.print(gs.getUser().isMemberOf("Service Desk"));

Say you wanted to do something, when a user is a member of a certain group, and not do something if the user was not in the group.

You would then do something like this:

if (gs.getUser().isMemberOf("Service Desk")) { // returns true or false
// User is a member of the group, do XYZ
}
else {
// This user is NOT in the group, so do ABC
}

You can also do things like find all of the groups a user is a member of, by utilizing the script outlined here:

Learn More About The User Object – gs.getUser()

If you want to see all of the other methods in ServiceNow for the user object, check out this post:

Leave a Comment

Product Image

Kelly just purchased A ServiceNow Developer’s Manual 10 Minutes ago from Tucson AZ.

Buy Now