{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":40032,"path":"platform-administration/time-configuration/r_RangeCalculatorScripts.md","title":"Range calculator scripts","bundle":null,"doc_type":null,"release":"australia","body":"\n# Range calculator scripts\n\nYou can specify a script include that calculates range restrictions and processes parent updates in a timeline page.\n\n## Range Calculator Scripts\n\nFollowing are three examples of script includes that help specify range restrictions.\n\n**ExampleUpdateParentsRangeCalculator**\n\nUpdates parent records when a child record span is moved or resized in the timeline.\n\n```\nvar ExampleUpdateParentsRangeCalculator = Class.create();\nExampleUpdateParentsRangeCalculator.prototype = {\ninitialize: function() { },\nupdateParents: function(id, table, startDate, endDate){\n if (table == \"rm_sprint\"){\n   var releaseID;\n   var sprint = new GlideRecord(table);\n   sprint.addQuery('sys_id', id);\n   sprint.query();\n   if (sprint.next())\n     releaseID = sprint.release + \"\";\n   if (releaseID) {\n         var now_GR = new GlideRecord(\"rm_release_scrum\");\n         gr.addQuery(\"sys_id\", releaseID);\n         gr.query();\n         if (gr.next()) {\n           if (startDate  && startDate < this.getTimeMs(gr.start_date))\n               gr.start_date = this.getTimeObject(startDate); \n           if (endDate && endDate > this.getTimeMs(gr.end_date))\n              gr.end_date = this.getTimeObject(endDate);\n              gr.update();\n         }\n    }\n  }\n},  \ngetMinRangeDetails: function(id, table){ return [-1, -1, \"\", \"\"]; },\ngetMaxRangeDetails: function(id, table){ return [-1, -1, \"\"]; },\ngetTimeMs: function(date){ \n  return new GlideScheduleDateTime(date).getMS(); },\ngetTimeObject: function(timeMS) { \n  var gdt = new GlideDateTime(); \n  gdt.setNumericValue(timeMS); \n  return gdt; },\nlogMessage: function(message){ gs.log(message); },\ntype: 'ExampleUpdateParentsRangeCalculator'\n}\n```\n\nIn this example, the span is identified based on the id and table from function\\(id, table, startDate, endDate\\).\n\n**ExampleMinRangeCalculator**\n\nDefines the earliest start date and the latest end date for a specified span.\n\n```\nvar ExampleMinRangeCalculator = Class.create();\n  ExampleMinRangeCalculator.prototype = {\n      initialize: function() { },\n      updateParents: function(id, table, startDate, endDate){ },\n      getMinRangeDetails: function(id, table){\n           var min = -1;\n           var max = -1;\n           var minID = \"\";\n           var maxID = \"\";\n           if (table == \"rm_release_scrum\"){\n                    var now_GR = new GlideRecord(\"rm_sprint\");\n                    gr.addQuery(\"release\", id);\n                    gr.query();\n                    while(gr.next()){\n                         var start = this.getTimeMs(gr[\"start_date\"]);\n                         var end = this.getTimeMs(gr[\"end_date\"]);\n                         var id = gr[\"sys_id\"];\n                         if (min == - 1 || start <= min){\n                                if (start != min)\n                                     minID = \"\";\n                         min = start;\n                         minID += \",\" + id;\n                         }\n                         if (max == -1 || end >= max){\n                                    if (end != max)\n                                            maxID = \"\";\n                                    max = end;\n                                    maxID += \",\" + id;\n                            }  \n                     }\n            }\n        return [min, max, minID, maxID];\n},\ngetMaxRangeDetails: function(id, table){ return [-1, -1, \"\"]; },\ngetTimeMs: function(date){ return new GlideScheduleDateTime(date).getMS(); },\ngetTimeObject: function(timeMS) { \n  var gdt = new GlideDateTime(); \n  gdt.setNumericValue(timeMS); \n  return gdt; },\nlogMessage: function(message){ gs.log(message); },\ntype: 'ExampleUpdateParentsRangeCalculator'\n}\n```\n\n**ExampleMaxRangeCalculator**\n\nDefines the earliest start date and the latest end date for a specified span.\n\n```\nvar ExampleMaxRangeCalculator = Class.create();\nExampleMaxRangeCalculator.prototype = {\n   initialize: function() { },\n   updateParents: function(id, table, startDate, endDate){ },\n   getMinRangeDetails: function(id, table){ return [-1, -1, \"\", \"\"]; },\n   getMaxRangeDetails: function(id, table){\n      if (table == \"rm_sprint\"){\n         var sprint = new GlideRecord(table);\n         sprint.addQuery('sys_id', id);\n         sprint.query();\n         if (sprint.next())\n            releaseID = sprint.release + \"\";\n         if (releaseID) {\n            var now_GR = new GlideRecord(\"rm_release_scrum\");\n            gr.addQuery(\"sys_id\", releaseID);\n            gr.query();\n            if (gr.next())\n               return [this.getTimeMs(gr.start_date),\n                this.getTimeMs(gr.end_date), gr.sys_id];\n         }\n      }\n      return [-1, -1, \"\"];\n   },\n   getTimeMs: function(date){ return new ScheduleDateTime(date).getMS(); },\n   getTimeObject: function(timeMS) { \n     var gdt = new GlideDateTime();\n     gdt.setNumericValue(timeMS); \n     return gdt; },\n   logMessage: function(message){ gs.log(message); },\n   type: 'ExampleUpdateParentsRangeCalculator'\n}\n```\n\nUse the following two functions to obtain the correct start and end dates in the three example script includes provided for reference.\n\n```\ngetTimeMs: function(date){\n        return new ScheduleDateTime(date).getMS();\n    }\n```\n\n```\ngetTimeObject: function(timeMS) {\n        var gdt = new GlideDateTime();\n        gdt.setNumericValue(timeMS);\n        return gdt;\n    }\n```\n\n**Parent Topic:**[Timeline pages](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-administration/time-configuration/c_TimelinePages.md)\n\n"}}