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

BALAJI40
Mega Sage

Please show the screen shot what you are telling exactly?



Instead of that, you can validate with length command through onSubmit, it did not create any issues.



ex: var gr = g_form.getValue('watch_list_var_name').split(',');


if(gr.length == 5){


alert('exceeded the length');


return false;


}


Firstly I require this for a list collector field.


it should not allow more than 5 options to be selected and so it should be executed on an onchange script not onsubmit since I need to through an error message when user selects more.


If you open the link that I have mentioned in the question it is showing the requirement with a solution already. Only change that i require from that is, the option that is selected after maximum allowed option should be removed from the right bucket back to left bucket. Currently the last option in right bucket is being removed. Hope this helps you to understand my question clearly.


Yes Ajai, I understood your requirement.


You can write onChange script on Listcollector variable.


find_real_file.png


try this script:


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


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


          return;


    }


var gr = newValue.split(',');


  if(gr.length>2){


  g_form.showFieldMsg('avl_grp','you have selected more than 2','error');


  }


    //Type appropriate comment here, and begin script below    


}


The link you provided, i am aware of it.


I tried with the code you mentioned and its not working properly it seems.


I need to display an alert message instead of the field message here.


If the allowed length is 5 then it will through the alert and accept the sixth value also. So I tried by giving the length as 4 and so it only allowed to select 5 options now.


But when you try selecting the same option 2 times then its added to the right bucket. That is when you select the fist time it gives the error and the second time it gives error and then add the value to right bucket.


find_real_file.png



current script:



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


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


          return;


    }


      //Limit the number of selected options in a list collector




  var gr = newValue.split(',');


  if(gr.length> 4){


  //g_form.showFieldMsg('groups','You cannot select more than 5 options.','error');


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


  }


     


}