Automatically populate member_list with all users from selected group?

MBarrott
Mega Sage

Our users dislike the fact that once they choose a group, they have to select the applicable users one by one rather than a mass selection. 

 

Is there a way to automatically populate the member_list field with ALL users from the chosen group, and they can then filter out?

 

MBarrott_0-1724944662254.png

 

1 ACCEPTED SOLUTION

Hemanth M1
Giga Sage
Giga Sage

Hi @MBarrott ,

 

Here you go:

HemanthM1_0-1724947518410.png

 

HemanthM1_1-1724947538727.png

Output:😍

HemanthM1_2-1724947581872.png

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var member = new GlideAjax("getMembers");
    member.addParam("sysparm_name", "userList")
    member.addParam("groupid", newValue);
    member.getXMLAnswer(output)

    function output(result) {
        g_form.setValue("u_member_list", result);
    }


    //Type appropriate comment here, and begin script below

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

	userList: function(){
		var members="";
		var id = this.getParameter("groupid");
		var grpMembers = new GlideRecord("sys_user_grmember")
		grpMembers.addQuery("group", id);
		grpMembers.query();
		while(grpMembers.next()){
			members+=","+grpMembers.user.toString();
		}
		return members;
	},

    type: 'getMembers'
});

 

Hope this helps!!!

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

View solution in original post

5 REPLIES 5

Vrushali  Kolte
Mega Sage

Hello @MBarrott ,

One way to achieve this requirement is, you can create an on-change client script that triggers when the user selects a group. Within this script, you can use GlideAjax to call a function from a script include. Please refer to the following scripts:

 

//Client Script -
//please replace the variable names
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

  var ga = new GlideAjax('GetUsersByGroup');
        ga.addParam('sysparm_name', 'getUsers');
        ga.addParam('sysparm_groupName', newValue);
        ga.getXMLAnswer(function(response) {
            var users = response;
            g_form.setValue('user_list_field_name', users);
        });

   //Type appropriate comment here, and begin script below
   
}
//Script Include 

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

    getUsers: function(groupName) {
        var users = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('group.name', groupName);
        gr.query();
        while (gr.next()) {
            users.push(gr.user.sys_id.toString());
        }
        return users.join(',');
    },

    type: 'GetUsersByGroup'
};

 

If my answer solves your issue, please mark it as Accepted ‌✔️‌and Helpful‌👍‌ based on the impact.

Hemanth M1
Giga Sage
Giga Sage

Hi @MBarrott ,

 

Here you go:

HemanthM1_0-1724947518410.png

 

HemanthM1_1-1724947538727.png

Output:😍

HemanthM1_2-1724947581872.png

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var member = new GlideAjax("getMembers");
    member.addParam("sysparm_name", "userList")
    member.addParam("groupid", newValue);
    member.getXMLAnswer(output)

    function output(result) {
        g_form.setValue("u_member_list", result);
    }


    //Type appropriate comment here, and begin script below

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

	userList: function(){
		var members="";
		var id = this.getParameter("groupid");
		var grpMembers = new GlideRecord("sys_user_grmember")
		grpMembers.addQuery("group", id);
		grpMembers.query();
		while(grpMembers.next()){
			members+=","+grpMembers.user.toString();
		}
		return members;
	},

    type: 'getMembers'
});

 

Hope this helps!!!

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Hi @Hemanth M1

 

Sorry for being so delayed in responding. I modified your script and field values and it worked perfectly. Greatly appreciate the help. 

 

Accepted as solution. 

Bert_c1
Kilo Patron

@MBarrott,

 

I've tested a client script and script include like @Hemanth M1 's. and it works. Of course you need to update the table name in the client script to your table, and the field names for "Group" and "Member list" shown in his post. Seems the first response here was not tested.