H
ere are some examples of how you could use a simple Javascript ‘split’ method to parse out the date and/or time value from a date/time field. Both examples split out the date and time, populate them into variables, and then populate the date section into a regular date field on the same record.
Business rule example:
This could also be used in a graphical workflow ‘Run script’ activity but would need to include ‘current.update()’ at the end to call the update to the record.
Name: Populate Date from Date/Time
When: Before
Update: True
Condition: current.u_start_date_time.changes()
var dateSection = current.u_start_date_time.split(' ')[0]; //Gets the Date
var timeSection = current.u_start_date_time.split(' ')[1]; //Gets the Time
//Set the value of the Date field with the date from the Date/Time field
current.u_start_date = dateSection;
Client script example:
As is the case with all client scripting in Service-now, all fields referenced must actually be part of the rendered form (though they may be invisible) to be referenced by the client-side script.
Name: Populate Date from Date/Time
Type: OnChange
Field name: Start date/time (or whatever your date/time field is named)
function onChange(control, oldValue, newValue, isLoading) {
//If the page isn't loading
if (!isLoading) {
//If the new value isn't blank
if(newValue != '') {
var dateSection = g_form.getValue('u_start_date_time').split(' ')[0]; //Gets the Date
var timeSection = g_form.getValue('u_start_date_time').split(' ')[1]; //Gets the Time
//Set the value of the Date field with the date from the Date/Time field
g_form.setValue('u_start_date', dateSection);
}
}
}