Show field based on the selection in a List collector variable.

Aps3
Kilo Contributor

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

1 ACCEPTED SOLUTION

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

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

You need to write onchange on the list collector variable

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

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

Regards
Ankur

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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