How to Hide/Show choice in Multiple choice variable based on other variable.

vinod9
Tera Expert

Hello Team,

I have requirement to hide/show the one choice in Multiple choice variable in catalog item.

Two variables we have,
1. checklist (name : Virtual meeting).
2. Multiple choice : It has below three choices
                                   1.AB
                                   2.BA
                                   3.CD

when I select the checklist (Virtual meeting) "BA" has to hide.
when I unselect the checklist (Virtual meeting) "BA" has to show.

Please help me on this requirement.
@Mohith Devatte 

@AnubhavRitolia 

@Gunjan Kiratkar 

@

 

 

5 REPLIES 5

Mohith Devatte
Tera Sage
Tera Sage

Hello @vinod9 ,

you can write an on change client script on your check box variable and your script can be like this 

 

In clear options field name put the select box field backend name 

In field name of addOption method put the field name of the select box

In the choice value put the choice value of your options your want to add when virtual meeting is true(under if loop) , In choice label put the choice labels that you want to add and target index is nothing but the position of the choice 

 

And in else loop do the exact opposite way by clearing the options again and adding all three options if its false 

 

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

if(newValue==true || newValue=='true')
	{
		g_form.clearOptions('<FIELD_NAME>');
		g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>, <targetIndex>);
		g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>, <targetIndex>);
	}
	else {
		g_form.clearOptions('<FIELD_NAME>');
		g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>, <targetIndex>);
		g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>, <targetIndex>);
		g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>, <targetIndex>);
		
	
	
	}
		
	
   
}

Hope this helps 

Mark my answer correct if this helps you 

Thanks

 

@Mohith Devatte @AnubhavRitolia 

I'm using this code, it's working fine, when I select the variable another field is hiding but when I unselect it, I'm not getting back hiden field.

If part is working but else part not working. Please help

 

IMG20221202144240.jpg

Hello @vinod9 ,

if you are trying to hide a field based on a condition then don't user getControl. It might not work in portal 

use g_form.setDisplay('your_field_name',false);

 

Hope this helps 

Mark my answer correct if this helps you

Thanks

Hi @vinod9 

 

Please try to replace '||' to '&&" online line 21:

 

else if (types !=true && types!='true') {

 

Try if this helps.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023