Filter a reference field based on another reference field

Markell
Tera Guru

Hi Guys.

What I am doiung is I have ceated an Assigned to (sys_user) field and a Assigned Group (sys_user_group) field on a the sc_cat_item form & what I am trying to do is flter the Groups field based on the assigned to (user) selected. So only show groups that the user selected is in.

 

My initial idea was to try this via a reference qualifier and a script include:

So I tried:
javascript: specificGroupUsefulUtils().getGroups();

 

with the following script include: specificGroupUsefulUtils
and script (I have removed specific field names):

Markell_0-1738922244735.png

 

 

getGroups: function() {
       var groups = [];
        var user = current.variables.<<assigned to value here>>
        var grGroups = new GlideRecord('sys_user_grmember');
        grGroups.addQuery('user', user);
        grGroups.query();
        while (grGroups.next()) {
            //gs.log(grGroups.user);
           groups.push(grGroups.group.sys_id);
        //gs.log("groups Name:" + grGroups.getDisplayValue('group') + "Group Sys Id: " + grGroups.group);
        }
        return groups.toString();
 
 
I guess my problem here is how to pass the assigned to field value or sys id to the script include in order to filter the returning list.
 
If this is even the way to do it because I'm reading more on this and see this article:
How to select only users of a specific group into a reference field - Support and Troubleshooting

I tried the code in the field but the part that confuses me there is I cant see where 'grp' is set as a value so how this would work:
Markell_0-1738921992650.png

 

 

Thanks in advanced for any help



 

1 ACCEPTED SOLUTION

@Markell 

it's a field and not variable, so this will work

current.u_assigned_to

javascript&colon; 'sys_idIN' + new global.ArrayUtil().convertArray(gs.getUser().getUserByID(current.u_assigned_to).getMyGroups()); 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Hi Ankur

Both variables are reference fields

The user I am selecting to test this is a member of 4 groups

Markell_1-1738925629781.png

 

 

@Markell 

it's a field and not variable, so this will work

current.u_assigned_to

javascript&colon; 'sys_idIN' + new global.ArrayUtil().convertArray(gs.getUser().getUserByID(current.u_assigned_to).getMyGroups()); 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Not really sure what you changed there but it worked that time lol.

 

Thanks for your help

Ahhh it was current.<<field>> not current.variable.<<field>>

 

Thanks