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

Make field visible when another field changes

BiancaK
Tera Expert

Hi Developers

I am trying to achieve the below. When a user changes the Forecasted date then the fields "Reason for change, Category and Sub category" should become visible on the form and mandatory. I was able to configure the requirement to make the field mandatory when user changes the Forecasted date. But how can I hide these fields from the form initially?

BiancaK_0-1722548381038.png

 

Please the code below:

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


  if (isLoading || newValue === '') {  


  if(g_form.isNewRecord() && newValue!=''){

g_form.setVisible('u_reason_for_change', false);
g_form.setMandatory('u_reason_for_change', false);


}
  return;  

  }  

  if(oldValue != newValue)  

  {  

  g_form.setValue('u_reason_for_change', true);
  g_form.setMandatory('u_reason_for_change', true);


  }  

  else  

  {  

  g_form.setVisible('u_reason_for_change', false);
  g_form.setMandatory('u_reason_for_change', false);


  }  


}  
1 ACCEPTED SOLUTION

@BiancaK Try writing a ui policy to hide all the field initially. Then update your if condition for above code share by me as 

if (oldValue != '' && oldValue != newValue)

 

…………………………………………..
Mark it helpful 👍and Accept Solution !! If this helps you to understand.

View solution in original post

15 REPLIES 15

Thank you so much @Satishkumar B for all your help, appreciate it.

@BiancaK Try writing a ui policy to hide all the field initially. Then update your if condition for above code share by me as 

if (oldValue != '' && oldValue != newValue)

 

…………………………………………..
Mark it helpful 👍and Accept Solution !! If this helps you to understand.

Additionally, I see the performance is not great using a Client script. How can this be achieved with a UI policy?

NikhilKumaK
Tera Expert

Hi,

 

I have tried in my PDI, It's working fine by using UI Policy, Without using client script.
Configure the UI Policy as below steps:

NikhilKumaK_0-1722574989137.png

UI Policy Actions made the fields Visibility and Mandatory as True:

NikhilKumaK_1-1722575062344.png

Testing:

If forecasted date is empty the fields is not visible:

NikhilKumaK_2-1722575208944.png

If Forecasted date is selected then the required fields are visible and mandatory as per your requirement:

NikhilKumaK_3-1722575304447.png

 

 

Hi @NikhilKumaK 

Thank you, please see below my requirement. 

On a new form the fields Reason for change, Category and Subcategory should not be visible. Because on the new form the user will first complete the Forecasted date and save the form. But then at a later stage the user will come back to the form and wants to change the Forecasted date, this is when the Reason for change, Category and Subcategory should display and be mandatory