{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":34781,"path":"mobile/datetime-input-setup-4-action-items.md","title":"Date/time input setup for different action item types","bundle":null,"doc_type":null,"release":"australia","body":"\n# Date/time input setup for different action item types\n\nAfter 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.\n\n<table id=\"table_iz4_xnx_t5b\"><thead><tr><th>\n\nAction item type\n\n</th><th>\n\nSet up\n\n</th></tr></thead><tbody><tr><td>\n\n**New** or **Update**\n\n</td><td>\n\nMake 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.\n\n</td></tr><tr><td>\n\n**Script**\n\n</td><td>\n\nYou 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](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/scripts/c_Script.md) **Note:** Customizations might be needed depending on the use case involved.\n\n</td></tr></tbody>\n</table>## GlideDateTime script example\n\nThe following example sets fields of the GlideDateTime type.\n\n```\n/sys_sg_write_back_action_step.do?sys_id=b390e6a7c1120110fa9b5abd6a7dbb64\n(function WriteBackAction(parm_input, parm_variable, actionResult) {\n\tvar shortDescription = parm_input.shortdescription;\n\tvar scheduledStart = parm_input.expectedstart;\n\tvar estimatedEnd = parm_input.estimatedend;\n\tvar description = parm_input.description;\n\tvar wotSysId = parm_variable['sys_id'];\n\t\n\tvar wotGr = new GlideRecord(\"wm_task\");\n\twotGr.get(wotSysId);\n\twotGr.setValue(\"description\", description);\n\twotGr.setValue(\"short_description\", shortDescription);\n\t\n\tvar newEnd = new GlideDateTime(estimatedEnd);\n\tvar endMS = newEnd.getNumericValue();\n\t\n\tvar newStart = new GlideDateTime(scheduledStart);\n\tvar startMS = newStart.getNumericValue();\n\t\n\tif (endMS - startMS < 0) {\n\t\tgs.addErrorMessage(gs.getMessage(\"The start date should come before the end date\"));\n\t} else {\n\t\t//set the time on screen all the time\n\t\twotGr.setValue(\"expected_start\", newStart);\n\t\twotGr.setValue(\"estimated_end\", newEnd);\n\t\tvar duration = endMS - startMS;\n\t\tvar newDur = new GlideDuration();\n\t\tnewDur.setValue(duration);\n\t\twotGr.setValue(\"estimated_work_duration\", newDur);\n\t\t\n\t\t//check double booking no matter which field is changed. set needs_attention so that dispatcher can review it.\n\t\tvar info = new global.FSMAgentInfo(gs.getUserID());\n\t\tvar doubleBooking = info.allowAgentDoubleBookingTask();\n\t\tif (doubleBooking){\n\t\t\tvar conflict = !(new global.SMDDateValidation().checkSchedulingConflictSimple(wotGr));\n\t\t\tif (conflict) {\n\t\t\t\t//BR Date Checks will display conflict message as Warning: {0} has been scheduled for a time the assigned\n\t\t\t\t//only when time change.\n\t\t\t\twot.Gr.setValue(\"needs_attention\", true);\n\t\t\t}\n\t\t}\n```\n\n-   In this example, the `expected_start` and `estimated_end` fields are edited based on the values of their corresponding date/time inputs of `expectedstart` and `estimatedend`.\n-   Because the IncludeTimezone attribute is set to `true`, the `parm_input.expectedstart` variable 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.\n-   Because `expected_start` and `estimated_end` are Date Time columns in the database, you can directly call GlideRecord.setValue\\(\\) and pass the `GlideDateTime` object as the value.\n\n## GlideScheduleDateTime script example\n\nThe following example sets fields of the GlideScheduleDateTime type.\n\n```\n/sys_sg_write_back_action_item.do?sys_id=539b20d6b72120107be0e34e9e11a971\n(function WriteBackAction(parm_input,parm_variable,actionResult) {\n\n\tvar personalSchedule = new global.FSMMobileUtil().getUserSchedule(parm_variable.user);\n\tif (gs.nil(personalSchedule)) {\n\t\tgs.error(\"create_event: personal schedule id is not found.\");\n\t\tgs.addErrorMessage(gs.getMessage(\"Create Event Failed\"));\n\t\treturn;\n\t}\n\t\n\tif(gs.nil(parm_input.type))\n\t\tparm_input.type = \"\";\n\t\n\tvar scheduleEntryGR = new GlideRecord(\"cmn_schedule_span\");\n\tscheduleEntryGR.initialize();\n\tscheduleEntryGR.setValue(\"name\", parm_input.name);\n\t\n\tvar start_time = new GlideDateTime(parm_input.start_time);\n\tscheduleEntryGR.start_date_time = start_time.getDisplayValueInternal();\n\t\n\tvar end_time = new GlideDateTime(parm_input.end_time);\n\tscheduleEntryGR.end_date_time = end_time.getDisplayValueInternal();\n\t\n\tscheduleEntryGR.setValue(\"show_as\", parm_input.show_as);\n\tscheduleEntryGR.setValue(\"type\", parm_input.type);\n\tscheduleEntryGR.setValue(\"user\", parm_variable.user);\n\tscheduleEntryGR.setValue(\"schedule\", personalSchedule);\n\tscheduleEntryGR.setValue(\"repeat_type\", parm_input.repeats);\n\tif(parm_input.repeats!=\"\"){\n\t\tscheduleEntryGR.setValue(\"repeat_count\", parm_input.repeat_every);\n\t\tscheduleEntryGR.repeat_until = parm_input.repeat_until;\n\t}\n\tscheduleEntryGR.insert();\n\t\n\t\n})(parm_input,parm_variable,actionResult);\n```\n\n-   In this example, the `start_date_time` and `end_date_time` fields of a schedule entry are set based on the values of their corresponding date/time inputs of `start_time` and `end_time`.\n-   Because the **IncludeTimezone** attribute is set to `true`, the `parm_input.start_time` variable 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.\n-   Because `start_date_time` and `end_date_time` are not Date Time Columns in the database \\(instead, they are Schedule Date/Time columns\\), their value are set with `gr.start_date_time = {date_time_value}`.\n\n    **Note:** The `date_time_value` is expected to be in the User Profile Time Zone \\(the value provided is expected to be the `Display Value` of a `GlideDateTime`\\). 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.\n\n\n"}}