Reference qualifier with a script include ‐ filtering User to Group Member - jcmings/sn GitHub Wiki

I recently had a use case to filter my User [sys_user] reference field to specific members of a group. I'm hosting an application on a larger, shared instance, and I don't want to to have any other users appearing in my dropdown. So I've created a few user groups (as one should) and am filtering down the User [sys_user] list to my group members.

It's actually pretty simple to do. I set it up with a Script Include. Check it out:

Script Include setup

Create your script include on the Script Include [sys_script_include] table. I'm only including one function because I won't be super strict with my filter.

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

	getAllGroupMembers: function() {

		var userList = '';
		var groupGr = new GlideRecord('sys_user_grmember');
		groupGr.addEncodedQuery('group.nameSTARTSWITHGroup Name');
		groupGr.query();
		while (groupGr.next()) {
			userList += ("," + groupGr.user.sys_id.toString());
		}
		return userList;
	},

    type: 'ScriptIncludeName'
}; 

I've genericized this a little bit, and I'm calling my script include ScriptIncludeName and my function getAllGroupMembers. For the sake of the example, my groups all begin with Group Name.

Setting up the ref qual

Next, head to your reference field on your table. For mine, I'm referencing the User [sys_user] table on a field I've called Meeting owner. In the Reference Specification tab, I've set Use reference qualifier to Advanced and have pasted in a custom Reference qual.

It looks like this: image

And here's my code:

javascript: 'sys_idIN' + new scope.ScriptIncludeName().getAllGroupMembers();

Obviously, you'd replace scope.ScriptIncludeName() with your Script Include API Name (which is found on the Script Include record). And you'd replace getAllGroupMembers() with your function name of choice.

Give this a test, and you should be good to go.

How this all works

Our script include is looking at the Group Member [sys_user_grmember] table and returning a list of User [sys_user] sys_id's. Our reference qualifier is basically an Encoded Query. So we're just saying show me the users whose sys_id's are in this list.

Troubleshooting

Make sure you have the function statements (()). Make sure you have new ahead of your script include. And make sure you include the plus (+) character so you don't confuse the ref qual with javascript.