Date/time input setup for different action item types
Summarize
Summary of Date/time input setup for different action item types
This document explains how to configure date/time inputs for different action item types in ServiceNow after creating an input form screen with theIncludeTimezoneattribute enabled. Proper setup ensures date/time values are correctly handled and stored across various action items, accounting for time zones and backend processing.
Show less
New or Update Action Items
For New or Update action items, map date/time inputs directly to date/time fields in the backend instance. The backend receives the Device Time Zone info from the input form and converts the date/time value to Coordinated Universal Time (UTC/GMT) before saving it.
Script Action Items
When using Script action items, you must manually convert date/time inputs to UTC/GMT. The document provides example scripts demonstrating how to handle date/time values using GlideDateTime and GlideScheduleDateTime objects.
GlideDateTime Script Example
- Shows setting fields of the GlideDateTime type, such as expectedstart and estimatedend.
- Uses the input string (with timezone info) to instantiate GlideDateTime objects.
- Validates that the start date precedes the end date, calculates duration, and updates the corresponding record fields.
- Includes logic to detect scheduling conflicts (double booking) and flags records for attention when conflicts exist.
- Directly sets Date Time columns by passing GlideDateTime objects to
GlideRecord.setValue().
GlideScheduleDateTime Script Example
- Demonstrates setting fields of GlideScheduleDateTime type for schedule entries, e.g., startdatetime and enddatetime.
- Initializes GlideDateTime objects from input date/time strings with timezone info.
- Assigns values using the internal display format (
getDisplayValueInternal()) to reflect the User Profile Time Zone, as these are schedule date/time columns, not standard Date Time columns. - Handles additional scheduling details such as repeat types and counts.
Key Takeaways for ServiceNow Customers
- When configuring date/time inputs on action item forms, enable IncludeTimezone to accurately capture user time zone data.
- For New or Update types, mapping inputs to backend fields leverages automatic UTC conversion.
- For Script types, custom scripting is necessary to convert and validate date/time values correctly, using GlideDateTime or GlideScheduleDateTime APIs.
- Proper validation and conflict checking improve scheduling reliability and user experience.
- Understanding the difference between Date Time columns and Schedule Date/Time columns is critical to correctly setting values in scripts.
After you create an input form screen and define its IncludeTimezone attribute for date/time inputs, you must associate the input form screen with an action item. How you set up the date/time inputs depends on the type of action item you use.
| Action item type | Set up |
|---|---|
| New or Update | Make sure that date/time inputs are mapped to date/time fields in the back-end instance. The back-end instance uses the Device Time Zone information sent by the input form screen. Then the back-end instance converts the date/time value to Coordinated Universal Time (UTC/GMT) before saving it to the database. |
| Script | You must convert the date/time input value to UTC/GMT. Refer to the following script
examples for more information. For more information about scripting in general for ServiceNow, see Scripting
Note: Customizations might be needed depending on the use case
involved. |
GlideDateTime script example
The following example sets fields of the GlideDateTime type.
/sys_sg_write_back_action_step.do?sys_id=b390e6a7c1120110fa9b5abd6a7dbb64
(function WriteBackAction(parm_input, parm_variable, actionResult) {
var shortDescription = parm_input.shortdescription;
var scheduledStart = parm_input.expectedstart;
var estimatedEnd = parm_input.estimatedend;
var description = parm_input.description;
var wotSysId = parm_variable['sys_id'];
var wotGr = new GlideRecord("wm_task");
wotGr.get(wotSysId);
wotGr.setValue("description", description);
wotGr.setValue("short_description", shortDescription);
var newEnd = new GlideDateTime(estimatedEnd);
var endMS = newEnd.getNumericValue();
var newStart = new GlideDateTime(scheduledStart);
var startMS = newStart.getNumericValue();
if (endMS - startMS < 0) {
gs.addErrorMessage(gs.getMessage("The start date should come before the end date"));
} else {
//set the time on screen all the time
wotGr.setValue("expected_start", newStart);
wotGr.setValue("estimated_end", newEnd);
var duration = endMS - startMS;
var newDur = new GlideDuration();
newDur.setValue(duration);
wotGr.setValue("estimated_work_duration", newDur);
//check double booking no matter which field is changed. set needs_attention so that dispatcher can review it.
var info = new global.FSMAgentInfo(gs.getUserID());
var doubleBooking = info.allowAgentDoubleBookingTask();
if (doubleBooking){
var conflict = !(new global.SMDDateValidation().checkSchedulingConflictSimple(wotGr));
if (conflict) {
//BR Date Checks will display conflict message as Warning: {0} has been scheduled for a time the assigned
//only when time change.
wot.Gr.setValue("needs_attention", true);
}
}
- In this example, the
expected_startandestimated_endfields are edited based on the values of their corresponding date/time inputs ofexpectedstartandestimatedend. - Because the IncludeTimezone attribute is set to true, the
parm_input.expectedstartvariable returns a date/time string in the format YYYY-MM-DDThh:mm:ss.sssTZD. To enable the system to handle this date/time format, initialize a GlideDateTime object by passing the string into the constructor. - Because
expected_startandestimated_endare Date Time columns in the database, you can directly call GlideRecord.setValue() and pass theGlideDateTimeobject as the value.
GlideScheduleDateTime script example
The following example sets fields of the GlideScheduleDateTime type.
/sys_sg_write_back_action_item.do?sys_id=539b20d6b72120107be0e34e9e11a971
(function WriteBackAction(parm_input,parm_variable,actionResult) {
var personalSchedule = new global.FSMMobileUtil().getUserSchedule(parm_variable.user);
if (gs.nil(personalSchedule)) {
gs.error("create_event: personal schedule id is not found.");
gs.addErrorMessage(gs.getMessage("Create Event Failed"));
return;
}
if(gs.nil(parm_input.type))
parm_input.type = "";
var scheduleEntryGR = new GlideRecord("cmn_schedule_span");
scheduleEntryGR.initialize();
scheduleEntryGR.setValue("name", parm_input.name);
var start_time = new GlideDateTime(parm_input.start_time);
scheduleEntryGR.start_date_time = start_time.getDisplayValueInternal();
var end_time = new GlideDateTime(parm_input.end_time);
scheduleEntryGR.end_date_time = end_time.getDisplayValueInternal();
scheduleEntryGR.setValue("show_as", parm_input.show_as);
scheduleEntryGR.setValue("type", parm_input.type);
scheduleEntryGR.setValue("user", parm_variable.user);
scheduleEntryGR.setValue("schedule", personalSchedule);
scheduleEntryGR.setValue("repeat_type", parm_input.repeats);
if(parm_input.repeats!=""){
scheduleEntryGR.setValue("repeat_count", parm_input.repeat_every);
scheduleEntryGR.repeat_until = parm_input.repeat_until;
}
scheduleEntryGR.insert();
})(parm_input,parm_variable,actionResult);
- In this example, the
start_date_timeandend_date_timefields of a schedule entry are set based on the values of their corresponding date/time inputs ofstart_timeandend_time. - Because the IncludeTimezone attribute is set to
true, the
parm_input.start_timevariable returns a date/time string in the format YYYY-MM-DDThh:mm:ss.sssTZD. To enable the system to handle this date/time format, initialize aGlideDateTimeobject by passing the string into the constructor. - Because
start_date_timeandend_date_timeare not Date Time Columns in the database (instead, they are Schedule Date/Time columns), their value are set withgr.start_date_time = {date_time_value}.Note:Thedate_time_valueis expected to be in the User Profile Time Zone (the value provided is expected to be theDisplay Valueof aGlideDateTime). This configuration makes it possible to call the GlideDateTime.getDisplayValueInternal() method, which returns a date/time string in the internal format of YYYY-MM-DD hh:mm:ss and in the User Profile Time Zone.