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

Thank you


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Edwin,



Looks like date filter is not applicable in UI policy. Please create a OnChange Client script on "Expected Completion Date" field with below script. Please modify field column names as per your req.


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


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


  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



}



Client Scripts - ServiceNow Wiki


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Edwin,



Is the issue resolved now?


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