Date/Time validation script

Masa29
Tera Contributor

We got "Invalid Text" error for date/time field of value '1970-01-01 00:00:00' GMT while saving the record on UI.

 

Below procedure can reproduce the issue (SN version Geneva):

A. Preparation  

1. Create a new table u_datetime_test

2. Add a field u_datetime_field

Type: Date/Time

Readonly: true

Default value:  javascript:gs.dateGenerate('1970-01-01', '00:00:00')

3. Add a client script for u_datetime_test.

Name: DateTime Test - Form on load

Type: onLoad

Table: u_datatime_test

Script:

function onLoad() {

   var datetime_val = g_form.getValue('u_datetime_field');

   if(!datetime_val){

  return;

   }

   if(datetime_val < '1970-01-01'){

  g_form.setValue('u_datetime_field', '1970-01-01 00:00:00');

   }

}

 

B. Test steps

1. Set SN time zone to GMT

2. Open the form of u_datetime_test table. (http://{instnace_name}/u_datetime_test.do)

3. Click Save buttone to create a new record.

4. Switch SN time zone to US/Eastern

5. http://{instnace_name}/u_datetime_test_list.do

Result: the value of u_datetime_field is '1969-12-31 19:00:00' (Date/Time display value is converted to local time zone)

6. Click on the record to open the form

Result: the value of u_datetime_field is '1970-01-01 00:00:00' (Display value is changed by the client script 'DateTime Test - Form on load')

7. Click Save button.

Result: Error message "Invalid text" under u_datetime_field and the record cannot be saved.

 

Extended tests :

Preconditions: Create a new record in GMT for every test case and open the record in EST - make sure the value of u_datetime_field is '1970-01-01 00:00:00' (GMT) in XML and display value is less than '1970-01-01 00:00:00' in list view.

Test case 1. If I change u_datetime_field to be writable (Readonly  = false), the record can be saved successfully, and the value of u_datetime_field is '1970-01-01 05:00:00' GMT in xml (GMT = EST + 5 hrs).

Test case 2. u_datetime_field is Readonly. Goto System Definition -> Validation Scripts -> Deactive the script for Date/Time (Type = Date/Time), then the record can be saved successfully.

 

Conclusion:  

1. If the display value of Date/Time field is less than '1970-01-01 00:00:00', record cannot be saved due to "Invalid text" error.

2. If the display value of Date/Time field is greater than '1970-01-01 00:00:00' and the Date/time field is readonly, record cannot be saved due to "Invalid text" error.

3. If the display value of Date/Time field is greater than '1970-01-01 00:00:00' and the Date/time field is NOT readonly, record can be saved and value of the Date/time field is converted to GMT based on the display value.

4. If the display value of Date/Time field is less than '1970-01-01 00:00:00', record cannot be saved due to "Invalid text" error no matter the field is readonly or not.

4. If the Date/Time validation script is disabled, the record can be saved successfully even though the display value is less than '1970-01-01 00:00:00'.

 

I find the link  http://wiki.servicenow.com/index.php?title=Validate_Date_and_Time#gsc.tab=0. Our Date/Time validation script is the one with  hard coded format. It is the original script coming with ServiceNow release.  Questions:

How does the date/time validation script work? i.e. Does it  validate datebase value or display value?

Why does the display value '1970-01-01 00:00:00' passes validation when the field is read/write but gets error when the field is readonly?

If the original validation script may cause problem, why ServiceNow doesn't use the updated script by default?

9 REPLIES 9

Mwatkins
ServiceNow Employee
ServiceNow Employee

I can't reproduce conclusion #4. I am successfully able to set a date/time field to a value prior to 1970.


Screenshot 2017-04-18 11.26.08.png



Also, the code in the Date/Time validation script for Geneva does not give an "Invalid text" response. It gives "Invalid Date".


Does the code in your validator look like this?


function validate(value) {


      // empty fields are still valid dates


      if(!value)


              return true;




      // We "should" have the global date format defined always defined. but there's always that edge case...


      if(typeof g_user_date_time_format !== 'undefined')


              return isDate(value, g_user_date_time_format) ? true : getMessage("Invalid Date");




      // if we don't have that defined, we can always try guessing


      return parseDate(value) !== null ? true : getMessage("Invalid Date");



}


Masa29
Tera Contributor

No, our validator code is not customized by us. It's like below.



function validate(value) {


if (!value) {


return true;


}


return (getDateFromFormat(value,'yyyy-MM-dd HH:mm:ss') != 0);


}


Mwatkins
ServiceNow Employee
ServiceNow Employee

Your code may not be customized by you, but it is not the out-of-box Geneva code.


What do you see if you look in these places?



/sys_upgrade_history_log_list.do?sysparm_query=file_name=sys_script_validator_829b0bb0533131008d7d154f811c08f7



/sys_update_xml_list.do?sysparm_query=GOTOnameLIKEsys_script_validator_829b0bb0533131008d7d154f811c08f7


Masa29
Tera Contributor

Ah, the validator was created by somebody in our firm back in 2010. How can I get the out-of-box code?



/sys_upgrade_history_log_list.do?sysparm_query=file_name=sys_script_validator_829b0bb0533131008d7d154f811c08f7


returns 4 records inserted by system



/sys_update_xml_list.do?sysparm_query=GOTOnameLIKEsys_script_validator_829b0bb0533131008d7d154f811c08f7


returns nothing.



Thanks,


Masa