Help with Limiting Number of Selections in a List field on sctask form

Sam198
Mega Guru

Hi,

I am trying to limit the selection by user on a list type custom field on sctask form with below Client script, but it removes all the options > it should only allow three selections and as soon as fourth is selected it would give the message and remove the fourth one:

 

function onChange(control, oldValue, newValue, isLoading) {

    var check = g_form.getValue('u_dropdown').split(',');
    
    if (check.length > 3) {
      
        g_form.showFieldMsg('u_dropdown', 'Only three selection is permitted for this field');
       g_form.setValue('u_dropdown', check[check.length-1]);
    
    }
}

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

don't remove any value as you will have to define logic to know which was the 4th one by comparing old and new value

just show the message near field

function onChange(control, oldValue, newValue, isLoading) {

	var check = g_form.getValue('u_dropdown').split(',');

	g_form.hideFieldMsg('u_dropdown');

	if (check.length > 3) {
		g_form.showFieldMsg('u_dropdown', 'Only three selection is permitted for this field','error');
	}
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

Thanks for the suggestion, however, just one query, this will still allow user to save the form with more than three selection (ignoring the message) - Thats why i was needing to remove the forth selection.

Hi Sam,

That should still be fine.

You can inform the user and let user decide which value to be removed so that only 3 remains.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Sam

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader