Name

global.PlannedTaskNotificationService

Description

No description available

Script

var PlannedTaskNotificationService = Class.create();
PlannedTaskNotificationService.getNewNotifications = function(projectId) {
  var formattedNotifications = [];
  var ptn = new PlannedTaskNotification();    
  var notifications = ptn.getNewNotificationsForProject(projectId);
  var showTime = false;
  while ( notifications.next() ) {
      var n = {};
      n.id = notifications.getValue('sys_id');
      n.status = notifications.getValue('status');
      n.externalTask = {project: {}};
      n.impactedTask = {};
      
      var sourceRec = notifications.source.getRefRecord();
      var sourceTopTaskRec = notifications.source.top_task.getRefRecord();
      n.externalTask.short_description = sourceRec.getValue('short_description');
      n.externalTask.number = sourceRec.getValue('number');
      n.externalTask.sys_class_name = sourceRec.getValue('sys_class_name');
      n.externalTask.sys_id = sourceRec.getUniqueValue();
      n.externalTask.project.short_description = sourceTopTaskRec.getValue('short_description');
      n.externalTask.project.number = sourceTopTaskRec.getValue('number');
      n.externalTask.project.sys_class_name = sourceTopTaskRec.getValue('sys_class_name');
      n.externalTask.project.sys_id = sourceTopTaskRec.getUniqueValue();

      var taskRec = notifications.task.getRefRecord();
      var topTaskRec = notifications.task_top_task.getRefRecord();
      var shadowTaskRec = notifications.shadow_task.getRefRecord();
      n.impactedTask.short_description = taskRec.getValue('short_description');
      n.impactedTask.wbs = taskRec.getValue('wbs');
      n.impactedTask.number = taskRec.getValue('number');
      
      var taskStartDate = taskRec.start_date.getGlideObject().getLocalDate();
      var taskEndDate = taskRec.end_date.getGlideObject().getLocalDate();
      var shadowTaskStartDate = shadowTaskRec.start_date.getGlideObject().getLocalDate();
      var shadowTaskEndDate = shadowTaskRec.end_date.getGlideObject().getLocalDate();
      var latestStartDate = notifications.getValue('start_date');
      var latestEndDateJsonString = (new SNC.PlannedTaskAPI()).calculateEndDateInternal(latestStartDate,
              taskRec.getValue('duration'), topTaskRec.getValue("schedule"));
      var latestEndDateJson = JSON.parse(latestEndDateJsonString);
      var latestEndDate = latestEndDateJson.date;
      
      var previousStartDate, previousEndDate, currentStartDate, currentEndDate;
      if(JSUtil.notNil(notifications.getValue("previous_start_date"))) {
          previousStartDate = new GlideDateTime(notifications.getValue("previous_start_date"));
          currentStartDate = new GlideDateTime(taskRec.getValue("start_date"));
          currentEndDate = new GlideDateTime(taskRec.getValue("end_date"));
          var diffMs = currentEndDate.getNumericValue() - currentStartDate.getNumericValue();
          previousEndDate = new GlideDateTime(notifications.getValue("previous_start_date"));
          previousEndDate.add(diffMs);
      }

      //if(taskStartDate.equals(taskEndDate) || taskStartDate.equals(shadowTaskStartDate) || shadowTaskStartDate.equals(shadowTaskEndDate))
      showTime = true;
  
      if(showTime){
          n.oldStartDate = JSUtil.notNil(previousStartDate)? previousStartDate.getDisplayValue() : taskRec.getDisplayValue('start_date');
          // n.newStartDate = shadowTaskRec.getDisplayValue('start_date');
          n.newStartDate = notifications.getDisplayValue('start_date');
          n.oldEndDate = JSUtil.notNil(previousEndDate)? previousEndDate.getDisplayValue() : taskRec.getDisplayValue('end_date');
          // n.newEndDate = shadowTaskRec.getDisplayValue('end_date');
          n.newEndDate = latestEndDate;
      }else{
          n.oldStartDate = taskStartDate.getDisplayValue();
          // n.newStartDate = shadowTaskStartDate.getDisplayValue();
          n.newStartDate = (new GlideDateTime(latestStartDate).getLocalDate().getDisplayValue());
          n.oldEndDate = taskEndDate.getDisplayValue();
          // n.newEndDate = shadowTaskEndDate.getDisplayValue();
          n.newEndDate = (new GlideDateTime(latestEndDate).getLocalDate().getDisplayValue());
      }
      formattedNotifications.push(n);
  }
  return formattedNotifications;
};

PlannedTaskNotificationService.getAllNotificationsForTask = function(taskId) {
  var notifications = [];
  var gr = new GlideRecord('planned_task_notification');
  gr.addQuery('task', taskId);
  gr.query();
  while ( gr.next() ) {
      var _tempGr = new GlideRecord('planned_task_notification');
      _tempGr.get(gr.getValue('sys_id'));
      notifications.push(new PlannedTaskNotification(_tempGr));
  }
  return notifications;
};

PlannedTaskNotificationService.getNewNotificationsForTask = function(taskId) {
  var notifications = [];
  var gr = new GlideRecord('planned_task_notification');
  gr.addQuery('task', taskId);
  gr.addQuery('state', 'new');
  gr.query();
  while ( gr.next() ) {
      var _tempGr = new GlideRecord('planned_task_notification');
      _tempGr.get(gr.getValue('sys_id'));
      notifications.push(new PlannedTaskNotification(_tempGr));
  }
  return notifications;   
};

PlannedTaskNotificationService.markAllHardNotificationsAsRead = function(projectId) {
  var ptn = new PlannedTaskNotification();    
  ptn.markAllHardNotificationsAsProcessed(projectId);
};

PlannedTaskNotificationService.getArchivedNotifications = function(projectId) {
  var formattedNotifications = [];
  var ptn = new PlannedTaskNotification();    
  var notifications = ptn.getProcessedNotificationsForProject(projectId);
  while ( notifications.next() ) {
      var n = {};
      n.id = notifications.getValue('sys_id');
      n.status = notifications.getDisplayValue('status');
      n.externalTask = {project: {}};
      n.impactedTask = {};
      
      var sourceRec = notifications.source.getRefRecord();
      var sourceTopTaskRec = notifications.task.top_task.getRefRecord();
      n.externalTask.short_description = sourceRec.getValue('short_description');
      n.externalTask.number = sourceRec.getValue('number');
      n.externalTask.sys_class_name = sourceRec.getValue('sys_class_name');
      n.externalTask.sys_id = sourceRec.getUniqueValue();
      n.externalTask.project.short_description = sourceTopTaskRec.getValue('short_description');
      n.externalTask.project.number = sourceTopTaskRec.getValue('number');
      n.externalTask.project.sys_class_name = sourceTopTaskRec.getValue('sys_class_name');
      n.externalTask.project.sys_id = sourceTopTaskRec.getUniqueValue();

      var taskRec = notifications.task.getRefRecord();
      var shadowTaskRec = notifications.shadow_task.getRefRecord();
      n.impactedTask.short_description = taskRec.getValue('short_description');
      n.impactedTask.wbs = taskRec.getValue('wbs');
      n.impactedTask.number = taskRec.getValue('number');
      n.impactedTask.start_date = notifications.getDisplayValue("start_date");
      formattedNotifications.push(n);
  }
  return formattedNotifications;
};

Sys ID

1ef295960b322200acc30e7363673a44

Offical Documentation

Official Docs: