Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to restrict list to add only limited number of users

Amol Pawar
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 @Amol Pawar ,
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

Sagar Pagar
Tera Patron

Hi @Amol Pawar,

 

Take a look at this solution: Limit values that can be entered on list collector variable 

Also, go through this article: Limiting Number of Selections in a List Collector 

 

It will help you to limit user selection in list collector field.


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

Rahul Talreja
Mega Sage

Hi @Amol Pawar ,
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

Amol Pawar
Tera Guru

Thank you @Rahul Talreja and @Sagar Pagar for your reply. With some minor changes as per requirement, it worked.

 

Thanks and regards,

Amol

Hi @Rahul Talreja and @Sagar Pagar ,

I've used the below code and it's working now but if I add 3rd user, it's removing the first two users and keeping the 3rd one. How can we achieve that it should keep first 2 users and remove the third one after adding?

 

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).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[i]);
g_form.addErrorMessage('You can add only two secondary SPOCs');
}
}

 

Thanks in advance,

Amol