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

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

Aditya02
Tera Guru

thats great its working! but there is problem. After selection of third option i am getting alert box and at the same time the 3rd option was also selecting so, i want to remove that selections after the alert.

use g_form.removeOption(<fieldName>, <choiceValue>); get the choice value from the new value variable OR

use the code in this answer https://www.servicenow.com/community/itsm-forum/remove-just-single-value-from-selected-values-in-a-l...

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

The values are Not Clearing @piyushsain.

I am getting that alert that's all.

Amit Verma
Kilo Patron
Kilo Patron

Hi @Aditya02 

 

Try below On-Change Client Script :

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var values = g_form.getValue('requested_for').toString().split(',');
    var listCollector = g_list.get('requested_for');
    var selectedOptions = g_form.getValue('requested_for').split(',');

    if (values.length > 2) {

        alert('Please select only 2 values');

        var index = 0;
        for (var i = 1, leng = values.length + 1; i < leng; i++) {
            listCollector.removeItem(selectedOptions[selectedOptions.length - i]);
            index++;
        }
    }

}

 

Thanks & Regards

Amit Verma


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