How to filter reference field based on a group that's selected in a choice field?

iDevilsdocry
Mega Contributor

Hello,

I have a catalog item with a choice field that loads a list of group/s based on logged in user group presence. (This isn't a reference field referencing groups table)

I have another item that IS a reference field that needs to point to the group that's selected in the first choice field and show all group members for that group. Each groups from the first field has an approval group and I'm referencing group members of that group.

I have looked at the advanced reference qualifier options and examples in community but I don't see how it'll fit my example because I need to be able to reference all groups (28 to be exact) just from that one field.

I know that changing the first field type to reference field make this a lot simpler but I cannot change anything about that.

 

Thanks for any info.

 

EDIT:

So I have found an alternative for this. Here is how it works but has a small problem....

 

So instead of having second field as a reference field, I made another field of same type as the first 'Select Box' with choices that auto populate with an OnChange client script I wrote below...

 

function onChange(control, oldValue, newValue, isLoading){
     if (isLoading || newValue == ''){
          return;
     }

var groupapproval = g_form.getValue('variables.approval_route');
//here approval_route is the name of the first select box.
//choice selected here isn't an actual group. So its just a randomly named choice.

if(groupapproval == 'approval_route choice')
{
     var users = new GlideRecord('sys_user_grmember');
     users.addQuery('group', 'sys_id_for_corresponding_group'); //sys_id_for_corresponding_group is the group that should be queried based on choice in first field
     users.query();

     while (users.next()){
//below 'approver' is a new field that I mentioned that I created as alternative to reference field
          g_form.addOption('variables.approver', users.user, users.user);
     }
}
}

So the problem with this is that it is grabbing sys_id from users table for each member in the group. I need their names. I'm pretty sure it has something to do inside what I have in the while loop but not sure what to change and what to change it to.

9 REPLIES 9

Tim Provin
Mega Guru

Your reference qualifier will need to query the sys_user_grmember table matching on the group field, and return all of the user sys_id(s) from that query.  I could help you write out the query, but would need to know what you are storing at the "value" for that choice list.  Is it the group name, sys_id, or some other random value?

But the problem with this is I'm not sure if it will be able to reference multiple groups in-case the choice option is changed.

The value passed inside the first choice field is random value named about that group (group with that name doesn't exist). 

For example,

if one of the choice was 'Fruit'

then the approval group (actual group) for that choice would be 'Fruit - Approval'

Just read through your update on the original post.  What you are doing there is the same basic idea as what you would need for the reference qualifier.  If you want to keep it as a choice list, then you should be able to just change your addOption line to the following.

g_form.addOption('variables.approver', users.user.name, users.user);

This will set the label to the User's name and the value to their sys_id.

That should be: g_form.addOption('variables.approver', users.user, users.user.name). changing to that returns empty blank space instead of the name. 

I would love to see how reference qualifier would look/work. I have never worked with those before (new to servicenow). Is it better to have dynamic reference qualifier considering that I need to change what group it refers to based on what's selected in the first choice. or should advanced reference qualifier work?

I suppose it'll involve writing a similar script in a script include and referencing that inside the qualifier.