How to filter groups to which the user is assigned to.

Pablo H
Tera Contributor

Hello all,

I have created a new reference field on the sys_user table and I want to create a reference qualifier that would only show active groups to which the given user is assigned to. Any idea how to achieve this? Thank you

 

For example, as a system admin I want to assign a primary group to Abel Tuter, but it needs to be active group to which he is assigned to.

1 ACCEPTED SOLUTION

Actually, if you're looking for a reference to the Group, which I assume you are, you may want to created a Script Include that builds a query string for you of all the sys_ids of the group the user is a member of.

var UserRefQual = Class.create();
UserRefQual.prototype = {
    initialize: function() {},
    getGroups: function(user) {
        var groups = [];

        var grUserGrp = new GlideRecord('sys_user_grmember');
        grUserGrp.addQuery('user', user.sys_id);
        grUserGrp.query();

        while (grUserGrp.next())
        {
            groups.push(grUserGrp.group);
        }
		gs.info("sys_idIN" + groups);
        return "sys_idIN" + groups;
    },
    type: 'UserRefQual'
};

And in your reference qualifier, you can use this:

javascript: new global.UserRefQual().getGroups(current);

Make sure you change your reference field to Group. You won't have to worry about display values that way.

View solution in original post

11 REPLIES 11

Change 

return "sys_idIN" + groups;

to 

return "active=true^sys_idIN" + groups;

I'm afraid that the script is not working, its not filtering anything. Logs show me an error. 

com.glide.script.RhinoEcmaError: undefined is not a function.
<refname> : Line(1) column(0)
==> 1: new global.UserRefQual().getGroups(current);

My bad, its working but filtering the groups wrong.

 

On the script include replace 

groups.push(grUserGrp.group);

 

with

groups.push(grUserGrp.group.toString());




Please mark Kristy's answer correct/helpful if this works

Hello Akif, this did the trick. Thanks.