Need to select only 10 options max in list collector

suuriya
Tera Contributor

HI Community,

 

In a catalog item there is a List collector variable i need to select only 10 options max....currently i can select multiple options but i need to select only 10.

How can we achieve this.

 

Thanks & Regards,

Suuriya Sridharan

1 ACCEPTED SOLUTION

Amitoj Wadhera
Kilo Sage

Hi @suuriya ,

 

Please use this onChange client script, on change field would be the list collector field itself.

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

    var selectedValues = g_form.getValue('<list_collector_name>').toString().split(',');

    if (selectedValues.length > 10) {
        alert('You can select a maximum of 10 values.');
        g_form.clearValue('<list_collector_name>');

        for (var i = 0; i < 10; i++) {
            g_form.setValue('<list_collector_name>', selectedValues[i]);
        }
        return false;
    }
}

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera

View solution in original post

2 REPLIES 2

AJ-TechTrek
Giga Sage
Giga Sage

Hi @suuriya ,

 

Refer the below solved articles which may help you.

 

https://www.servicenow.com/community/developer-forum/limiting-number-of-selections-in-a-list-collect...

 

https://www.servicenow.com/community/csm-forum/how-to-limit-the-list-collector-choices-selection/m-p...

 

Please appreciate the efforts of community contributors by marking appropriate response as Mark my Answer Helpful or Accept Solution this may help other community users to follow correct solution in future.

 

Thanks

Ajay Kumar

Linkedin Profile:- https://www.linkedin.com/in/ajay-kumar-66a91385/

ServiceNow Community Rising Star 2024

Amitoj Wadhera
Kilo Sage

Hi @suuriya ,

 

Please use this onChange client script, on change field would be the list collector field itself.

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

    var selectedValues = g_form.getValue('<list_collector_name>').toString().split(',');

    if (selectedValues.length > 10) {
        alert('You can select a maximum of 10 values.');
        g_form.clearValue('<list_collector_name>');

        for (var i = 0; i < 10; i++) {
            g_form.setValue('<list_collector_name>', selectedValues[i]);
        }
        return false;
    }
}

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera