Get All Of The Groups For A User

This script will print all of the groups a member is in, in ServiceNow. Below, we query the [sys_user_grmember] table and find all of the groups where the user field is the sys_id of a …

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)

sys_user_grmember



Buy The "ServiceNow Developer's Manual" Now

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



This script will print all of the groups a member is in, in ServiceNow.

Below, we query the [sys_user_grmember] table and find all of the groups where the user field is the sys_id of a specific user.

In the example below, gs.getUserID() is used for the current user, change this value to be any user’s sys_id.

gs.getUserID(); // returns the sys_id of the logged in user

Here is the full script:

var user = gs.getUserID(); // gets current users sys_id
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("user", user);
gr.query();

var arr = [];

while (gr.next()) {
    arr.push(gr.group.getDisplayValue())
}

gs.print("All groups: " + arr);

When this is executed, it will print an array of groups that the user is a member of.

You can change this to get the groups of any user, by changing the sys_id of the variable user, in the script to any user’s sys_id.

var user = "5137153cc611227c000bbd1bd8cd2005"; //a sys_id
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("user", user);
gr.query();

var arr = [];

while (gr.next()) {
    arr.push(gr.group.getDisplayValue())
}

gs.print("All groups: " + arr);

What other related scripts are helpful for ServiceNow Admins?

How would you take this script and improve upon it?

We’d love to hear your thoughts below.

1 thought on “Get All Of The Groups For A User”

Leave a Comment

Product Image

Linda just purchased A ServiceNow Developer’s Manual 58 Minutes ago from New Orleans LA.

Buy Now