How To Limit Reference Choices Based on Different Variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 10:41 AM
Good morning,
I am attempting to create a new catalog item with two fields, "IT Application Support Group" and "Support Group Contact"
The top box references the sys_group table, and the bottom box references the sys_user table. What I am trying to do is limit the options in the Support Group Contact reference to only show users that belong to the group selected in the IT Application Support Group box. So for example in the picture above, the Support Group Contact would only show users who are members of the Enterprise Architecture group.
Could someone please show or tell me how to accomplish this? Much appreciated in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 12:14 PM
You reference qualifier on Support Group Contact would look similar to this:
Where GetGroupUsers is the name of a Script Include, getUsers is the name of a function in that SI, and v_support_group is the name of your IT Application Support Group variable.
Your Client callable Script Include would look like this:
var GetGroupUsers = Class.create();
GetGroupUsers.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUsers: function(group) {
var members = [];
var groupMembers = new GlideRecord('sys_user_grmember');
groupMembers.addQuery('group', group);
groupMembers.query();
while (groupMembers.next()) {
members.push(groupMembers.user.toString());
}
return 'sys_idIN' + members.join(',');
},
type: 'GetGroupUsers'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 12:37 PM
To limit selection in a reference field, you need to use a reference qualifier.
If you only need active members of the group, you can reuse a method from the WorkflowApprovalUtils class in the code of your reference qualifier like this:
javascript: 'sys_idIN' + new global.WorkflowApprovalUtils().getMembersOfGroup(current.variables.name_of_your_first_field);
If you need all members, no matter if they are active or not, you could use the following reference qualifier:
javascript:'sys_idIN'+ getIDs(current.variables.name_of_your_first_field); function getIDs(grp) {var m = GlideUserGroup.getMembers(grp); var ids = []; while (m.next()) {if (!m.user.nil()) {ids.push(m.user.toString());}} return ids;}
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/