Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

script to return all reference of reference list field

Lion Kesler
Tera Contributor

Hi
I have a field of type reference list
I am trying to write a script that returns me all the SYS ID
For example -
In the groups table I have a record named X and Y and Z are registered
And record Y has a record U and Q
Finally I want to output an array of X,Y,Z,Q,U

LionKesler_0-1732156007108.png

 

LionKesler_1-1732156023166.png

 

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

This should get you started - assuming Secondary Group is a list field referencing the groups table:

var grpArr = [];
var grpGr = new GlideRecord('sys_user_group');
//addQuery criteria?
grpGr.query();
while (grpGr.next()) {
	grpArr.push(grpGr.sys_id.toString());
	if (grpGr.u_secondary_group) { //custom list field name
		grpArr.push(grpGr.u_secondary_group.toString().split(','));
	}
}
var arrayUtil = new ArrayUtil();
var uniqueArray = arrayUtil.unique(grpArr);