DrewW
Mega Sage

Recently I have had to create some scripts that gets all of the user records in a group and update them with some information.  In the past I have done a query of the sys_user_grmember table, stored the data in an array and then queryed the sys_user table.  So I started looking at a better way to do this so I did not have to do two loops in my code and I came up with using a related list query.  I also needed something like this for the Assigned to field of a custom table that was not going to use the assignment group field.

So these two examples will get all of the users in a group but you could have it return all the users in several groups if you wanted.

//Get users based on sys_id of group
var gr = new GlideRecord("sys_user");
gr.addEncodedQuery("^RLQUERYsys_user_grmember.user,>=1,m2m^group=SYS_ID_OF_GROUP^ENDRLQUERY");
gr.query();
while (gr.next()) {
	gs.print(gr.getValue("user_name"));
}

//Get users based on name of group
var gr = new GlideRecord("sys_user");
gr.addEncodedQuery("^RLQUERYsys_user_grmember.user,>=1,m2m^group.name=NAME_OF_GROUP^ENDRLQUERY");
gr.query();
while (gr.next()) {
	gs.print(gr.getValue("user_name"));
}

If you are unsure of the query format you can install the "SN Utils - Tools for ServiceNow" plugin for Chrome or FireFox then you can go to the reporting app in your ServiceNow instance and devise the query using the related list condition.  Run the report then double click on the white space at the end of the breadcrumb/filter and a dialog will open that has the query.

ServiceNow has some documentation on Related list querys but its not much.
https://docs.servicenow.com/bundle/orlando-platform-user-interface/page/use/using-lists/concept/c_EncodedQueryStrings.html

 

Comments
Allen Andreas
Tera Patron

Hi,

I believe this may only work if you enabled related list querying though, correct?

Just wanted to add this:

------

Note: Related list queries must be enabled in System PropertiesList v3. Select the Allow related list query conditions to be added through the filter check box to enable it.

You can build a related list query for a list that uses List v2, however, the filter conditions cannot be modified until you removed the related list condition in the breadcrumb.

------

Please mark reply as Helpful, if applicable. Thanks!

 

DrewW
Mega Sage

What they are warning you about is that if you use a URL and the query you give it uses the Related list querying you will not be able to open the breadcrumb to edit it until you remove the related list part of the query.  So basically its a user interface issue but the backend will use it properly regardless.

We do not have List v3 enabled and it works fine in the Reporting App which has the functionality built in whether or not its enabled which is why I went there to build the first query to get a copy for the correct formatting I needed.

Satiir Stevenso
Tera Contributor

Hi,

 

You seem very proficient in this . I am trying to print all "male" users in the sys_user table. What function would I add in the query. 

So far I have

 

var rec = new GlideRecord('sys_user');

DrewW
Mega Sage

You need to add a filter condition for how you indicate the user is Male or not.  Something like

rec.addQuery("FIELD THAT INDICATES SEX", "VALUE YOU NEED TO LOOK FOR");

 

Satiir Stevenso
Tera Contributor

var rec = new GlideRecord ('sys_user');

rec.addQuery("gender", "Male");

rec.query();

while 9rec.next() ) {

 gs.print('user name' + rec.name);

 

does this make sense?

Version history
Last update:
‎04-14-2020 01:21 PM
Updated by: