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.

Get the groups in reference field which the user contains

Ashok32
Tera Contributor

Hi,

 

I have a requirement like ,I have a two variable in a form "Requested for" and " groups are" both are reference fields.

When i select any user in "requested for" variable then in the "groups" variable it only shows the groups which the selected user belongs.

 

Thanks in advance.

3 REPLIES 3

Yousaf
Giga Sage

Hi Ashok,

Try this
Reference qualifier for group 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'
};

 

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

 

Mark Correct and Helpful if it helps.


***Mark Correct or Helpful if it helps.***

This might be Old post but just adding other option, so that some one can use it , 

 

In the Reference variable , Select group member table and use the group sysID , this will fix the issue 

RudhraKAM_0-1689942149935.png

 

 

Shamma Negi
Kilo Sage
Kilo Sage

Hi,

 

Please select Advanced Reference Qualifier on group field. As it will take the user from the requested for field and show only those groups in which user is added.

Put this in Advance Reference Qualifier condition on group field:

javascript: 'sys_idIN' + new CheckGroupMember().getGroups(current.variables.VariableRequestedFor);

 

Create a script Include:

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

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

	type: 'CheckGroupMember'
};

It will give you the result.

 

Hope this helps.

 

Regards,

Shamma

 

Regards,Shamma Negi