{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":1026,"path":"api-reference/server-api-reference/c_GlideScheduleScopedAPI.md","title":"GlideSchedule - Scoped","bundle":null,"doc_type":null,"release":"australia","body":"\n# GlideSchedule- Scoped\n\nThe scoped GlideSchedule API provides methods for performing operations on GlideSchedule objects, such as adding new schedule segments to a schedule, determining if a date/time is within the schedule, or setting the schedule timezone.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## GlideSchedule - GlideSchedule\\(\\)\n\nInstantiates an empty GlideSchedule object.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n## GlideSchedule - GlideSchedule\\(String sysID, String timeZone\\)\n\nInstantiates a GlideSchedule object and loads the schedule information. If a time zone is not specified, the current session time zone is used.\n\n<table id=\"table_emk_4cg_1r\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nsysID\n\n</td><td>\n\nString\n\n</td><td>\n\nThe system ID for the schedule.\n\n</td></tr><tr><td>\n\ntimeZone\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. The time zone to use.Default: The current session time zone.\n\nTime zones can be provided in the following formats.\n\n-   `Country/City`. For example, `America/Los_Angeles`.\n-   `Country/Time zone`. For example, `US/Pacific`.\n-   `Time zone abbreviation`. For example, `PST`.\n\nFor a complete list of valid timezones, see the **Time zone** field in the User \\[sys\\_user\\] table. For more information about time zones, see [Time zones](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-administration/r_TimeZones.md).\n\n</td></tr></tbody>\n</table>```\nvar schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828', 'US/Pacific');\n```\n\n## GlideSchedule - add\\(GlideDateTime startDate, GlideDuration offSet\\)\n\nAdds a new schedule segment to the current schedule.\n\n|Name|Type|Description|\n|----|----|-----------|\n|startDate|GlideDateTime|The starting date of the new schedule segment.|\n|offSet|GlideDuration|The time offset of the new schedule segment.|\n\n|Type|Description|\n|----|-----------|\n|GlideDateTime|The schedule updated with the new schedule segment.|\n\n```\nvar startDate = new GlideDateTime('2014-01-02');\nvar days = 2;\nvar dur = new GlideDuration(60 * 60 * 24 * 1000 * days);\nvar schedule = new GlideSchedule();\nvar end = schedule.add(startDate, dur);\ngs.info(end);\n```\n\nOutput:\n\n```\n2014-01-04 00:00:00\n```\n\n## GlideSchedule - duration\\(GlideDateTime startDate, GlideDateTime endDate\\)\n\nDetermines the elapsed time in the schedule between two date time values using the timezone of the schedule or, if that is not specified, the timezone of the session.\n\n|Name|Type|Description|\n|----|----|-----------|\n|startDate|GlideDateTime|The starting datetime.|\n|endDate|GlideDateTime|The ending datetime.|\n\n|Type|Description|\n|----|-----------|\n|GlideDuration|The difference between the starting and ending datetime.|\n\n```\nvar startDate = new GlideDateTime('2014-10-16 02:00:00');\nvar endDate = new GlideDateTime('2014-10-18 04:00:00');\nvar schedule = new GlideSchedule();\n \nschedule.load('090eecae0a0a0b260077e1dfa71da828'); // loads \"8-5 weekdays excluding holidays\" schedule\nvar duration = schedule.duration(startDate, endDate);\ngs.info(duration.getDurationValue()); // gets the elapsed time in schedule\n```\n\n## GlideSchedule - getName\\(\\)\n\nRetrieves the schedule name.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|The name of the current schedule.|\n\n```\nsys_id ='04e664654a36232701a2247dcd8fc4cf'; // sys_id for \"Application\" schedule record\nvar sched = new GlideSchedule(sys_id);\ngs.info(sched.getName());\n```\n\n## GlideSchedule - isInSchedule\\(GlideDateTime time\\)\n\nDetermines if the specified date and time is within the current schedule.\n\n|Name|Type|Description|\n|----|----|-----------|\n|time|GlideDateTime|Date and time value to check.|\n\n<table id=\"table_nvd_jkg_1r\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the specified date and time is within the schedule.Valid values:\n\n-   true: Date and time is within the schedule.\n-   false: Date and time are outside of the schedule.\n\n</td></tr></tbody>\n</table>```\nvar glide = new GlideRecord('cmn_schedule');\nglide.addQuery('type', 'blackout');\nglide.query();\nif (glide.next()) {\n   var sched = new GlideSchedule(glide.sys_id);\n   var date = new GlideDateTime();\n   date.setDisplayValue(\"2007-09-18 12:00:00\");\n   if (sched.isInSchedule(date)) \n      gs.info(\"Is in the schedule\");\n   else\n      gs.info(\"Is NOT in the schedule\");\n}\n```\n\n## GlideSchedule - isValid\\(\\)\n\nDetermines if the current schedule is valid. A schedule is valid if it has at least one schedule span.\n\n|Type|Description|\n|----|-----------|\n|Boolean|True if the schedule is valid.|\n\n```\nvar glide = new GlideRecord('cmn_schedule');\nglide.addQuery('type', 'blackout');\nglide.query();\nif (glide.next()) {\n   var sched = new GlideSchedule(glide.sys_id);\n   var date = new GlideDateTime();\n   date.setDisplayValue(\"2007-09-18 12:00:00\");\n   if (sched.isValid()) \n      gs.info(\"Is valid\");\n \n   else\n      gs.info(\"Is not valid\");\n}\n```\n\n## GlideSchedule - load\\(String sysID, String timeZone, String excludeSpanID\\)\n\nLoads a schedule with the schedule information.\n\n|Name|Type|Description|\n|----|----|-----------|\n|sysID|String|The system ID of the schedule.|\n|timeZone|String|\\(Optional\\) The timezone. If a timezone is not specified, or is nil, the current session timezone is used for the schedule.|\n|excludeSpanID|String|Any span to exclude.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar x = new GlideSchedule();\nx.load('08fcd0830a0a0b2600079f56b1adb9ae');\n```\n\n## GlideSchedule - setTimeZone\\(String timeZone\\)\n\nSets the timezone for the current schedule.\n\n<table id=\"table_sdh_hzg_1r\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\ntimeZone\n\n</td><td>\n\nString\n\n</td><td>\n\nThe time zone to use.Time zones can be provided in the following formats.\n\n-   `Country/City`. For example, `America/Los_Angeles`.\n-   `Country/Time zone`. For example, `US/Pacific`.\n-   `Time zone abbreviation`. For example, `PST`.\n\nFor a complete list of valid timezones, see the **Time zone** field in the User \\[sys\\_user\\] table. For more information about time zones, see [Time zones](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-administration/r_TimeZones.md).\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|void| |\n\nThis example sets the timezone for the schedule to US/Pacific.\n\n```\nvar schedule = new GlideSchedule();\nschedule.setTimeZone('US/Pacific');\n```\n\n## GlideSchedule - whenNext\\(GlideDateTime time, String timeZone\\)\n\nDetermines how much time \\(in milliseconds\\) until start time of the next schedule item.\n\nThis function is intended to be called when the GlideSchedule object \\(cmn\\_schedule table\\) is not currently in the schedule window. The whenNext\\(\\) call returns duration \\(in ms\\) until the GlideSchedule object is within the schedule. This function does not return a meaningful value if called when the GlideSchedule object is within the schedule.\n\n|Name|Type|Description|\n|----|----|-----------|\n|time|GlideDateTime|Time to be evaluated|\n|timeZone|String|Timezone|\n\n|Type|Description|\n|----|-----------|\n|Number|Number of milliseconds until the start time of the next schedule item. Returns -1 if never.|\n\n```\nvar startDate = new GlideDateTime('2014-10-25 08:00:00');\nvar glideSchedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae', 'UTC');\ngs.info(glideSchedule.whenNext(startDate));\n```\n\nOutput:\n\n```\n172800000\n```\n\n```\ntestScript(); \nfunction testScript() { \nvar now = new GlideDateTime(); //current date and time\nvar sched = new GlideSchedule(\"<sys_id>\"); // Use a cmn_schedule sys_id \nif (sched.isInSchedule(now)){ \ngs.info('We are in an active schedule window so whenNext() is not helpful'); \n} else{  \ngs.info('Not currently in schedule so call whenNext()'); \nvar msUntilNext = sched.whenNext(new GlideDateTime(), 'US/Pacific'); \ngs.info('Next schedule starts in '+msUntilNext+' milliseconds'); \n} \n}\n\\\\ Output [schedule inactive)]:\n\\\\ *** Script: Not currently in schedule so call whenNext() \n\\\\ *** Script: Next schedule starts in -1 milliseconds\n```\n\nOutput:\n\n```\n[Scheduled for future] *** Script: Not currently in schedule *** Script: Next schedule starts in 332894000 milliseconds\n```\n\n"}}