How remove selected values from a list collector

Hari1
Mega Sage

Hi,

I have a requirement to remove list collector selected values if we selection is more than 3 values.

I should be able to enter the 4th value the list collector should not accept the 4th value when the user enters beyond 3 values. It should remove the additional selected value. I am try to achieve this using the onChange - Client Script. Can this be achieved?

 

1 ACCEPTED SOLUTION

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Hari1 ,

 

You Can use below onChange client script for that :-

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

    //Type appropriate comment here, and begin script below

    var values = g_form.getValue('work_notes_list').toString().split(',');
    //g_form.addInfoMessage('total value in users '  + values);

    if (values.length > 3) {

        alert('Please select only 3 choices');
        //g_form.removeOption('who_are_need_to_access','value');
        for (var i = 0; i < 3; i++) {
            g_form.setValue('work_notes_list', values[i]);  
        }
        return values;

    }

}

 Replace your field wherever 'work_notes_list' is written.


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

View solution in original post

6 REPLIES 6

Community Alums
Not applicable

You can try the below code in onChange Client Script;

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

//Type appropriate comment here, and begin script
var values = g_form.getValue('variable_name').toString().split(',');

if(values.length > 3){

alert('You can select only 3 choices');

g_form.clearValue('choose_your_favourite');

}



}

Regards,

Karthik R

Muhammad Khan
Mega Sage
Mega Sage

Add the below attribute to your list collector variable.

glide_list=true

Adding above attribute to make it look like a list field, this might help to avoid auto sorting of selected values to ensure that none from the first 3 selected values get removed.

 

After that use onchange catalog client script on your list collector variable.

 

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

    var appCount = g_form.getValue('applications_to_be_updates_is_more_than_5');

	var arr = newValue.split(',');
	
    if (appCount == 'Yes' && arr.length > 3) {
        alert("You cannot add more than 3 applications");
		g_form.setValue('select_the_application_name', arr.slice(0, -1));
    }

}