We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to restrict list to add only limited number of users

amolpawar
Tera Guru

Hi Experts,

 

I have a requirement where I need to restrict the users by adding multiple users using the list.

AmolPawar_0-1690180779366.png

As you can see in the screenshot, here I can add n numbers of users. Can we restrict this to add only 2 users?

If yes, then please reply with a solution.

 

Thanks in advance,

Regards,

Amol

 

1 ACCEPTED SOLUTION

Rahul Talreja
Mega Sage

Hi @amolpawar ,
You can use script below in onchange client script:

var variablename = 'variable name here';


var mylist = g_form.getValue(variablename).split(',');


var maxitem = 2;


       


       if (mylist.length>maxitem){


               var mynewlist = [];


               for (var i = 0;i<maxitem;i++)


               {


                       mynewlist.push(mylist[i]);


               }


               g_form.setValue(variablename,mylist[i]);


               g_form.showFieldMsg(variablename,'Only 2 items can be entered','error');


       }


}
Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

View solution in original post

7 REPLIES 7

Hi @amolpawar,

 

Updated scripts:

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

	//Type appropriate comment here, and begin script below
	var add_secondary_spocc = 'add_secondary_spocc';
	var mylist = g_form.getValue(add_secondary_spocc).toString().split(',');
	var maxitem = 2;

	if (mylist.length > maxitem) {
		var mynewlist = [];
		for (var i = 0; i < maxitem; i++) {
			mynewlist.push(mylist[i]);
		}
	}

	g_form.setValue(add_secondary_spocc, mylist);
	g_form.addErrorMessage('You can add only two secondary SPOCs');

}

 

If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar

The world works with ServiceNow

Hi @Sagar Pagar ,

A small change in last few lines and it worked 😊

 

}

g_form.setValue(add_secondary_spocc, mynewlist);
g_form.addErrorMessage('You can add only two secondary SPOCs');
}
}

 

Thank you so much!

Amol.

Hi @amolpawar,

 

Yes. it should be your mynewlist array. as it contains only 1st two selected users.

 

If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar

The world works with ServiceNow