Name

global.ShadowTaskSync

Description

Syncs the task with its shadow in other projects

Script

ShadowTaskSync = Class.create();

ShadowTaskSync.prototype = {
  initialize: function() {
      this.syncColumns = gs.getProperty('com.snc.project_management.sync_shadow_task_columns',
      'short_description,percent_complete,state,start_date,end_date,duration,work_start,work_end,work_duration,schedule_start_date,schedule_end_date,state,time_constraint,active,status');
      this.syncColumnsList = this.syncColumns.split(',');
      this.syncSuccColumnsList = this.syncColumns.split(',');
  },
  
  process: function(topTaskId, jsonData) {
      PPMDebug.log("Into ShadowTaskSync: process -> " + topTaskId + " | jsonData -> " + jsonData);
      if(JSUtil.notNil(jsonData)) {
          var jsonDataObj = (new JSON()).decode(jsonData);
          var tasks = jsonDataObj.tasks;
          if ( JSUtil.nil(tasks) )
              return; 
          // validate if ORIG_SYS_ID Exists
          if(gs.fieldExists('planned_task', 'orig_sys_id'))
              this.syncTasksToShadows(tasks);
      }
  },
      
  syncTasksToShadows: function (tasks) {
      PPMDebug.log("Into ShadowTaskSync: syncTasksToShadows -> " + JSON.stringify(tasks));
      var filterNonShadowTask = ShadowTaskQueryHelper.filterForNonShadowTask();
      var filterTaskHavingShadows = ShadowTaskQueryHelper.filterForTaskHavingShadows();       
      var tasksToSync = tasks.filter(filterNonShadowTask).filter(filterTaskHavingShadows);        
      var self = this;
      tasksToSync.forEach(function(task) {
          PPMDebug.log("Into ShadowTaskSync: syncingTask -> " + JSON.stringify(task));
          var taskId = task.id || task.sys_id;
          self.syncTaskToShadows(taskId);
      });     
  },
  
  syncTaskToShadows: function(taskId) {
      PPMDebug.log("Into ShadowTaskSync: syncTaskToShadows -> " + taskId);
      var taskRecord = this.plannedTaskRecord(taskId);
      PPMDebug.log("Into ShadowTaskSync: syncTaskToShadows: taskRecord -> " + taskRecord.getValue("short_description")
          + taskRecord.getValue("start_date") + " | " + taskRecord.getValue("end_date"));
      var shadows = ShadowTaskQueryHelper.findShadowTasks(taskId);
      var syncList = [];
      var syncSuccColumns = false;
      while ( shadows.next() ) {
          if ( ShadowTaskQueryHelper.isShadowTaskSuccessor(shadows.getValue('sys_id')) ) {
              syncList = this.syncSuccColumnsList;
              syncSuccColumns = true;
          } else {
              syncList = this.syncColumnsList;
          }
          
          syncList.forEach(function(col) {
              if( JSUtil.notNil(taskRecord.getValue(col)) ) {
                  if(col === 'short_description') {
                      var topTask = taskRecord.top_task.getRefRecord();
                      var shortDescription = topTask.getValue(col) + ": " + taskRecord.getValue(col);
                      shadows.setValue(col, shortDescription);
                  } else {
                      shadows.setValue(col, taskRecord.getValue(col));
                  }
              } else {
                  shadows.setValue(col, 'NULL');
              }
          });
          PPMDebug.log("Sync to Shadow: " + shadows.getValue("short_description") +  " -> " + shadows.getValue("start_date") + 
              "| " + shadows.getValue("end_date"));
          shadows.setWorkflow(false);
          shadows.update();
      }
  },
  
  plannedTaskRecord: function(taskId) {
      var gr = new GlideRecord("planned_task");
      gr.get(taskId);
      return gr;
  },
      
  type: 'ShadowTaskSync'
};

Sys ID

23b4dbbc9f032200598a5bb0657fcf44

Offical Documentation

Official Docs: