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

Ankur Bawiskar
Tera Patron
Tera Patron

@Markell 

no scripting required

in the 2nd variable use this advanced ref qualifier

javascript&colon; 'sys_idIN' + new global.ArrayUtil().convertArray(gs.getUser().getUserByID(current.variables.assignedToVariable).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

Hi, Thanks for your response but this did not work for me. 
I put the following in the reference qual field and it didnt work:
javascript&colon; 'sys_idIN' + new global.ArrayUtil().convertArray(gs.getUser().getUserByID(current.variables.u_assigned_to).getMyGroups());

 

It returned a blank list of fields from the reference field

 

@Markell 

this is for catalog or normal form?

Does that assigned to user has groups associated?

Is that group variable a List collector or Reference? share variable configuration screenshot

The solution I shared above has worked recently where community member did confirm the same

How can i dynamically populate a List Collector on Service Portal? 

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

@Markell 

did you test with admin?

If the group variable is list collector then ensure you add variable attributes

ref_qual_elements=u_assigned_to

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