
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2017 02:10 AM
Hi All,
I have a requirement to Limiting Number of Selections in a List Collector to 5.
I have used the script from SNGuru https://www.servicenowguru.com/scripting/client-scripts-scripting/limiting-selections-list-collector...
and it is restricting correctly. But I found another issue with that there. When you select more than 5 options, it is throwing the error correctly but removing the last option in the right bucket and after sorting. Actually It should be removing the last entered value from the right bucket. Can anyone help with a script for that ?
Any help is appreciated.
Regards,
Ajai
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2017 07:06 AM
Finally I found another function from a SNGuru link https://www.servicenowguru.com/scripting/client-scripts-scripting/move-list-collector-options/ which solved my issue.
I used moveOptionAndSort(rightBucket, leftBucket, '-—None—-', selectedIDs, '-—None—-'); instead of moveSelectedOptions(selectedIDs, rightBucket, leftBucket, '--None--'); from the script in SNGuru link for
Limiting Number of Selections in a List Collector
So the script that is working now is ,
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(g_form.getValue('action') == 'add')
{
var maxOptions = 5;
var varName = 'groups';
var leftBucket = gel(varName + '_select_0');
var rightBucket = gel(varName + '_select_1');
var selectedOptions = rightBucket.options;
if(selectedOptions.length > maxOptions){
//Move any options with IDs greater than maxOptions back to left bucket
var selectedIDs = [];
var index = 0;
for(var i = maxOptions; i < selectedOptions.length; i++){
selectedIDs[index] = i;
index++;
}
//Move options and sort the left bucket
// moveSelectedOptions(selectedIDs, rightBucket, leftBucket, '--None--');
moveOptionAndSort(rightBucket, leftBucket, '-—None—-', selectedIDs, '-—None—-');
// sortSelect(leftBucket);
alert('You cannot select more than ' + maxOptions + ' options.');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 02:14 AM
I am got error in script like
onChange script error: TypeError: gel is not a function function () { [native code] }
i think its not allowing gel function

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2021 10:47 AM
For those looking for an answer for Service Portal list collector and find this question, a solution similar to what is shown by others in this old thread seems to work well.
Catalog Client Script
Type: onChange
Variable name: Select your list collector variable
function onChange(control, oldValue, newValue, isLoading) {
//Limit the number of selected options in a list collector
//Specify the max options and variable name below
var maxOptions = 5;
var variableName = 'my_variable_name'; //change this to your variable name
var tempArray = newValue.split(',');
var length = tempArray.length;
if (length > maxOptions) {
alert('Please limit your selections to ' + maxOptions);
tempArray.length = maxOptions;
g_form.setValue(variableName,tempArray.toString());
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2021 07:12 AM
The error message will not go away after clicking ok.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2021 07:40 AM
Hi,
Above script is for Self Service Portal only, that why you getting error message