Name

global.PlannedTaskStatus

Description

This script include can be used to determine what should be status of planned_task as per Servicenow status policy Red if planned end date < current date Yellow (planned end date +3 )< current date Green anything else

Script

var PlannedTaskStatus = Class.create();
PlannedTaskStatus.prototype = {
  initialize: function(topTask, days) {
  	var currentDate = new GlideDate();
  	var currentDateTime = new GlideDateTime();
  	currentDateTime.setDisplayValueInternal(currentDate.getDisplayValueInternal() + " " + "00:00:00");
  	var api = new SNC.PlannedTaskAPI();
      var scheduledEndDate = api.calculateEndDate(currentDateTime, days + " 00:00:00", this.getSchedule(topTask));
  	var scheduledEndDateObj = JSON.parse(scheduledEndDate);
      this.yellowStratagyDate = new GlideDateTime();
  	this.yellowStratagyDate.setDisplayValue(scheduledEndDateObj.date);
  	this.offScheduleYellowStratagyDate = new GlideDateTime();
  	this.offScheduleYellowStratagyDate.addDays(days);
  },
  
  determineStatus: function(gr) {
      var endDate = new GlideDateTime(gr.getValue('end_date'));
  	if(this.isOffSchedule(gr))
  		return this._determineStatus(endDate, this.offScheduleYellowStratagyDate);
  	else
  		return this._determineStatus(endDate, this.yellowStratagyDate);
  },
  
  _determineStatus: function(endDate, yellowStratagyEndDate) {
  	var currentDate = new GlideDate();
  	if (endDate.getLocalDate().onOrBefore(currentDate.getLocalDate()))
          return 'red';
  	else if(endDate.onOrBefore(yellowStratagyEndDate))
  		return 'yellow';
  	else 
  		return 'green';
  },

  getSchedule: function(top_task) {
  	return new PTGlobalAPI().getScheduleId(top_task);
  },

  isOffSchedule: function(gr) {
  	var allowOutSideSchedule = gr.getValue('allow_dates_outside_schedule');
  	return (allowOutSideSchedule == 1) || (allowOutSideSchedule == 'true');
  },
  
  type: 'PlannedTaskStatus'
};

Sys ID

5a5358eb537d1010a887ddeeff7b12d2

Offical Documentation

Official Docs: