Require a field based on value changing in another

Edwin Fuller
Tera Guru

Hi everyone, within the form I created I have a date field called "Expected Completion Date". Whenever the value of the field changes to any other value I would like the next field "Expected Completion Date Change Reason" to become a required field. So basically in order to change the date field you need to provide a reason. Does anyone know how this can be done, possible using a script?

find_real_file.png

Thanks,

Edwin

1 ACCEPTED SOLUTION

Try this



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


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


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


g_form.setMandatory('u_start_date', false);


}


  return;  


  }  


  if(oldValue != newValue)  


  {  


  g_form.setMandatory('u_start_date', true); //Here replace u_start_date with exact field column name  


  }  


  else  


  {  


  g_form.setMandatory('u_start_date', false); //Here replace u_start_date with exact field column name  


  }  


 


  //Type appropriate comment here, and begin script below  


 


}  


View solution in original post

8 REPLIES 8

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Edwin,



This is simple. You can create a UI policy and trigger it only when i.e Expected completion date is changed then make Expected completed date change reason to mandatory.


Please refer below link for more info.


http://wiki.servicenow.com/index.php?title=Creating_a_UI_Policy


Abhinay Erra
Giga Sage

Write an onChnage client script on the Expected completion date field and put this script in there



g_form.setMandatory("your field name goes here",true);


The script works as intended, but I need a condition. When the field is populated for the first time or there is no previous value then do NOT require the next field


Try this



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


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


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


g_form.setMandatory('u_start_date', false);


}


  return;  


  }  


  if(oldValue != newValue)  


  {  


  g_form.setMandatory('u_start_date', true); //Here replace u_start_date with exact field column name  


  }  


  else  


  {  


  g_form.setMandatory('u_start_date', false); //Here replace u_start_date with exact field column name  


  }  


 


  //Type appropriate comment here, and begin script below  


 


}