Name

sn_gf.GoalRelationshipHelper

Description

No description available

Script

var GoalRelationshipHelper = Class.create();

GoalRelationshipHelper.insertOrUpdateGoalRelationshipWhenWorkItemIsUpdated = function(previous, current) {
  var previousPrimaryGoalId = previous.getValue('primary_goal');
  var currentPrimaryGoalId = current.getValue('primary_goal');
  var tableName = sn_gf.GFGoalRelationshipSNC.getWorkItemTableName(current);
  var workItemId = current.getUniqueValue();
  
  var primaryGoalIsCleared = (currentPrimaryGoalId == null);
  if (primaryGoalIsCleared) {
      (new sn_gf.GFGoalRelationship(previousPrimaryGoalId, tableName, workItemId)).deleteGoalRelationship();
  } else { //Primary goal is updated with the other Goal
      var previousGoalRelationship = new sn_gf.GFGoalRelationship(previousPrimaryGoalId, tableName, workItemId);
      //isWorkItemPrimaryGoalUpdatedByUser is TRUE, when User directly updates the Primary Goal column on Work Item
      //isWorkItemPrimaryGoalUpdatedByUser is FALSE, when an existing/new Goal Relationship is made Primary Goal, then WorkItem's Primary Goal will be updated (INDIRECTLY, through another BR on Goal Relationship) after is_primary_goal of Previous Goal Relationship is made false.
      var isWorkItemPrimaryGoalUpdatedByUser = previousGoalRelationship.isPrimaryGoalChecked();
      if (previousPrimaryGoalId == null || isWorkItemPrimaryGoalUpdatedByUser) {
          var currentGoalRelationship = new sn_gf.GFGoalRelationship(currentPrimaryGoalId, tableName, workItemId);
          var workItemGoalRelationshipExists = currentGoalRelationship.goalRelationshipId != '';
          if (workItemGoalRelationshipExists) {
              currentGoalRelationship.makeGoalRelationshipPrimary();
          } else {
              if (!sn_gf.GFGoalRelationshipSNC.checkGoalRelationshipRec(currentPrimaryGoalId, tableName, workItemId, true))
                  currentGoalRelationship.createGoalRelationship(currentPrimaryGoalId, tableName, workItemId, true);
          }
          //need to initialize again to get latest data on Goal rel record
          var previousGoalRelationshipRec = new sn_gf.GFGoalRelationship(previousPrimaryGoalId, tableName, workItemId);
          previousGoalRelationshipRec.deleteGoalRelationship();
      }
  }
};

Sys ID

008e5f2577023010a55229354f5a9938

Offical Documentation

Official Docs: