reference field to return only users from a specific group

JJG
Kilo Guru

Hello, 

I have a reference field to the 'sys_user' table with the name 'name_of_recruiter'. I want the reference field to only show users who are in the recruiting group. However, it is returning every user. Is my script incorrect?

Script include:

var GetGroupMember= Class.create(); 

GetGroupMember.prototype = { 

getMember : function() { 

var x = 'e4d875a8db9a5300e21cfd131d961992'; //This is the sys_id of the recruiter group 

var user_array = []; 

var getMembers = new GlideRecord('sys_user_grmember'); 

getMembers.addQuery('group',x); 

getMembers.query(); 

while(getMembers.next()) { 

user_array.push(getMembers.user + ''); 

}

 return user_array.toString(); 

}};

Reference qualified in the 'sys_user' reference field 'name_of_recruiter'

 javascript:new GetGroupMember().getMember();

1 ACCEPTED SOLUTION

Ahhh it's Scoped!

Try with:

var GetGroupMember= Class.create(); 
GetGroupMember.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

	getMember : function() { 

		var x = 'e4d875a8db9a5300e21cfd131d961992'; //This is the sys_id of the recruiter group 
		var user_array = []; 

		var getMembers = new GlideRecord('sys_user_grmember'); 
		getMembers.addQuery('group',x); 
		getMembers.query(); 

		while(getMembers.next()) { 
			user_array.push(getMembers.user + ''); 
		}

		gs.info('sys_idIN' + user_array.toString());
		return 'sys_idIN' + user_array.toString(); 
	
	},

    type: 'GetGroupMember'
	
});

Note there's also a gs.info for debugging purposes. So you could check the logs which query is returned and if it's what you would expect.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

13 REPLIES 13

Mark Roethof
Tera Patron
Tera Patron

Hi there,

There's a small error in your return.

Change:

return user_array.toString();

Into:

return 'sys_idIN' + user_array.toString(); 

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hey Mark,

Just made the necessary changes but it's not working, something must be wrong with my field/script include settings (See pics below)

 

find_real_file.png

find_real_file.png

Ahhh it's Scoped!

Try with:

var GetGroupMember= Class.create(); 
GetGroupMember.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

	getMember : function() { 

		var x = 'e4d875a8db9a5300e21cfd131d961992'; //This is the sys_id of the recruiter group 
		var user_array = []; 

		var getMembers = new GlideRecord('sys_user_grmember'); 
		getMembers.addQuery('group',x); 
		getMembers.query(); 

		while(getMembers.next()) { 
			user_array.push(getMembers.user + ''); 
		}

		gs.info('sys_idIN' + user_array.toString());
		return 'sys_idIN' + user_array.toString(); 
	
	},

    type: 'GetGroupMember'
	
});

Note there's also a gs.info for debugging purposes. So you could check the logs which query is returned and if it's what you would expect.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

It is throwing an error:

Invalid query detected, please check logs for details [Unknown field null in table sys_user]