Name

sn_chg_score.ChangeSuccessSNC

Description

SNC Code for the ChangeSuccessSNC class. Provides methods for the querying of data related to the success of Change Requests.

Script

var ChangeSuccessSNC = Class.create();

ChangeSuccessSNC.prototype = {
  initialize: function() {
      this._withTeamScore = true;
      this._withModelScore = true;
      this._withTypeScore = true;
      this._withSecurity = true;
      this._withRatings = false;
      this._withIndicatorScores = false;

      this._changeSuccessScoreUtils = new sn_chg_score.ChangeSuccessScoreUtils();
      this._changeSuccessScoreRating = new sn_chg_score.ChangeSuccessScoreRating();
  },

  withTeamScore: function(trueOrFalse) {
      this._withTeamScore = this._isTrue(trueOrFalse);

      return this;
  },

  withTypeScore: function(trueOrFalse) {
      this._withTypeScore = this._isTrue(trueOrFalse);

      return this;
  },

  withModelScore: function(trueOrFalse) {
      this._withModelScore = this._isTrue(trueOrFalse);

      return this;
  },

  withSecurity: function(trueOrFalse) {
      this._withSecurity = this._isTrue(trueOrFalse);

      return this;
  },

  withTeamScoreRatings: function(trueOrFalse) {
      this._withRatings = this._isTrue(trueOrFalse);

      return this;
  },

  withIndicatorScores: function(trueOrFalse) {
      this._withIndicatorScores = this._isTrue(trueOrFalse);

      return this;
  },

  getScoresForChange: function(changeGr) {
      var scoreData = {};
      if (this._withTeamScore)
          scoreData.team_success_score = this.getTeamScoreForChange(changeGr);

      if (this._withModelScore)
          scoreData.model_success_score = this.getModelScoreForChange(changeGr);

      if (this._withTypeScore)
          scoreData.type_success_score = this.getTypeScoreForChange(changeGr);

      return scoreData;
  },

  getTeamScoreForChange: function(changeGr) {
  	return this.getTeamScoreForGroupId(this._getGroupIdFromChange(changeGr), this._getDomainFromChange(changeGr));
  },

  getTeamScoreForGroupId: function(groupId, domainId) {
      var teamScoreData = {
          "can_read": false
      };

      if (!groupId)
          return teamScoreData;

      var groupGr = this._getGroup(groupId);
      if (groupGr === null)
          return teamScoreData;

      teamScoreData.can_read = true;
      teamScoreData.sys_user_group = global.ChangeCommon.toJS(groupGr);
      teamScoreData.score = this._getBreakdownScore(this._changeSuccessScoreUtils.PA_CHANGE_SUCCESS_SCORE_INDICATOR_ID,
          this._changeSuccessScoreUtils.PA_CHANGE_SCORE_GROUP_BREAKDOWN_ID,
          groupId,
  		domainId);
      // add in the Change Success Score Rating record that matches this score
      teamScoreData.chg_success_score_rating = this._changeSuccessScoreRating.getRatingForScoreJS(teamScoreData.score.value);

      if (this._withRatings)
          teamScoreData.ratings = this._changeSuccessScoreRating.getAllRatingsJS();

      if (this._withIndicatorScores)
          teamScoreData.indicator_scores = this._getPABreakdownScoresByIndicatorGroup(this._changeSuccessScoreUtils.PA_CHANGE_SCORE_INDICATOR_GROUP_ID,
              this._changeSuccessScoreUtils.PA_CHANGE_SCORE_GROUP_BREAKDOWN_ID,
              groupId,
  			new global.ChangeSuccessScoreGlobalUtils().getIndicatorScoresDomain());

      return teamScoreData;
  },

  getMinimumPermittedTeamScore: function() {
      return this._changeSuccessScoreUtils.getMinAllowedScore();
  },

  getMaximumPermittedTeamScore: function() {
      return this._changeSuccessScoreUtils.getMaxAllowedScore();
  },

  getEntryLevelTeamScore: function() {
      return this._changeSuccessScoreUtils.getEntryLevelScore();
  },

  getModelScoreForChange: function(changeGr) {
      return this.getModelScoreForModelId(this._getModelIdFromChange(changeGr), this._getDomainFromChange(changeGr));
  },

  getModelScoreForModelId: function(modelId, domainId) {
      var modelScoreData = {
          "can_read": false
      };

      if (!modelId)
          return modelScoreData;

      var modelGr = this._getChangeModel(modelId);
      if (modelGr === null)
          return modelScoreData;

      modelScoreData.can_read = true;
      modelScoreData.chg_model = global.ChangeCommon.toJS(modelGr);
      modelScoreData.score = this._getBreakdownScore(this._changeSuccessScoreUtils.PA_MODEL_SUCCESS_FORMULA_INDICATOR,
          this._changeSuccessScoreUtils.PA_MODEL_SUCCESS_BREAKDOWN_ID,
          modelId,
  		domainId);

      if (this._withIndicatorScores)
          modelScoreData.indicator_scores = this._getPABreakdownScoresByIndicatorGroup(this._changeSuccessScoreUtils.PA_MODEL_SUCCESS_AUTOMATED_INDICATOR_GROUP,
              this._changeSuccessScoreUtils.PA_MODEL_SUCCESS_BREAKDOWN_ID,
              modelId,
  			domainId);

      return modelScoreData;
  },

  getTypeScoreForChange: function(changeGr) {
      return this.getTypeScoreForType(this._getTypeFromChange(changeGr), this._getDomainFromChange(changeGr));
  },

  getTypeScoreForType: function(type, domainId) {
      var typeScoreData = {
          "can_read": false
      };

      if (!type)
          return typeScoreData;

      typeScoreData.type = this._getChangeType(type);
      if (gs.nil(typeScoreData.type.sys_id))
          return typeScoreData;

      typeScoreData.can_read = true;
      typeScoreData.score = this._getBreakdownScore(this._changeSuccessScoreUtils.PA_TYPE_SUCCESS_FORMULA_INDICATOR,
          this._changeSuccessScoreUtils.PA_TYPE_SUCCESS_BREAKDOWN_ID,
          typeScoreData.type.sys_id,
  		domainId);

      if (this._withIndicatorScores)
          typeScoreData.indicator_scores = this._getPABreakdownScoresByIndicatorGroup(this._changeSuccessScoreUtils.PA_TYPE_SUCCESS_AUTOMATED_INDICATOR_GROUP,
              this._changeSuccessScoreUtils.PA_TYPE_SUCCESS_BREAKDOWN_ID,
              typeScoreData.type.sys_id,
  			domainId);

      return typeScoreData;
  },

  _getPABreakdownScoresByIndicatorGroup: function(indicatorGroupId, breakdownId, breakdownValue, domain) {
      var indicators = [];

      if (!breakdownValue)
          return indicators;

      var paIndicatorTagGr = new GlideRecord("pa_m2m_indicator_tags");
      paIndicatorTagGr.addQuery("tag", indicatorGroupId);
      paIndicatorTagGr.orderBy("order");
      paIndicatorTagGr.query();

      while (paIndicatorTagGr.next()) {
          var scoreResult = this._getBreakdownScore(paIndicatorTagGr.getValue("indicator"),
              breakdownId,
              breakdownValue,
              domain);

          if (!scoreResult.hasOwnProperty("value"))
              continue;

          indicators.push(scoreResult);
      }

      return indicators;
  },

  _getBreakdownScore: function(indicatorId, breakdownId, breakdownValue, domain) {
      var scoreResult = this._getPABreakdownScore(indicatorId, breakdownId, breakdownValue, domain);
      if (scoreResult === null)
          return {
              hasScore: false
          };

      return {
          indicator: {
              name: scoreResult.indicator.display_value,
              sys_id: scoreResult.indicator.value
          },
          value: scoreResult.value,
          display_value: scoreResult.value_formatted,
          delta: {
              value: gs.nil(scoreResult.change) ? "0" : scoreResult.change,
              display_value: scoreResult.change_formatted
          },
          has_score: !global.ChangeCommon.isNil(scoreResult.value)
      };
  },

  _getPABreakdownScore: function(indicatorId, breakdownId, breakdownValue, domain) {
      if (!indicatorId || !breakdownId || !breakdownValue)
          return null;

      var scorecard = new PAScorecard();
      scorecard.addParam("sysparm_display", "all");
      scorecard.addParam("uuid", indicatorId + ":" + breakdownId + ":" + breakdownValue + (domain ? ";" + domain : ""));

      var scoreResults = scorecard.query();
      if (!scoreResults || scoreResults.length === 0)
          return null;

      return scoreResults[0];
  },

  _getGroupIdFromChange: function(changeGr) {
      return this._getFieldValueFromChange(changeGr, "assignment_group");
  },

  _getModelIdFromChange: function(changeGr) {
      return this._getFieldValueFromChange(changeGr, "chg_model");
  },

  _getTypeFromChange: function(changeGr) {
      return this._getFieldValueFromChange(changeGr, "type");
  },

  _getDomainFromChange: function(changeGr) {
      var domain = this._getFieldValueFromChange(changeGr, "sys_domain");
  	
  	return gs.nil(domain) ? "global" : domain;
  },

  _getFieldValueFromChange: function(changeGr, fieldName) {
      if (!changeGr || !fieldName || typeof changeGr.isValidField !== "function" || !changeGr.isValidField(fieldName))
          return null;

      return changeGr.getValue(fieldName);
  },

  _getGroup: function(groupId) {
      var groupGr = new GlideRecord("sys_user_group");
      if (!groupGr.get(groupId))
          return null;

      if (!this._withSecurity)
          return groupGr;

      return groupGr.canRead() ? groupGr : null;
  },

  _getChangeModel: function(modelId) {
      var modelGr = new GlideRecord("chg_model");
      if (!modelGr.get(modelId))
          return null;

      if (!this._withSecurity)
          return modelGr;

      return modelGr.canRead() ? modelGr : null;
  },

  _getChangeType: function(type) {
      if (!type)
          return {};

      var sysChoiceGr = new GlideRecord("sys_choice");
      sysChoiceGr.addQuery("name", "change_request");
      sysChoiceGr.addQuery("element", "type");
      sysChoiceGr.addQuery("language", "en");
      sysChoiceGr.addQuery("value", type);
      sysChoiceGr.query();

      if (!sysChoiceGr.next())
          return {};

      var choice = null;
      var choiceList = new GlideChoiceList.getChoiceList("change_request", "type");
      if (choiceList.indexOf(type) >= 0)
          choice = choiceList.getChoice(type);

      return {
          sys_id: sysChoiceGr.getUniqueValue(),
          value: sysChoiceGr.getValue("value"),
          display_value: choice !== null ? choice.getLabel() : sysChoiceGr.getLabel()
      };
  },

  _isTrue: function(trueOrFalse) {
  	return gs.nil(trueOrFalse) ? true : this._changeSuccessScoreUtils.isTrue(trueOrFalse);
  },

  type: 'ChangeSuccessSNC'
};

Sys ID

5ea162fc00528510f877dd41b70c8e90

Offical Documentation

Official Docs: