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

Hi Ajai



try this




function onSubmit() {


    //Type appropriate comment here, and begin script below


      var maxOptions = 5;


      var varName = 'v_select';


      var leftBucket = gel(varName + '_select_0');


      var rightBucket = gel(varName + '_select_1');


      var selectedOptions = rightBucket.options;


        //var e = document.getElementById("quantity");


  // e.value =selectedOptions.length;



    //alert(selectedOptions.length,e);


      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++;


                      //alert(selectedOptions);


                      g_form.showFieldMsg('v_select','you have selected more than 5 pls select only 5','error');


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


                        return false;  


              }


              //Move options and sort the left bucket


        //     moveSelectedOptions(selectedIDs, rightBucket, leftBucket, '--None--');


            // sortSelect(leftBucket);


                 


      }


}


Thanks Venkateswarlu Kuruva.


After small modifications it worked for me.



My requirement was to have maximum of 5 selections allowed and I slighly changed value of variable maxOptions to 4.


If 5 is given it is adding the sixth value after displaying the alert message.



When I tested further, using arrows I am still able to choose all groups from the list from left to right. Can you please help me to correct this?


HI Ajai


All means only 5 groups ?? or totally from the left bucket   to right bucket   can u pls provide some more information


Any value after the 5th selection is also added. If we select the whole options in the left then that will also be added to right.


In between it will pop up that alert that's it. we can click ok and move using arrow.