Name

sn_chg_score.ChangeSuccessScoreRatingSNC

Description

Provides functions that are used when creating or updating Change Success Score Rating records. To modify behavior, use the provided ChangeSuccessScoreRating extension to override functions defined within this script include.

Script

var ChangeSuccessScoreRatingSNC = Class.create();
ChangeSuccessScoreRatingSNC.prototype = {
  initialize: function(ratingGr) {
      this.ratingGr = null;
      if (ratingGr && ratingGr.getUniqueValue())
          this.ratingGr = ratingGr;

      this.changeSuccessScoreUtils = new sn_chg_score.ChangeSuccessScoreUtils();

      this.NO_RATING_FOUND = {
          rating: {
              value: "",
              displayValue: gs.getMessage("No rating found")
          },
          color: {
              value: "#E6E8EA",
              displayValue: "#E6E8EA"
          }
      };
  },

  getRatingForScoreJS: function(score, returnNoRatingFound) {
      var rating = {};
      returnNoRatingFound = this.changeSuccessScoreUtils.isTrue(returnNoRatingFound);

      if (typeof score !== 'number' || isNaN(score))
          return returnNoRatingFound ? this.NO_RATING_FOUND : rating;

      var ratingGr = this.getRatingForScore(score);
      if (ratingGr === null)
          return this.NO_RATING_FOUND;

      return global.ChangeCommon.toJS(ratingGr);
  },

  getAllRatingsJS: function() {
      var ratings = [];
      var ratingsGr = this.getRatings();
      if (!ratingsGr || !ratingsGr.hasNext())
          return ratings;

      while (ratingsGr.next()) {
          var ratingObj = global.ChangeCommon.toJS(ratingsGr);
          if (!global.JSUtil.isEmpty(ratingObj))
              ratings.push(ratingObj);
      }

      return ratings;
  },

  getRatingForScore: function(score) {
      if (typeof score !== 'number' || isNaN(score))
          return null;

      var ratingGr = this.getRatings(score);
      if (!ratingGr || !ratingGr.isValidRecord())
          return null;

      return ratingGr;
  },

  getRatings: function(score) {
      if (!gs.nil(score) && (typeof score !== 'number' || isNaN(score)))
          return null;

      var ratingGr = new GlideRecord(this.changeSuccessScoreUtils.CHG_SUCCESS_SCORE_RATING);
      ratingGr.addActiveQuery();

      if (typeof score === 'number' && !isNaN(score)) {
          ratingGr.addQuery("score_start", "<=", score);
          ratingGr.addQuery("score_end", ">=", score);
      }

      ratingGr.orderByDesc("score_start");
      ratingGr.query();

      if (!ratingGr.hasNext())
          return null;

      if (!gs.nil(score)) {
          while (ratingGr.next())
              if (score <= ratingGr.getValue("score_end"))
                  return ratingGr;

          return null;
      } else
          return ratingGr;
  },

  validate: function() {
      var validationErrors = this._validateScoreRange();
      if (Array.isArray(validationErrors) && validationErrors.length > 0)
          return validationErrors;

      validationErrors = this._checkForConflicts();
      if (Array.isArray(validationErrors) && validationErrors.length > 0)
          return validationErrors;
  },

  _validateScoreRange: function() {
      var validationErrors = [];

      if (this.ratingGr === null)
          return validationErrors;

      var scoreStart = parseInt(this.ratingGr.getValue("score_start"), 10);
      var scoreEnd = parseInt(this.ratingGr.getValue("score_end"), 10);

      if (isNaN(scoreStart))
          validationErrors.push(gs.getMessage("Score start must be a number"));
      if (isNaN(scoreEnd))
          validationErrors.push(gs.getMessage("Score end must be a number"));

      if (validationErrors.length > 0)
          return validationErrors;

      var minAllowedScore = this.changeSuccessScoreUtils.getMinAllowedScore();
      var maxAllowedScore = this.changeSuccessScoreUtils.getMaxAllowedScore();

      if (scoreStart < minAllowedScore || scoreStart > maxAllowedScore)
          validationErrors.push(gs.getMessage("Score start must be between {0} and {1}", ["" + Math.floor(minAllowedScore), "" + Math.floor(maxAllowedScore)]));
      if (scoreEnd < minAllowedScore || scoreEnd > maxAllowedScore)
          validationErrors.push(gs.getMessage("Score end must be between {0} and {1}", ["" + Math.floor(minAllowedScore), "" + Math.floor(maxAllowedScore)]));
      if (scoreStart > scoreEnd)
          validationErrors.push(gs.getMessage("Score start cannot be greater than score end"));

      return validationErrors;
  },

  _checkForConflicts: function() {
      var conflictErrors = [];

      if (this.ratingGr === null)
          return conflictErrors;

      var ratingGr = new GlideRecord(this.changeSuccessScoreUtils.CHG_SUCCESS_SCORE_RATING);
      ratingGr.addActiveQuery();
      ratingGr.addQuery("sys_id", "!=", this.ratingGr.getValue("sys_id"));
      var qc = ratingGr.addQuery("score_start", "");

      var startOnly = qc.addOrCondition("score_start", ">=", this.ratingGr.getValue("score_start"));
      startOnly.addCondition("score_start", "<=", this.ratingGr.getValue("score_end"));

      var endOnly = qc.addOrCondition("score_end", ">=", this.ratingGr.getValue("score_start"));
      endOnly.addCondition("score_end", "<=", this.ratingGr.getValue("score_end"));

      var startAndEnd = qc.addOrCondition("score_end", ">=", this.ratingGr.getValue("score_end"));
      startAndEnd.addCondition("score_start", "<=", this.ratingGr.getValue("score_start"));

      ratingGr.orderBy("score_start");
      ratingGr.query();

      while (ratingGr.next())
          conflictErrors.push(gs.getMessage("You cannot use these score start and/or end values as they conflict with score rating \"{0}\" which has a score range of {1} to {2}",
              [ratingGr.getDisplayValue(), ratingGr.getValue("score_start"), ratingGr.getValue("score_end")]));

      return conflictErrors;
  },

  type: 'ChangeSuccessScoreRatingSNC'
};

Sys ID

c124c12b73001010491d235f04f6a788

Offical Documentation

Official Docs: