Reference qualifier for group variable to display only groups for the selected user

Ogrecic
Kilo Contributor

Hi All!

I'm currently building a catalog item for a request to remove user from groups. There is firstly a user field from sys_user table and then groups field. What I'm trying to do is to link these 2 fields so when the user is selected, in the groups field would appear only the groups that this particular user is part of. Yet no success. Any ideas? 

Thank you in advance. 

Best regards,

Oleg.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

You will have to create reference qualifier for the 2nd variable and call script include function

how to call script include in 2nd variable

javascript: 'sys_idIN' + new GetGroupMembers().getGroups(current.variables.userVariableName);

Script Include:

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

	getGroups:function(user) { 
		
		var group_array = [];
		var getMembers = new GlideRecord('sys_user_grmember');
		getMembers.addQuery('user ',user);
		getMembers.query();
		while(getMembers.next())
		{
			var groupSysId = getMembers.getValue('group');
			group_array.push(groupSysId);
		}
		return group_array;
	},

	type: 'GetGroupMembers'
};

Regards
Ankur

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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

You will have to create reference qualifier for the 2nd variable and call script include function

how to call script include in 2nd variable

javascript: 'sys_idIN' + new GetGroupMembers().getGroups(current.variables.userVariableName);

Script Include:

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

	getGroups:function(user) { 
		
		var group_array = [];
		var getMembers = new GlideRecord('sys_user_grmember');
		getMembers.addQuery('user ',user);
		getMembers.query();
		while(getMembers.next())
		{
			var groupSysId = getMembers.getValue('group');
			group_array.push(groupSysId);
		}
		return group_array;
	},

	type: 'GetGroupMembers'
};

Regards
Ankur

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

Hello Ankur, 

Thank you very much! It works. 

Best regards,

Oleg.

Glad to help.

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

@Ankur Bawiskar 

Hi Ankur,

We exactly have the same requirement to populate groups based on the user variable on the catalog form. We follow the above same steps in Quebec release and it is not working. Can you please suggest?

 

Regards

Muktha