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.

How to add Script include in Reference Qualifier

Abdul333
Tera Contributor

Hello All,

 

I created a custom field called "Special Group".

Field type "List":

Reference to "sys_user" table

In that field we need to show only a particular group members.

Abdul333_1-1701691970496.png

 

I written Script include like this:

 

var GetUsersOfGroup = Class.create();
GetUsersOfGroup.prototype = {
initialize: function() {
},

getUsersFromAppEngineAdminsGroup: function() {
var userList = [];

var groupGr = new GlideRecord('sys_user_grmember');
groupGr.addEncodedQuery('group=477a05d153013010b846ddeeff7b1225');// sys_id of App Engine Admins group
groupGr.query();

while (groupGr.next()) {
var user = new GlideRecord('sys_user');
if (user.get(groupGr.user)) {
userList.push(user.name.toString()); // Assuming name is used as the reference field
}
}

return userList;
},

type: 'GetUsersOfGroup'
};

 

how should i call this in that field "Special group" reference qualifier so that only

Please correct me if i did anything wrong.

2. In the list it showing sys_id's, how should we show user "names instead of sys_ids"?

 

@Ankur Bawiskar @SANDEEP28 @Community Alums 

 

 

Thanks,

Abdul

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Abdul333 

update script include as this

var GetUsersOfGroup = Class.create();
GetUsersOfGroup.prototype = {
	initialize: function() {
	},

	getUsersFromAppEngineAdminsGroup: function() {
		var userList = [];

		var groupGr = new GlideRecord('sys_user_grmember');
		groupGr.addEncodedQuery('group=477a05d153013010b846ddeeff7b1225');// sys_id of App Engine Admins group
		groupGr.query();
		while (groupGr.next()) {
			userList.push(groupGr.getValue('user'));
		}

		return 'sys_idIN' + userList.toString();
	},

	type: 'GetUsersOfGroup'
};

Call it like this from the advanced ref qualifier

javascript: new GetUsersOfGroup().getUsersFromAppEngineAdminsGroup();

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Abdul333 

update script include as this

var GetUsersOfGroup = Class.create();
GetUsersOfGroup.prototype = {
	initialize: function() {
	},

	getUsersFromAppEngineAdminsGroup: function() {
		var userList = [];

		var groupGr = new GlideRecord('sys_user_grmember');
		groupGr.addEncodedQuery('group=477a05d153013010b846ddeeff7b1225');// sys_id of App Engine Admins group
		groupGr.query();
		while (groupGr.next()) {
			userList.push(groupGr.getValue('user'));
		}

		return 'sys_idIN' + userList.toString();
	},

	type: 'GetUsersOfGroup'
};

Call it like this from the advanced ref qualifier

javascript: new GetUsersOfGroup().getUsersFromAppEngineAdminsGroup();

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Abdul333 ,
I trust you are doing great.

Reference qualifier :

javascript:new GetUsersOfGroup().getUsersFromAppEngineAdminsGroup();

The updated Script Include should look like this:

 

 
var GetUsersOfGroup = Class.create();
GetUsersOfGroup.prototype = {
    initialize: function() {
    },

    getUsersFromAppEngineAdminsGroup: function() {
        var userList = [];
        var groupGr = new GlideRecord('sys_user_grmember');
        groupGr.addEncodedQuery('group=477a05d153013010b846ddeeff7b1225'); // sys_id of App Engine Admins group
        groupGr.query();
        while (groupGr.next()) {
            var user = new GlideRecord('sys_user');
            if (user.get(groupGr.user)) {
                userList.push(user.name.toString()); // Assuming name is used as the reference field
            }
        }
        return 'nameIN' + userList.join(','); // Changed sys_idIN to nameIN and array to comma-separated string
    },

    type: 'GetUsersOfGroup'
};

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



@Amit Gujarathi  @Ankur Bawiskar I added the query in Advanced reference qualifier but after saving it's again changed to simple, how should we fix this?

Abdul333_0-1701698145509.png

 

 

 

Thanks,

Abdul

@Abdul333 

ensure you give : and not : in the advanced ref qualifier

I think there is a bug in community if we add : it gets converted to : automatically

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader