Limiting Number of Selections in a List Collector

Ajai S Nair
Giga Guru

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

1 ACCEPTED SOLUTION

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

https://www.servicenowguru.com/scripting/client-scripts-scripting/limiting-selections-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.');


      }    


}


}


View solution in original post

18 REPLIES 18

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

kchorny
Tera Guru

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());
}
}

The error message will not go away after clicking ok. 

Hi,

Above script is for Self Service Portal only, that why you getting error message