how to populate field of type List using onChange client script with multiple values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
how to populate field of type List using onChange client script with multiple values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Alon Grod ,
Yes, you can populate multiple values into a List type field from an onChange Client Script.
A List field stores multiple sys_ids from its referenced table.
If you already know the sys_ids, use an array:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue)
return;
var values = [
'SYS_ID_1',
'SYS_ID_2',
'SYS_ID_3'
];
g_form.setValue('u_list_field', values);
}
Replace:
u_list_field
with your List field name.
Important:
The values must be sys_ids from the table referenced by the List field.
For example, if u_users is a List field referencing sys_user:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;
if (!newValue) {
g_form.clearValue('u_users');
return;
}
var users = [
'6816f79cc0a8016401c5a33be04be441',
'62826bf03710200044e0bfc8bcbe5df1'
];
g_form.setValue('u_users', users);
}
If the values have to be retrieved dynamically based on the field changed, use GlideAjax.
Example:
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;
if (!newValue) {
g_form.clearValue('u_users');
return;
}
var ga = new GlideAjax('GetUsersForList');
ga.addParam(
'sysparm_name',
'getUsers'
);
ga.addParam(
'sysparm_value',
newValue
);
ga.getXMLAnswer(function(answer) {
if (!answer) {
g_form.clearValue('u_users');
return;
}
var users = JSON.parse(answer);
g_form.setValue(
'u_users',
users
);
});
}
Client-callable Script Include:
var GetUsersForList = Class.create();
GetUsersForList.prototype =
Object.extendsObject(AbstractAjaxProcessor, {
getUsers: function() {
var value =
this.getParameter('sysparm_value');
var users = [];
var gr =
new GlideRecord('sys_user');
gr.addActiveQuery();
// Add your actual condition here.
// Example:
// gr.addQuery('department', value);
gr.query();
while (gr.next()) {
users.push(
gr.getUniqueValue().toString()
);
}
return JSON.stringify(users);
},
type: 'GetUsersForList'
});
Recommended approach:
onChange field
-> GlideAjax
-> Query required records server-side
-> Return array of sys_ids
-> g_form.setValue()
-> List field populated with multiple records
If instead you want to ADD new values while preserving the values already present in the List field, use:
var existing =
g_form.getValue('u_users');
var values =
existing ? existing.split(',') : [];
values.push('NEW_SYS_ID');
g_form.setValue(
'u_users',
values
);
Before adding values, I would also remove duplicates:
values = Array.from(
new Set(values)
);
Important points:
- The target field must be Type = List.
- Its referenced table must match the sys_ids being supplied.
- Use sys_ids, not names/display values.
- Use GlideAjax when records need to be queried dynamically.
- Do not use client-side GlideRecord for this requirement.
- If you want to replace the existing values, call setValue() directly.
- If you want to preserve existing values, read them first and append the new sys_ids.
ServiceNow officially supports passing an array of sys_ids to g_form.setValue() when the target field is a Glide List.
Hope this helps!
If this response helped, please mark it as Helpful.
If it resolves your issue, please Accept it as Solution.
Kind Regards,
Abhishek Pal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
simply set string of comma separated sysIds using g_form.setValue()
what did you try and what didn't work?
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Ankur Bawiskar @Abhishek Pal @Risto D_od_o
This field of type List with predefined choices
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Alon Grod ,
Thanks for clarifying.
Since your field is Type = List with predefined choices and is NOT referencing another table, you do not need sys_ids or GlideAjax.
You should populate it using the internal VALUES of the predefined choices, not their display labels.
For example, suppose your List field is:
u_environment
and the predefined choices are:
Label Value
Production production
Development development
Test test
To populate multiple selections from an onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;
if (newValue == 'some_value') {
g_form.setValue(
'u_environment',
'production,development,test'
);
} else {
g_form.clearValue('u_environment');
}
}
The important part is:
production,development,test
These must be the choice VALUES, not:
Production, Development, Test
You can verify the values by:
Right-click the field
-> Configure Choices
or:
Show Choice List
and check the Value column.
If you already have some selected values and want to append additional choices instead of replacing them:
var existing = g_form.getValue('u_environment');
var values = existing
? existing.split(',')
: [];
var valuesToAdd = [
'production',
'test'
];
valuesToAdd.forEach(function(value) {
if (values.indexOf(value) == -1)
values.push(value);
});
g_form.setValue(
'u_environment',
values.join(',')
);
So for your case:
List with reference table
-> use referenced record sys_ids
List with predefined choices
-> use internal choice values
Since you confirmed yours is the second type, use the predefined choice values.
One design consideration:
ServiceNow's standard Choice field itself supports only one selected value. A Glide List can hold multiple values, but ServiceNow generally models Glide Lists as collections of references. If this field becomes important for integrations/reporting or needs to work consistently across different Workspace experiences, a small reference table containing those predefined options is the more robust data model.
For your current onChange requirement in the standard form, however, try the comma-separated internal choice values first.
Official ServiceNow references:
Choice Lists:
https://www.servicenow.com/docs/r/platform-administration/c_ChoiceLists.html
View Choice List Definitions:
https://www.servicenow.com/docs/r/platform-administration/t_ViewChoiceListDefinitions.html
Related ServiceNow Community discussion for Glide List fields with choices:
https://www.servicenow.com/community/developer-forum/how-to-manage-choices-in-glide-list-type-field/...
Hope this helps!
If this response helped, please mark it as Helpful.
If it resolves your issue, please Accept it as Solution.
Kind Regards,
Abhishek Pal