- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2020 01:19 AM
Hello All,
I have requirement to make fields visible based the selection in the list collector variable.
List collector variable is populating from the 'file system' table.
so if someone selects one option in the right bucket, then one field should be visible and if someone selects two options, two fields should be visible.
Please assist me that How can I read the selections so that fields can be visible.
I think catalog client script should be written to make the filed visible but have no proper solution.
Please provide some solution.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2020 03:45 AM
Hi,
don't use gel() it won't work in portal
In below script use your valid variable names and list collector variable name
1) Use onchange and onSubmit
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var value = g_form.getValue('user'); // your list collector variable name here
var len = value.toString().split(',').length;
if(len == 1){
// show your variable
g_form.setMandatory('second',false);
g_form.setMandatory('first',true);
g_form.setVisible('first',true);
g_form.setVisible('second',false);
}
else if(len == 2){
// show your variables
g_form.setMandatory('first',true);
g_form.setMandatory('second',true);
g_form.setVisible('first',true);
g_form.setVisible('second',true);
}
}
2) onSubmit
function onSubmit(){
var value = g_form.getValue('user'); // your list collector variable name here
var len = value.toString().split(',').length;
if(len > 2){
alert('You are only allowed to select 2 values');
return false;
}
//Type appropriate comment here, and begin script below
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2020 01:27 AM
Hi,
You need to write onchange on the list collector variable
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2020 01:42 AM
Hello Ankur,
I have the requirement of having different options to be selected.
So i need a script which can work on the selection not on the selected value.
Like if Selected= 1, then one field is visible and if selected=2 then two fields are visible.
and only 2 selection are allowed.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2020 02:47 AM
Hi,
if you write onchange client script then it would work when 1 value is selected it runs and shows 1 variable
Each time when value is selected you can get the list collector value using g_form.getValue() and you can count the values by splitting with comma
So when length of array is 2 then show 2 variables
Sample Script below:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var value = g_form.getValue('users'); // your list collector variable name here
var len = value.toString().split(',').length;
if(len == 1){
// show your variable
}
else if(len == 2){
// show your variables
}
}
For checking only 2 values given you need to use onSubmit client script
function onSubmit(){
var value = g_form.getValue('users'); // your list collector variable name here
var len = value.toString().split(',').length;
if(len > 2){
alert('You are only allowed to select 2 values');
return false;
}
//Type appropriate comment here, and begin script below
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2020 03:14 AM
Hello Ankur,
I tried onchange script using gel function but no luck and one thing more that how can I limit slection to two options only.
function onChange(control, oldValue, newValue, isLoading) {
var limit =2;
var user = 'storage';
var bln =g_form.getValue(user );
var leftB = gel(user);
var rightB = gel(user);
var selectedOptions = rightB.options;
var selectedOption = leftB.options;
alert(selectedOptions.length);
if(selectedOptions.length ==1){
g_form.setMandatory('second',false);
g_form.setMandatory('first',true);
g_form.setVisible('first',true);
g_form.setVisible('second',false);
}
else if(selectedOptions.length ==2 ) {
g_form.setMandatory('first',true);
g_form.setMandatory('second',true);
g_form.setVisible('first',true);
g_form.setVisible('second',true);
}
Thanks