Sunday, December 10, 2023

ServiceNow: Date and Time validation on layouts and custom forms


ServiceNow: Date and Time validation

Date and Time format along with System timezone setting for ServiceNow instance can be checked in System Properties à System.


Date format - use the same 'format' strings as the java.text.SimpleDateFormat class, with minor exceptions. Note that MM is months, where mm indicates minutes. The format string consists of the following abbreviations:

Field
Full Form  
Short Form
Year
yyyy (4 digits)
yy (2 digits), y (2 or 4 digits)
Month
MMM (name or abbr.)
MM (2 digits), M (1 or 2 digits)
Day of Month
dd (2 digits)
d (1 or 2 digits)

For example: yyyy-MM-dd


Time format - use the same 'format' strings as the java.text.SimpleDateFormat class, with minor exceptions. The format string consists of the following abbreviations:

Field
Full Form  
Short Form
Hour (1-12)
hh (2 digits)
h (1 or 2 digits)
Hour (0-23)
HH (2 digits)
H (1 or 2 digits)
Minute
mm (2 digits)
m (1 or 2 digits)
Second
ss (2 digits)
s (1 or 2 digits)
AM/PM
 a

Example: HH:mm:ss


System timezone, used as default for calendars and users. Olson/zoneinfo timezone values are accepted. Examples:

Africa/Johannesburg
America/Toronto
America/Mexico_City
Antarctica/Vostok
Asia/Shanghai
Asia/Tokyo
Australia/Sydney
Europe/Berlin
Europe/London
US/Pacific
US/Mountain
US/Central
US/Eastern

For example: UTC


Appropriate validations are applied for required date and time fields on ServiceNow forms, as different user may work on same instance in different timezone with respective date and time settings.

To restrict users from using different date time formats on ServiceNow forms, Validation Scripts can be added for respective data type (like for e.g. Date type) to conform with instance settings

function validate(value) {

            if(!value)
            {
                        return true;
            }          
            return(getDateFromFormat(value,'yyyy-MM-dd') != 0);       
           
}


Client scripts can also be written when field value is changed or ServiceNow form is submitted.

function onChange(control, oldValue, newValue, isLoading) {  
    if (isLoading || newValue == oldValue) {  
          return;  
    }  

    if (getDateFromFormat(newValue, 'yyyy-MM-dd HH:mm:ss') == 0) {  
          //your message for the date field - Invalid date
    } 
     

function onSubmit() {  
     if (getDateFromFormat(g_form.getValue('yourField'), 'yyyy-MM-dd HH:mm:ss') == 0) {
          //your message for the date field - Invalid date
          return false;    // Stop the form submit event
     }  
   
}


Date and time format setting for the ServiceNow instance can be checked through scripts as well

var rec = new GlideRecord(‘sys_properties’);
rec.addQuery(‘name’, ‘glide.sys.date_format');
rec.query();
if(rec.next()){
    var yourPropertyValue = rec.value;
    gs.print(yourPropertyValue);
}

var rec = new GlideRecord(‘sys_properties’);
rec.addQuery(‘name’, ‘glide.sys.time_format');
rec.query();
if(rec.next()){
    var yourPropertyValue = rec.value;
    gs.print(yourPropertyValue);
}


For custom form validation (for e.g. in UI page), separate script include with common functions (similar to client scripts logic) can be written to validate field formatting. These functions can be called through GlideAjax method in client script section of UI page.

6 comments:

  1. The Community-Driven Education is the best to help us and provide great results. The Project Ownership Where it Belongs is amazing and I like that you shared this post for us to know about these ideas. Also from 2V0-31.20 Exam Dumps Questions I realize that it is more helpful for us.

    ReplyDelete

Popular Posts