Need to populate groupmembers based on group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2024 02:45 AM
Hello,
We have 2 reference fields on form. 1 is referring to group table & other is group member table.
My requirement here is to Populate group members in 2nd reference field based on the group that we have selected in 1st reference field.
Can anyone help with this code to get users of selected groups & populate on 2nd refernce field.
Client script:
Script Include:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2024 03:01 AM
Hello,
To populate the group members in the second reference field based on the group selected in the first reference field, we can improve the client script and Script Include provided. There are a few key adjustments needed:
- Client Script: We need to parse the JSON response correctly and set the values properly in the second reference field.
- Script Include: Ensure the
GlideRecord
query and the return of JSON-encoded array of users are correctly implemented.
Here's the updated code:
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('c_populateGroupUsers');
ga.addParam('sysparm_name', 'getUsers');
ga.addParam('sysparm_user', newValue);
ga.getXMLAnswer(function(response) {
var answer = response;
var users = JSON.parse(answer);
var userList = users.join(',');
g_form.setValue('select_users', userList);
});
}
Script Include:
var c_populateGroupUsers = Class.create();
c_populateGroupUsers.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUsers: function() {
var group = this.getParameter('sysparm_user');
var users = [];
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', group);
gr.query();
while (gr.next()) {
users.push(gr.user.toString());
}
return JSON.stringify(users);
},
type: 'c_populateGroupUsers'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2024 03:19 AM
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Create GlideAjax object
var ga = new GlideAjax('c_populateGroupUsers');
ga.addParam('sysparm_name', 'getUsers');
ga.addParam('sysparm_group', newValue);
ga.getXMLAnswer(function(response) {
var answer = response;
if (answer) {
var userList = JSON.parse(answer);
g_form.clearOptions('select_users');
g_form.addOption('select_users', '', '-- None --');
userList.forEach(function(user) {
g_form.addOption('select_users', user.sys_id, user.name);
});
}
});
}
var c_populateGroupUsers = Class.create();
c_populateGroupUsers.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUsers: function() {
var groupSysId = this.getParameter('sysparm_group');
var users = [];
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', groupSysId);
gr.query();
while (gr.next()) {
var userObj = {
sys_id: gr.user.sys_id.toString(),
name: gr.user.getDisplayValue()
};
users.push(userObj);
}
return JSON.stringify(users);
},
type: 'c_populateGroupUsers'
});
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2024 03:43 AM
Hi @Vinay49 ,
Why don't you user advance reference qualifier. for the 2nd reference field.
Regards,
Pratap Singh Sisodia
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2024 03:49 AM
This is the Best Practice and you can find similar configuration on the Incident form as well in case of Assignment group and Assigned to.