Name

global.PlannedTaskStateChangeHandler

Description

No description available

Script

var PlannedTaskStateChangeHandler = Class.create();

PlannedTaskStateChangeHandler.processStateChange = function(current, previous, origSysId) {
  PPMDebug.log("Into handleStateChange()");
  // get state attributes for this table
  var plannedTaskStateUtil = new PlannedTaskStateUtil(current);
  var stateBucket = plannedTaskStateUtil.getBucketForState(current.state);
  var isTopTask = current.top_task == current.sys_id;
  var recalculationHelper = new RecalculationHelper();
  var projectAction = current.top_task == origSysId;
  var skippedStates = new PlannedTaskSkippedStates(current);
  var currentState = current.getValue("state");
  var shouldUpdatePercentComplete = skippedStates.shouldUpdatePercentComplete(currentState);
  var isSubProjectAction = current.sys_id != current.top_task && current.instanceOf("pm_project") && current.sys_id == origSysId;
  if (isTopTask || isSubProjectAction) {
      if (!current.isNewRecord() && current.active.changesTo(true) && (projectAction || isSubProjectAction) && !previous.active) {
          PPMDebug.log("Task: " + current.short_description + " Re-opened, Clearing the Actuals and Reactivate the Children");
          current.work_start = "";
          current.work_end = "";
          current.work_duration = "";
          current.percent_complete = 0;
          recalculationHelper.process(current, previous); // recalculate here
          PlannedTaskUtilsV2.reactivateChildren(current, previous.state);
      }
      PPMDebug.log("stateBucket -> " + stateBucket + " | projectAction -> " + projectAction + " | isSubProjectAction " + isSubProjectAction);
      if (stateBucket == PlannedTaskStateUtil.WORK_IN_PROGRESS) {
          if (!(projectAction || isSubProjectAction))
              return;
          if (JSUtil.nil(current.work_start))
              current.work_start = current.start_date;
          recalculationHelper.process(current, previous); // recalculate here
          PlannedTaskUtilsV2.startLeaves(current, PlannedTaskStateUtil.WORK_IN_PROGRESS);
          if (isSubProjectAction) {
              PlannedTaskStateChangeHandler.updateRootWorkStartDate(current);
              PlannedTaskUtilsV2.setParentState(current);
          }
      } else if ((projectAction || isSubProjectAction) && stateBucket == PlannedTaskStateUtil.OPEN_STATES) {
          PPMDebug.log("Set Project to Open");
          PlannedTaskUtilsV2.openProject(current);
          if (isSubProjectAction) {
              recalculationHelper.process(current, previous);
              PlannedTaskUtilsV2.setParentState(current);
          }
      } else {
          if (current.active.changesTo(false)) {
              PPMDebug.log("Task has been closed " + current.short_description);
              if (projectAction || isSubProjectAction) {
                  if (shouldUpdatePercentComplete)
                      current.percent_complete = 100;
                  PPMDebug.log("Closing the Children with Current State: " + current.state);
                  // we populate the dates using PTA
  				if(current.rollup)
  					PlannedTaskUtilsV2.closeChildren(current, current.state, shouldUpdatePercentComplete);
  				else
  					recalculationHelper.process(current); // recalculate here
              }
              if (!projectAction && ((origSysId == current.sys_id) || current.rollup)) {
                  if (!current.rollup && shouldUpdatePercentComplete)
                      current.percent_complete = 100;
                  PPMDebug.log("Starting the Successors " + current.short_description);
                  // start successor tasks
  				if(!isSubProjectAction)
  					recalculationHelper.process(current, previous); // recalculate_here
                  PlannedTaskUtilsV2.startSuccessors(current, PlannedTaskStateUtil.OPEN_STATES);
                  PPMDebug.log("Setting the Parent State " + current.short_description);
                  // check parent for close dependencies
                  PlannedTaskUtilsV2.setParentState(current);
              }
          } else {
  			if (isSubProjectAction)
                  PlannedTaskUtilsV2.setParentState(current);
              // task has been closed and set to inactive
  			var isProjOrSubProjAction = projectAction || isSubProjectAction;
              if (!isProjOrSubProjAction || (isProjOrSubProjAction && !previous.active))
                  return;
              PPMDebug.log("Setting the Children State to Current State: " + current.state);
              current.percent_complete = 0;
              //recalculationHelper.process(current); // recalculate here
              PlannedTaskUtilsV2.setChildrenState(current, stateBucket, current.state);
              // Reset the Parent - to trigger the recalculation
              current.work_start = "";
              current.work_end = "";
              current.work_duration = "";        
          }
      }
  } else {
      PPMDebug.log("Task is not Top Task");
      if (stateBucket == PlannedTaskStateUtil.WORK_IN_PROGRESS) {
          PPMDebug.log("stateBucket -> WORK_IN_PROGRESS");
          if (origSysId == current.sys_id) {
              if (JSUtil.nil(current.work_start))
                  current.work_start = current.start_date;
              PlannedTaskStateChangeHandler.updateRootWorkStartDate(current);
          }
          if ((!current.parent.nil() && current.parent != origSysId) ||
              (!current.parent.nil() && current.parent.changes())) {
              PPMDebug.log("Starting Parent with Work State");
              current.work_end = "";
              recalculationHelper.process(current, previous); // recalculate here
              PlannedTaskUtilsV2.startParent(current, PlannedTaskStateUtil.WORK_IN_PROGRESS);
          }
      } else {
          if (current.active.changesTo(false) || stateBucket == PlannedTaskStateUtil.CLOSE_STATES) { // current task moved to closed state
              PPMDebug.log("Task has been closed " + current.short_description);
              if (!projectAction && ((origSysId == current.sys_id) || current.rollup)) {
                  if (!current.rollup && shouldUpdatePercentComplete)
                      current.percent_complete = 100;
                  PPMDebug.log("Starting the Successors " + current.short_description);
                  // start successor tasks
                  recalculationHelper.process(current, previous); // recalculate_here
                  PlannedTaskUtilsV2.startSuccessors(current, PlannedTaskStateUtil.OPEN_STATES);
                  PPMDebug.log("Setting the Parent State " + current.short_description);
                  // check parent for close dependencies
                  PlannedTaskUtilsV2.setParentState(current);
              }
          } else {
              if ((!projectAction && !current.parent.nil() && current.parent != origSysId) ||
                  (!current.parent.nil() && current.parent.changes())) {
                  if (current.getValue("rollup") == "true") {
                      current.work_start = "";
                      current.work_end = "";
                      current.work_duration = "";
                      recalculationHelper.process(current, previous); // recalculate here
                  }
                  PlannedTaskUtilsV2.setParentState(current);
              }
          }
      }
  }
  if (current.parent != previous.parent && JSUtil.notNil(previous.parent)) {
      PPMDebug.log('Parent has changed for task ' + previous.short_description);
      PlannedTaskUtilsV2.setParentState(previous, true);
  }
};

//Updates root node work_start if needed
PlannedTaskStateChangeHandler.updateRootWorkStartDate = function(current) {
  var root = new GlideRecord("planned_task");
  root.get(current.top_task);
  if (current.work_start < root.work_start) {
      root.work_start = current.work_start;
      root.setWorkflow(false);
      root.update();
  }
};

Sys ID

68084cd793321200ea933007f67ffb7c

Offical Documentation

Official Docs: