Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to restrict the selection of number of choices for a List Collector Variable

Aditya02
Tera Guru

I have a List Collector Variable, In this I want to restrict the selection of number of choices to two only. So, that user can able to select two options only. How to Perform this.

1 ACCEPTED SOLUTION

I can see OnChange

piyushsain_0-1706520215427.png

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain

View solution in original post

13 REPLIES 13

It was Not working Amit.

Hi @Aditya02 

 

I have tested it on my PDI and it is working as expected. Can you please share your script and the name of the list collector variable to debug it further ?

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var values = g_form.getValue('multiple_choice').toString().split(',');
    var listCollector = g_list.get('multiple_choice');
    var selectedOptions = g_form.getValue('multiple_choice').split(',');

    if (values.length > 2) {

        alert('Please select only 2 values '+selectedOptions);

        var index = 0;
        for (var i = 1, leng = values.length + 1; i < leng; i++) {
            listCollector.removeItem(selectedOptions[selectedOptions.length - i]);
            index++;
        }
    }
    //Type appropriate comment here, and begin script below
}
 
name of list collector variable is: multiple_choice
 
 
Amit Here i also want to remove some choices based on selection of another variable.
i have state variable. if state is new, i want to remove all the options from the list collector except two options, else do nothing.

Hi @Aditya02 

 

On which variable you have kept the On-Change catalog client script ? I just copied your script and it is working as expected on my end. It should be On-Change of list collector variable.

 

Also, if the state changes to new, which two options to you want to remove and from where in the list collector ( Available Values or Selected Values). As we are keeping restriction on the selected values in the list collector to 2, I assume that it will be on the selected values only. If yes, then we need to keep an On-Change Catalog Client Script on the State field so that it can control the list collector field.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.