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

Hi @Hari1 ,

write onSubmit catalog client script

Applies to Catalog Item

Applies on Catalog Item view -  True

UI Type = ALL

Note: give your list collector variable name below

function onSubmit()

var values = g_form.getValue('variableName').toString().split(',');

if(values.length > 3){

alert('Please select only 3 choices');

return false;

}

}

I would something on the OnChange Client Script.

Below is the code that i am currently using.

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

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

    var appList = g_form.getValue('select_the_application_name');

    if(appCount == 'Yes' && appList.split(",").length > 3){
		alert("You cannot add more than 3 applications");
		appList.split(",").splice(appList.split(",").indexOf(appList.split(",")[3]), 1);
		//appList.removeItem(appList.split(",")[3]);
    }

}

Hi @Hari1 ,

 

Try below script:

 

 

 

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

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

    var appList = g_form.getValue('select_the_application_name');
    var app = appList.split(",");
    if(appCount == 'Yes' && app.length > 3){ //If you want this to depend on appCount then fine else revie appCount check
		 alert("You cannot add more than 3 applications");
                 app.pop() // THis will removet the recent selection
                 g_form.clearValue("select_the_application_name");
                 g_form.setValue("select_the_application_name",app); // repopulate with 3 values
    }

}

 

 

In case if you want this to be dependent on count of app only then use below code: 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
   var appList = g_form.getValue('select_the_application_name');
    var app = appList.split(",");
    if(app.length > 3){ 
		 alert("You cannot add more than 3 applications");
                 app.pop() // This will removet the recent selection
                 g_form.clearValue("select_the_application_name");
                 g_form.setValue("select_the_application_name",app); // repopulate with 3 values
    }

}

 

I hope this help.

 

Regards,

Kamlesh

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