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

Ajai


i tried this in both cases i.e onChange() and onSubmit() in both conditions am selecting all the options from slush bucket from left to right but the thing is while on onChange am getting the alert message form submitted more than 5 options in onSubmit condition am getting alert message if   more than 5 options but form not submitted until options are less than 5 options.In above script works perfectly on   onSubmit() type


But in my case , my requirement is an onchange script. Another interesting thing is that, if you try to add the same value twice then that will also be added.


Anyway thanks for the help.



Can anyone help me on this?


Hi Ajai


pls try this its works perfectly for me once i selected a group that is not present on the left bucket for one more selection




function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }


        var maxOptions = 5;


      var varName = 'v_select';


      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--');


              sortSelect(leftBucket);


              alert('You cannot select more than ' + maxOptions + ' options.');


      }    


}



Hi Venkateswarlu,



As I mentioned in the question itself, I have used the same script from Servicenow guru and it had some other issue. It will sort the right bucket at the time of checking and so it will add the value based on ascending order and remove the last option.



That is, if you try to add a value after 5 allowed options. It will compare it with the available values in the right bucket and if this value comes lower in the order then it will be added and last option will be moved back to left bucket.


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.');


      }    


}


}