addJoinQuery example

mreaves
Giga Contributor

I need to populate a list ('u_myList') with all the user names from a single sys_user_group.
I presume the correct way to do this is using an addJoinQuery as suggested in the WIKI at: http://wiki.servicenow.com/index.php?title=GlideRecord

8.5 addJoinQuery

public QueryCondition addJoinQuery(joinTable, [primaryField], [joinTableField])
Adds a filter to return records based on a relationship in a related table. For example, find all the users that are in the database group (users via sys_user_grmember table).

I'm having trouble figuring out how to go about this.
My code creates a new glide record of all users then adds a join query (sub query) via sys_user_grmember.
The field from sys_user.user = sys_user_grmember.user.

I don't get it, there's actually three tables sys_user, sys_user_group, sys_user_grmember.
The sys_user_grmember table actually contains sys_id's.

I can deal with populating the list once I get a glide record with the right entries.
Any assistance would be greatly appreciated.

Mike

1 REPLY 1

benn23
ServiceNow Employee
ServiceNow Employee

You can run your query against the sys_user_grmember table. That's the table that joins users with groups:

var myGroup = "Service Desk";
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('group.name',myGroup);
grp.query();
while(grp.next()){
gs.print("Member Name: " + grp.user.getDisplayValue());
gs.print("Member Sys_Id: " + grp.user);
}