Catalog Item Variable to Dynamically List Members of a Group

fcaruso123
Tera Expert

We have a group defined that contains users who can approve the purchase of certain devices. The catalog item uses the requesting users organization value (from user record) to further filter the group members.

 

The Group Members variable is a reference type defined to pull from sys_user_grmember:

fcaruso123_1-1727367390388.png

I see the following after selecting an Organization:

fcaruso123_0-1727367308145.png

 

The number of records getting listed in Group Members is correct but I cannot figure out how to get the user name to display.

 

Thank you




1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

A reference variable on the sys_user_grmember table is going to select a record on this table - which is just the user and group relationship.  You can change the Display column, but that will affect every appearance of the group member table system-wide, so you really don't want to do this as showing the value of the user column will not make sense outside of this reference variable.  What you really want to do is make this a reference to the sys_user table, then you will be selecting an actual user record which will display correctly, and you will be able to use the value in an approval, etc. - something you will not be able to do with a group member table record.

 

To filter the records to only show users who are members of a certain group, and your organization variable you will need a reference qualifier that looks more like this:

BradBowman_0-1727373652588.png

Where groupUtils (or whatever you want to name it) is the name of a Script Include, and getGrpMbr a function, passing in the name of a group.  If your organization variable is a reference, and the u_organization field on your user table is a reference, there's no need to dot-walk to name on each in the qualifier.  The Script Include will look like this:

 

var groupUtils = Class.create();
groupUtils.prototype = {
	initialize: function() {
	},
	
	getGrpMbr : function(grpname){
		var usrArr = [];
		var grp = new GlideRecord('sys_user_grmember');
		grp.addQuery('group.name', grpname)
		grp.query();
		while (grp.next()) {
			usrArr.push(grp.user.toString());
		}
				
		return 'sys_idIN' + usrArr.join(',');
	},

    type: 'groupUtils'
};

 

 

 

 

View solution in original post

21 REPLIES 21

Community Alums
Not applicable

Good Morning,

 

Following up. 

Can you elaborate on the above statement. 

You showed the behavior in portal, but I'm not sure what you were showing, or what isn't working in portal.

Community Alums
Not applicable

got it.

okay,

 

the members are not showing in the field.  I am assuming because member values are not set. the field is read only and does not display members on the client-side of the form. 

 

Saboogs3_0-1729862350293.png

 

I believe because the names are only in the available and not selected portion, it is not showing. 

 

when it's set to read only - it shows selected members:

Saboogs3_1-1729862761097.png

 

when it is not read only, it shows both:

 

 

Saboogs3_3-1729862969380.png

 

If I can get the names into the selected section then the read only field will show all members via the portal (client-side)

 

 

 

 

 

Right, the reference qualifier just sets the filter so the correct records can be selected.  If you want to actually set the value of a variable to a list of sys_ids, you can do that with an onChange Catalog Client Script when the existing_assignment_group_name variable changes.  This script will use GlideAjax to call the same Script Include.  For this to work for both (in case the variable is ever not read-only and someone will be manually selecting/editing) you need to change the reference qualifier to:

javascript: 'sys_idIN' + new groupUtils().getGrpMbr(current.variables.existing_assignment_group_name);

Then check the Client callable box on the Script Include.  That may not alter the existing script, so this is what it should look like - also with the new value from the client script/reference qualifier, and a return that works with both: 

var groupUtils = Class.create();
groupUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getGrpMbr: function(group) {
        var grpsysid = this.getParameter('sysparm_group') || group; //same sysparm_xxxx used in client script
        var usrArr = [];
        var grp = new GlideRecord('sys_user_grmember');
        grp.addQuery('group', grpsysid)
        grp.query();
        while (grp.next()) {
            usrArr.push(grp.user.toString());
        }

        return usrArr.join(',');
    },

    type: 'groupUtils'
});

Back in the client script in the GlideAjax callback then you would just setValue of the list variable to the response.  This should work both in the native UI where there is an Available and Selected boxes, and Service Portal that looks more like List fields.

 

Community Alums
Not applicable

I also changed this to grp.addQuery('group', group);

 This is for anyone else trying to do what I just did.

 

Final Edit that worked:

 

Variable:

 

Saboogs3_0-1729689301472.png

 

Script Include:

 

var groupUtils = Class.create();
groupUtils.prototype = {
    initialize: function() {},

    getGrpMbr: function(group) {
        var usrArr = [];
        var grp = new GlideRecord('sys_user_grmember');
        grp.addQuery('group', group);
        grp.query();
        while (grp.next()) {
            usrArr.push(grp.user.toString());
        }

        return 'sys_idIN' + usrArr.join(',');
    },

    type: 'groupUtils'
};