How to find out if a user is a member of a group or not in a scoped application

Community Alums
Not applicable

Hi all,
I have two fields in the form, Iniator group(Reference to sys_user_group) and Assigned to(reference to sys_user)Field in a Scoped Application. Iniator group is the group that Requestor is a member of. The Requirement is that all the group members of the Iniator Group should not be displayed when search icon is clicked on Assigned to field. 
I have tried writing this script, but its not working. Maybe because getUserById() does not work in scoped Application.PFB script

 

var user = new GlideRecord('sys_user');

user.addQuery('active', true);

user.query();

var arr = [];

while (user.next()) {

    if (!gs.getUser().getUserById(user.sys_id).isMemberOf('3bd91ac21b014654bd2d97522a4bcbd3')) {

        arr.push(user.sys_id);

    }

}

gs.print( arr);

How to achieve this? Please give your Insights.
Thanks in advance!

 

 

 

1 ACCEPTED SOLUTION

So RLQUERYs join to the current table being queried, and allows you to filter the results from the database. It's not a true "join" as the joined table fields aren't available

 

RLQUERYsys_user_grmember.user,=00,m2m^group=" + current.iniator_group +"ENDRLQUERY

 

sys_user_grmember.user <- This is the table, and field we want to join on. so we're joining sys_user to this field as the value is a unique sysid

=00 <- This means we only want to return records where no related record is found. We're basically saying "I want all users, where they have no sys_user_grmember record"

m2m <- This is an additional syntax only used when the resulting table is a m2m table

^group=" + current.iniator_group +" <- This is the query. So based on the =00 we're saying "I want all users, where there is no sys_user_grmember record with a group of XYZ". 

 

By inverting your requirement, the logic is passed to the database rather than application layer, improving performance

View solution in original post

8 REPLIES 8

Kieran Anson
Kilo Patron

Is this function you're building to be used as a reference qualifier? If so, you can avoid the need of returning a array of sys_ids (which may be truncated on large instances) and instead use a related list query.

Encoded query strings (servicenow.com)

 

Requirement: The 'assigned_to' reference field should be filtered to not show any sys_users who are a member of the group selected in the 'iniator_group' reference field.

 

Reference Qualifer: "RLQUERYsys_user_grmember.user,=00,m2m^group=" + current.iniator_group +"ENDRLQUERY"

Community Alums
Not applicable

Hi @Kieran Anson , it is not working

Is your form a standard table form, or a catalogue item?

Community Alums
Not applicable

Hi @Kieran Anson ,why user is given as 00 ? and how to give not (!) condition to the query