Set minimum value for list collector in a catalogue item variable

Cirrus
Kilo Sage

Evening,

I have a catalogue item list collector variable where the user must specify a minimum of 2 users. If not, I want an alert message to tell them when they submit the request. With ref to the following post

https://community.servicenow.com/community?id=community_question&sys_id=bf4c4f65db9cdbc01dcaf3231f961969

I have created an onSubmit catalogue client script as follows:

function onSubmit() {
//Type appropriate comment here, and begin script below
var string = g_form.getValue('sp_group_add_owners');
var array = string.split(",");
if(array.length>1){
alert("less then 2");
}
else{
alert("2 or more");
}
}

However, there are two issues.

The first is that if I only select one user it returns the "2 or more" alert but still creates the request. ideally I want the user to have to go back and select at least one more before being able to submit.

The second is that it doesnt work on the portal. The request gets created regardless.

 

Any suggestions please?

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Cirrus,

 

Replace the code with the below and also make sure client script UI type field is set to ALL. In addition, you can also create a OnChange client script and alert the user when the field is modified and the value is less than 2. Edit: There was a typo error. Updated the script.

function onSubmit() {
//Type appropriate comment here, and begin script below
var string = g_form.getValue('sp_group_add_owners');
var array = string.split(",");
if(array.length < 2){
alert("less then 2");
	return false;
}
}

View solution in original post

3 REPLIES 3

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Cirrus,

 

Replace the code with the below and also make sure client script UI type field is set to ALL. In addition, you can also create a OnChange client script and alert the user when the field is modified and the value is less than 2. Edit: There was a typo error. Updated the script.

function onSubmit() {
//Type appropriate comment here, and begin script below
var string = g_form.getValue('sp_group_add_owners');
var array = string.split(",");
if(array.length < 2){
alert("less then 2");
	return false;
}
}

Below script will stop the item from submission if list collector values is less than 2 

var string = g_form.getValue('sp_group_add_owners');
var array = string.split(",");
if(array.length < 2 ){
 return false
}

Cirrus
Kilo Sage

Thanks Pradeep. Now works perfectly in the portal I had the UI set to Desktop!