Filter Reference Field Based on User's Groups

jmiskey
Kilo Sage

We are building a Form in ServiceNow, where certain users can upload attachments that will be attached to certain Service Catalog Items.

So, we currently have a Control table, that tracks the Catalog Items that we allow this for, and the Group that can upload these attachments.

So, our Control Table has these fields:

- Catalog Items (reference to sc_cat_item)

- Group (reference to sys_user_group)

This control table will be maintained by us Admins.

So, we have this "My Attachments" table that the specified users will have access to in order to upload their files.  The key field in this table is "Catalog Item", which is a reference to the Control Table that we created, so it only shows the items we included in that table.

However, we need to limit this further.  We would like to filter this "Catalog Item" field in the "My Attachments" table, so that it only shows records where the "Group" field in the Control Table matches one of the Groups that the logged in User is a member of. 

How do I do this?

Thanks

1 ACCEPTED SOLUTION

jmiskey
Kilo Sage

So I did need to write my own function in a Client Script in order to get it to work.

The function looks like this:

	getUserGroups: function(){  
		//return as criteria string of all groups a member is part of (each one separated by a comma)
		var groups='';

		//get current user
		var usr = gs.getUserID();

		//query the group member table to get all groups user is part of
		var grps = new GlideRecord('sys_user_grmember');
		grps.addQuery('user', usr);
		grps.query();
     
		//write all groups to string
		while(grps.next()) {
			groups += (',' + grps.group);
		}

		//return criteria string of all groups separated by commas
		return 'u_groupIN' + groups;
	},

I have it within a large Script Includes we have called "getUserInformation".

So, then we just use the following Advanced Reference Qualifier on that field:

javascript: new getUserInformation().getUserGroups()

And it does what we want.

Thanks to all for the tips/tricks!

 

View solution in original post

5 REPLIES 5

jmiskey
Kilo Sage

So I did need to write my own function in a Client Script in order to get it to work.

The function looks like this:

	getUserGroups: function(){  
		//return as criteria string of all groups a member is part of (each one separated by a comma)
		var groups='';

		//get current user
		var usr = gs.getUserID();

		//query the group member table to get all groups user is part of
		var grps = new GlideRecord('sys_user_grmember');
		grps.addQuery('user', usr);
		grps.query();
     
		//write all groups to string
		while(grps.next()) {
			groups += (',' + grps.group);
		}

		//return criteria string of all groups separated by commas
		return 'u_groupIN' + groups;
	},

I have it within a large Script Includes we have called "getUserInformation".

So, then we just use the following Advanced Reference Qualifier on that field:

javascript: new getUserInformation().getUserGroups()

And it does what we want.

Thanks to all for the tips/tricks!