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

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