The CreatorCon Call for Content is officially open! Get started here.

How to make a field appear based on value of another field.

mateusz3
Mega Contributor

Hi Guys,

I am struggling with this one.

I have two fields on the form:

-Difficulty

-New Ticket

What I need to do is to make the New Ticket field hidden on the form unless the difficulty field is Level 2

find_real_file.png

find_real_file.png

Any advice?

Regards

1 ACCEPTED SOLUTION

I would recommend using UI policy option here.


When possible, consider using a UI policy instead of a client script for these reasons:


  • UI policies have an Order field to allow full control over the order in which client-side operations take place.
  • UI policies do not require scripting to make a field mandatory, read-only, or visible.

http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices#Use_UI_Policies_Instead_of_C...


View solution in original post

8 REPLIES 8

amlanpal
Kilo Sage

Hi Matt,



The Best way is to Use UI Policy as Pradeep mentioned above. It is always recommended to use UI Policy over Client Script wherever possible.


In case you are looking for Client script, then you need to write an onChange Client Script on change of the field Difficulty. The sample code is given below. Please take care about the appropriate field name in the script:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (newValue === '') {


  g_form.setDisplay('u_new_ticket', false);


  return;


  }



  if (isLoading && g_form.getValue('u_difficulty') == '2'){


  g_form.setDisplay('u_new_ticket', true);


  }



  else if (newValue == '2'){


  g_form.setDisplay('u_new_ticket', true);


  }


  else{


  g_form.setDisplay('u_new_ticket', false);


  }


}



I hope this helps.Please mark correct/helpful based on impact


udaya1
Tera Contributor

Yes, it worked. Thank Amlan!

h_ctort
Giga Contributor

Hi, you need to use a ui policy with the condition difficulty is not level 2


with ui policy action New ticket visible=false.


this is best practice for this cases.


Community Alums
Not applicable

I can concur with the other replies that the UI policy is the easiest and best way to go since the system already had that functionality available to you. It should be a pretty easy UI policy to create for your need.