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 …

isMemberOf()

Buy The "ServiceNow Developer's Manual" Now

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

Buy It Now

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:



What Do You Think About This Article?

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Joshua
Joshua
2 months ago

Can you add a * footnote that it also doesn’t care what other groups or roles the user is part of. If they are an admin and this is used in a condition, the condition will return false.

1
0
Would love your thoughts, please comment.x
()
x