Set Mandatory Conditions for a List Collector

angela_benway
Kilo Expert

Is there a way to set a list collector mandatory for different conditions? I am trying to make it mandatory for a list collector to have at least 2 values selected. I need to do this onChange with a Client Script. I am not familiar with how list collector "answers" or "values" (on the right hand side) are stored so I'm not sure where to start with my script. I know this should be an if statement for when a variable is changed, but I'm not sure what the body should be. Thank you in advance.

1 ACCEPTED SOLUTION

mduluk
Giga Expert

So here is a script that checks a field called u_analyst_name to see how many people were selected in the list collector.   This just alerts for the test scenarios but you should be able to change that.   You might want to change it to an on submit script that pops up an error message if there are less then 2 people and aborts the update.   I think the set mandatory property only cares if there is one or more values in the list.



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


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


View solution in original post

4 REPLIES 4

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Angela,



I hope this will article from SN guru will be useful


http://www.servicenowguru.com/scripting/client-scripts-scripting/move-list-collector-options/



Thanks,


Pradeep Sharma


Aaron40
Kilo Guru

The only data stored in the list collector variable is a comma separated list of the sys_ids of the chosen records. The options that weren't chosen aren't stored anywhere.



That said, client scripts can change the filter of the slush bucket so you only show certain options under specific conditions.


mduluk
Giga Expert

So here is a script that checks a field called u_analyst_name to see how many people were selected in the list collector.   This just alerts for the test scenarios but you should be able to change that.   You might want to change it to an on submit script that pops up an error message if there are less then 2 people and aborts the update.   I think the set mandatory property only cares if there is one or more values in the list.



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


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


Subhajit1
Giga Guru

I think this can be done through an Before Insert/Update BR.



Condition: previous.field_name.changes() && current.field_name == 'Your New Value'// sysid in case of Reference



Script:-


var list = current.watch_list.toString();


var array = list.split(",");


var ln = array.length;



if(ln<2)


{


gs.addErrorMessage('Your Message');


current.setAbortAction(true);


}



Thanks,


Subhajit