Display form section on choice selection otherwise hide

JohnDF
Mega Sage

Hi everyone,

 

I have a requirement that a form section is only display when I select a specific choice in a choice field otherwise the should be hidden.

I created a on change client script but its not working. The section is displayed and when a select "SLIR" choice the section is hidden?? What could be the problem?

 

 

 

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

   //Type appropriate comment here, and begin script below
   var select=g_form.getValue('u_themeselection');
	if(select=='SLIR')
		{
   g_form.setSectionDisplay('slir', true);
		}
	else{
		g_form.setSectionDisplay('slir', false);
	}
}

 

 

 

Thanks for your hep.

1 ACCEPTED SOLUTION

You can write 2 client scripts:

  1. OnLoad of form where you can hide the section.
  2. OnChange of choice field where you can check choice value and show the section.

Or you can also have only the OnChange client script if you always want to hide the section even choice value is SLIR

reshmapatil_0-1671077235458.png

 

 

Just one question, do you also want to hide the section on the load of the form even if the choice is "SLIR" in the backend (database)?

 

Regards,

Reshma

**Please mark my answer correct or helpful based on the impact**

View solution in original post

9 REPLIES 9

Veer
Tera Guru

Hello @JohnDF  Please verify with some alerts that the value that you are getting from getValue is correct, also verify if it is going inside the if & else block.

 

Also, go through the below article.

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0696583

Please mark this as a Helpful / Accepted Solution 

Mike_R
Kilo Patron
Kilo Patron

Make sure there aren't any mandatory fields in that sections. It won't hide the section if there are mandatory fields.

reshmapatil
Tera Guru

Hi @JohnDF , I tried the same on incident table and my code is working fine.

reshmapatil_0-1671045848599.png

  1. Check field name is correct or not. Use 'newValue' instead.
  2. Check the section name, for example Section name is 'Show Section' then we should use the name 'show_section'.
  3. Check for any mandatory fields.

     

    Regards,

    Reshma

    **Please mark my answer correct or helpful based on the impact**

     

@reshmapatil

Thanks for your reply. I guess you didnt understand me right.

 

The section should be hidden when I load the form, only when I select on the frm the choice "SLIR" in the choice fild than the tab should be visible.

 

Hopefully its now clear.