Name

sn_sow_chg.SowChangeCardsSNC

Description

No description available

Script

var SowChangeCardsSNC = Class.create();

SowChangeCardsSNC.DEFAULT = gs.getMessage("Default");
SowChangeCardsSNC.EMERGENCY = gs.getMessage("Break/fix");
SowChangeCardsSNC.OOB = gs.getMessage("Out-of-the-box");
SowChangeCardsSNC.LEGACY = gs.getMessage("Legacy");
SowChangeCardsSNC.CUSTOM = gs.getMessage("Custom");
SowChangeCardsSNC.PREAPPROVED = gs.getMessage("Pre-approved");
SowChangeCardsSNC.UNPIN = gs.getMessage("Unpin this change");
SowChangeCardsSNC.PIN = gs.getMessage("Pin this change");
SowChangeCardsSNC.SUCCESS = gs.getMessage("Success rate");
SowChangeCardsSNC.REQUIRED_FIELDS = {'sys_id': true, 'sys_class_name': true, 'display_value': true, 'target_field': true, 'value': true, 'name': true, 'default_change_model': true, 'sys_created_by': true, 'table_name': true, 'description': true, 'short_description': true, 'current_version': true, 'closed_change_count': true, 'percent_successful': true};

SowChangeCardsSNC.prototype = Object.extendsObject(sn_chg_model_ui.ChgLandingBase, {
  initialize: function(_gs) {
  	sn_chg_model_ui.ChgLandingBase.prototype.initialize.call(this, null, _gs);
  	this._isStdChgCatalogActive = global.ChangeCommon.isPluginActive("com.snc.change_management.standard_change_catalog");
  	this._isSuccessScoreActive = GlidePluginManager.isActive("com.snc.change_management.change_success_score");
  	this.currentModels = ['Standard'];
  },

  getChangeCards: function(startAt, limit, filter, orderBy, textSearch, restrict) {
  	if (!orderBy)
  		orderBy = sn_chg_model_ui.ChgLandingBase.NAME;

  	if (!limit || isNaN(limit))
  		limit = sn_chg_model_ui.ChgLandingBase.LANDING_LIMIT;

  	if (!filter && filter !== sn_chg_model_ui.ChgLandingBase.ALL && filter !== sn_chg_model_ui.ChgLandingBase.PREAPPROVED && filter !== sn_chg_model_ui.ChgLandingBase.PINNED) {
  		var filterUserPref = gs.getUser().getPreference("sn_chg_model_sow.landing_page.chg_model_filter");
  		filter = filterUserPref ? filterUserPref : sn_chg_model_ui.ChgLandingBase.ALL;
  	}

  	if (!startAt || isNaN(startAt))
  		startAt = 0;

  	var gr = null;
  	var models = this._getDefaultModel();
  	var chgModels;
  	var stdChgModels = [];
  	var chgTypes = [];
  	var allCards = [];

  	if (filter === sn_chg_model_ui.ChgLandingBase.PREAPPROVED && this._isStdChgCatalogActive) {
  		var stdCards = this._getModel(global.StandardChangeTemplate.findAll(orderBy, textSearch, "available_in_ui=true"), startAt, limit);
  		allCards = stdCards[sn_chg_model_ui.ChgLandingBase.CHG_MODEL];
  	} else if (filter === sn_chg_model_ui.ChgLandingBase.ALL || filter === sn_chg_model_ui.ChgLandingBase.PINNED) {
  		var encodedQuery = "available_in_ui=true";
  		if (filter === sn_chg_model_ui.ChgLandingBase.PINNED)
  			encodedQuery = this._getPinnedEncodedQuery();

  		//For rescricted pinned cards, do not fetch all available cards
  		if (!restrict || restrict === "") {
  			chgModels = this._getModel(this._getChangeModelsGr(orderBy, textSearch, encodedQuery));
  			if (gs.getProperty('com.snc.change_management.change_model.type_compatibility', false) === 'true') {
  				if (filter !== sn_chg_model_ui.ChgLandingBase.PINNED)
  					encodedQuery = "";
  				chgTypes = this._getModel(this._getChangeTypesGr(orderBy, textSearch, encodedQuery));
  				allCards = chgModels[sn_chg_model_ui.ChgLandingBase.CHG_MODEL].concat(chgTypes[sn_chg_model_ui.ChgLandingBase.CHG_MODEL]);
  			} else 
  				allCards = chgModels[sn_chg_model_ui.ChgLandingBase.CHG_MODEL];
  		}
  		if (this._isStdChgCatalogActive) {
  			stdChgModels = this._getModel(global.StandardChangeTemplate.findAll(orderBy, textSearch, encodedQuery));
  			allCards = allCards.concat(stdChgModels[sn_chg_model_ui.ChgLandingBase.CHG_MODEL]);
  		}
  	}

  	models[sn_chg_model_ui.ChgLandingBase.CHG_MODEL] = this._formatCards(allCards.slice(startAt, (startAt + limit)));
  	models.__totalRecords = allCards.length;
  	models.__pinned = this._getPinnedArray().join();
  	return models;
  },

  _getDefaultModel: function() {
  	var model = {
  		__totalRecords: 0,
  		__pinned: [],
  		__pinLabels: {"pin": SowChangeCardsSNC.PIN, "unpin": SowChangeCardsSNC.UNPIN}
  	};
  	model[sn_chg_model_ui.ChgLandingBase.CHG_MODEL] = [];
  	return model;
  },

  _getModel: function(gr) {
  	var model = this._getDefaultModel();

  	if (!gr)
  		return model;

  	model.__rowCount = gr.getRowCount();

  	while (gr.next()) {
  		model[sn_chg_model_ui.ChgLandingBase.CHG_MODEL].push(this._toJS(gr, SowChangeCardsSNC.REQUIRED_FIELDS));
  		this.currentModels.push(gr.getValue(sn_chg_model_ui.ChgLandingBase.NAME));
  	}

  	return model;
  },

  _getChangeModelsGr: function(orderBy, textSearch, encodedQuery) {
  	var changeModelsGr = new GlideRecordSecure(sn_chg_model_ui.ChgLandingBase.CHG_MODEL);
  	if (!orderBy)
  		orderBy = sn_chg_model_ui.ChgLandingBase.NAME;

  	changeModelsGr.addActiveQuery();
  	if (textSearch && typeof textSearch !== "undefined" && textSearch.trim() !== "")
  		changeModelsGr.addQuery("name", "CONTAINS", textSearch).addOrCondition("description", "CONTAINS", textSearch);

  	if (encodedQuery)
  		changeModelsGr.addEncodedQuery(encodedQuery);

  	changeModelsGr.orderBy(orderBy);
  	changeModelsGr.query();
  	return changeModelsGr;
  },
  
  _getChangeTypesGr: function(orderBy, textSearch, encodedQuery) {
  
  	var changeTypesGr = new GlideRecordSecure("aw_record_type_selector");
  	if (!orderBy)
  		orderBy = sn_chg_model_ui.ChgLandingBase.NAME;

  	changeTypesGr.addActiveQuery();
  	if (textSearch && typeof textSearch !== "undefined" && textSearch.trim() !== "")
  		changeTypesGr.addQuery("name", "CONTAINS", textSearch).addOrCondition("description", "CONTAINS", textSearch);

  	if (encodedQuery)
  		changeTypesGr.addEncodedQuery(encodedQuery);

  	changeTypesGr.orderBy(orderBy);
  	changeTypesGr.addQuery("target_table", "change_request");
  	changeTypesGr.addQuery("display_value", "NOT IN", this.currentModels);
  	changeTypesGr.query();
  	return changeTypesGr;
  },

  _getDefaultStruct: function() {
  	return {
  		sysIds: [],
  		__more: false,
  		limit: parseInt(sn_chg_model_ui.ChgLandingBase.LANDING_LIMIT),
  		recordCount: 0,
  		maxLimit: parseInt(sn_chg_model_ui.ChgLandingBase.MAX_CHG_MODELS),
  		offset: 0
  	};
  },

  _getPinnedArray: function() {
  	var pinnedUserPref = gs.getUser().getPreference("sn_chg_model_ui.landing_page.pinned_change_models");
  	var pinnedSysIds = pinnedUserPref ? pinnedUserPref.split(",") : [];
  	return pinnedSysIds;
  },

  _getPinnedEncodedQuery: function() {
  	return "sys_idIN" + this._getPinnedArray();
  },

  _getChgSuccessRate: function(chgCard) {
  	var successRate = "-";
  	if (!chgCard)
  		return successRate;
  	
  	if (chgCard.sys_class_name.value === "std_change_record_producer") {
  		if (!chgCard.closed_change_count)
  			return successRate;

  		var closedChangeCount = parseInt(chgCard.closed_change_count.value || 0);
  		if (!closedChangeCount)
  			return successRate;

  		var percentSuccessful = parseFloat(chgCard.percent_successful.value);
  		if (isNaN(percentSuccessful))
  			return successRate;

  		successRate = Math.round(percentSuccessful) + "%";

  		return successRate;
  	} else {
  		if (this._isSuccessScoreActive) {
  			var scoreData = new sn_chg_score.ChangeSuccess().getModelScoreForModelId(chgCard.sys_id.value);
  			var hasScore = scoreData && scoreData.score && scoreData.score.value;
  			
  			if (hasScore)
  				successRate = Math.round(scoreData.score.value) + "%";
  		}	
  		return successRate;
  	}
  },

  _formatCards: function(cards) {
  	var stateModels = [];
  	var pinnedArray = this._getPinnedArray();

  	cards.forEach(function(card) {
  		var model = {};
  		model.sysId = card.sys_id.value;
  		model.sys_class_name = card.sys_class_name.value;
  		if (card.sys_class_name.value === "aw_record_type_selector") {
  			model.name = card.display_value.display_value;
  			model.description = card.short_description.display_value;
  			model.qry = card.target_field.value + '=' + card.value.value;
  		} else {
  			model.name = card.name.display_value;
  			model.description = card.hasOwnProperty("short_description") ? card.short_description.display_value : card.description.display_value;
  		}
  		model.tagLabel = "‎‎ ";
  		model.tagIcon = null;
  		model.icon = "";
  		if (card.sys_class_name.value === "chg_model") {
  			if (card.default_change_model.value + '' === 'true') {
  				model.sidebar = {
  					"color": "positive",
  					"variant": "secondary"
  				};
  				model.color = "positive";
  				model.label = SowChangeCardsSNC.DEFAULT;
  			} else if (model.name === 'Emergency') {
  				model.sidebar = {
  					"color": "critical",
  					"variant": "secondary"
  				};
  				model.color = "critical";
  				model.label = SowChangeCardsSNC.EMERGENCY;
  			} else if (card.sys_created_by.display_value === 'admin' || card.sys_created_by.display_value === 'Change.Manager') {
  				model.sidebar = {
  					"color": "info",
  					"variant": "secondary"
  				};
  				model.color = "info";
  				model.label = SowChangeCardsSNC.OOB;
  			} else {
  				model.sidebar = {
  					"color": "pink",
  					"variant": "secondary"
  				};
  				model.color = "pink";
  				model.label = SowChangeCardsSNC.CUSTOM;
  			}
  			model.successScore = (this._isSuccessScoreActive ? this._getChgSuccessRate(card) : "");
  			model.successLabel = (this._isSuccessScoreActive ? SowChangeCardsSNC.SUCCESS : "");
  			model.targetTableName = card.table_name.value;
  		} else if (card.sys_class_name.value === "std_change_record_producer") {
  			model.versionId = card.current_version.value;
  			model.sidebar = {
  				"color": "purple",
  				"variant": "secondary"
  			};
  			model.color = "purple";
  			model.label = SowChangeCardsSNC.PREAPPROVED;
  			
  			model.successScore = this._getChgSuccessRate(card);
  			model.successLabel = SowChangeCardsSNC.SUCCESS;
  		}  else if (card.sys_class_name.value === "aw_record_type_selector") {
  			model.sidebar = {
  				"color": "teal",
  				"variant": "secondary"
  			};
  			model.color = "teal";
  			model.label = SowChangeCardsSNC.LEGACY;
  			
  			model.successScore = "";
  			model.successLabel = "";
  		}

  		if (pinnedArray.indexOf(model.sysId) >= 0) {
  			model.isPinned = true;
  			model.pinIcon = [{
  				"icon": "thumbtack-fill",
  				"label": SowChangeCardsSNC.UNPIN
  			}];
  		} else {
  			model.isPinned = false;
  			model.pinIcon = [{
  				"icon": "thumbtack-outline",
  				"label": SowChangeCardsSNC.PIN
  			}];
  		}
  		stateModels.push(model);
  	}.bind(this));

  	return stateModels;
  },

  type: 'SowChangeCardsSNC'
});

Sys ID

9decd08b23f33010b8f7dc1756bf65da

Offical Documentation

Official Docs: