Custom Reference field return group members from one group

anfield
Tera Guru

I have a custom reference field where i want to return all the group members from one group (to be able to select one). At the moment it points to sys_user_grmember and it just returns the group name multiple times. I'd rather not customize anything about sys_user_grmember as that is used in other modules. Found the encoded query below to add into the attributes of the reference field, but it doesnt seem to have an affect.

 

Also tried to modify the columns attribute using ref_ac_columns=user, but that do anything either.

 

Encoded query tried:

javascript&colon;var gr = new GlideRecord('sys_user_grmember');gr.addQuery('group',"<group sys id here>");gr.query();var users='';while(gr.next()){users+=gr.user.sys_id + ",";}"sys_idIN" + users;

 

 

1 ACCEPTED SOLUTION

I used a clean PDI to add a field called App Devs on the incident table to only return the users in the Application Development group.

Final outcome after clicking lookup:

drewmasonNR_0-1710990164161.png

Column entry:

drewmasonNR_1-1710990321021.png

Script Includes:

drewmasonNR_2-1710990374548.png

Group:

drewmasonNR_3-1710990490046.png

 

 

View solution in original post

12 REPLIES 12

drewmasonNR
Tera Expert

I would try the following from the Global scope and replace Tron with your assignment group:

 

1. Go to System Definition > Script Includes
2. Create New
3. Enter the following
  • Name: getTronGroup
  • Client Callable: True
  • Active: True
  • Accessible From: All application scopes
  • Pase Script below:

 

 

function getTronGroup() {
    var groupName = 'Tron';

    var userSysIds = [];

    var groupGR = new GlideRecord('sys_user_group');
    groupGR.addQuery('name', groupName);
    groupGR.query();
    if (groupGR.next()) {
        var groupId = groupGR.sys_id.toString();

        var memberGR = new GlideRecord('sys_user_grmember');
        memberGR.addQuery('group', groupId);
        memberGR.query();
        while (memberGR.next()) {
            userSysIds.push(memberGR.user.toString());
        }
    }
    
    return userSysIds;
}

 

 

To test, go to the table you would like to filter and use the condition below:

drewmasonNR_0-1710955643981.png

Be sure to update the User field with the field you need, and dot walk to the sys_id making sure the 'is one of' qualifier is selected.

 

Let me know if this helps or if I have misunderstood the ask!

 

Drew

Thanks for the response. When I put this in a fix script and run it, it does return the correct sysids of the users I am looking for. Not sure I follow your last line though about updating the user field etc..?

 

The script include though doesnt work when run from the list view on the user table or sys_user_grmember, and Im not seeing it work properly on the reference field itself when I add it.

 

It should be added to the field like this right? With semi colon at the end..javascript&colon;global.getTronGroup();

 

Still not quite sure if the reference field itself should be pointed to sys_user or sys_user_grmember. The users are just looking to be able to select the names (and I dont think they mind) if the sysid of the users themselves will be stored on the form when selected (as this is the case with many other fields on servicenow)

I see.. this was meant for using in reporting and would need to be reworked for fix scripts...

 

When you place this in a condition of a report or table view, you will use a colon[:], not a semicolon [;]. 

 

1. Dot walk to the sys_id field of the user reference field. User field>Sys ID

2. Use the 'is one of' operator.

3. enter the following in the value field as is: javascript&colon; gs.getTronGroup()

 

Remember, you should be replacing the word Tron with the group name you are wanting to get the members both in the name of the script and in the script itself. If still set to Tron this will not work.

Ok. Made sure the script include name, function name etc, and variable all look good> Still not working for the fields itself though, but i do see it produce a list of sysids on the user table in the list view as intended. (Still returning just the group name multiple times (to match the number of users returned)