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

How to make fields read only when future date is selected in a Date field

varunkumar1
Tera Contributor

Hi,

I have 3 fields on a form as below

Date field and 2 string fields.

When Date selected is tomorrow's date, then other two string fields should be made as read only.

Please let me know how can this be done.

1 ACCEPTED SOLUTION

arron_ubhi
ServiceNow Employee
ServiceNow Employee

Hi Varun,



You can achieve this by using a Client Script, or possibly a UI policy to control this.



You can set a UI policy for the following, for example with the following conditions in the condition builder:



"Datefield1" "after" "current minute"




Best Regardds,



Arron


View solution in original post

7 REPLIES 7

Joe McCarty1
ServiceNow Employee
ServiceNow Employee

You could add an onChange script to the date field like this:



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


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


  return;


  }



  var date_field = new Date(getDateFromFormat(newValue, g_user_date_time_format));


  var current_date = new Date(new Date().getTime());


  if(current_date < date_field){


  g_form.setReadOnly('field_one',true);


  g_form.setReadOnly('field_two',true);


  }


}


I tried this out of curiosity and i found that below line is returning me epoch time, that is 1 jan 70.



var date_field = new Date(getDateFromFormat(newValue, g_user_date_time_format));  


The getDateFromFormat should return epoch time.   You use it because a user may have modified their date format preferences to get a 'sanitized' date in a common format.   When wrapped in a new Date object, it should be a standard client date object.   This makes it safe for comparison no matter what the user's date time preferences are.   There is a more complete explanation here (https://jamesfarrer.com/?id=319😞



Screen Shot 2017-03-17 at 7.41.30 AM.png


Understand thanks.