How to restrict number of records on Glide List(list collector) of service catalog.

Sharique Azim
Mega Sage

Hi All,

I am trying to restrict the number of records which can be selected in a glide list to be 5.

I can best figure out that   if i can operate something similar to an array concept i can restrict the   selection and alert.

My interest is particularly in glide list, as I dont want to show all the user's name at start.

Piece of my code is

var maxOptions = 5;

  var selectedIDs = [];

  var str= selectedIDs.push(g_form.getValue('users'));

  alert(str);

  alert(str.length); //i know it would store a single string with multiple commas using this code

              var index = 0;

              for(var i = maxOptions; i < str.length; i++){

                      selectedIDs[index] = i;

                      index++;

            }

Kindly advice me on my code or any better approach..

1 ACCEPTED SOLUTION

This may work better as an onSubmit rather than an onChange. That way users can remove those list choices they deem unnecessary rather than relying upon the script to remove all but the first 5 entries. I am using list_variable as the variable name in my example:



function onSubmit() {


      g_form.hideFieldMsg('list_variable', true);


      var chkString = g_form.getValue('list_variable');


      var chkArray = chkString.split(',');


      if (chkArray.length > 5) {


              var eMessage = 'You cannot select more than 5 entries at a time';


              g_form.showFieldMsg('list_variable', eMessage, 'error');


              return false;


      }


}



Let me know if this is more successful since I have not tested it.


View solution in original post

9 REPLIES 9

OK, reading again what you are trying to accomplish, let me clarify to make sure:



On a GlideList variable, you want to validate that the number of selections does not exceed a given amount.


Yes sir!


Since you indicated this will be an onChange Catalog Client script, we can leverage the value of newValue which may be more efficient. This way you can use the list field in your onChange script for validation. Feel free to adjust accordingly.


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


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


              return;


      }


      var chkArray = newValue.toString().split(',');


      if (chkArray.length > 5) {


              alert("You cannot select more than 5 entries at a time");


      }


}


Hi Christopher,



You/We are almost close.. But the issue is even users after 5 are being added.



I tried  



chkArray.pop();


and return false;



Either of them didnt work..


This may work better as an onSubmit rather than an onChange. That way users can remove those list choices they deem unnecessary rather than relying upon the script to remove all but the first 5 entries. I am using list_variable as the variable name in my example:



function onSubmit() {


      g_form.hideFieldMsg('list_variable', true);


      var chkString = g_form.getValue('list_variable');


      var chkArray = chkString.split(',');


      if (chkArray.length > 5) {


              var eMessage = 'You cannot select more than 5 entries at a time';


              g_form.showFieldMsg('list_variable', eMessage, 'error');


              return false;


      }


}



Let me know if this is more successful since I have not tested it.