Name

global.FixedCostAnalyticsHelper

Description

No description available

Script

var FixedCostAnalyticsHelper = Class.create();
FixedCostAnalyticsHelper.prototype = {
  initialize: function(definition, current) {
  	this.definition = definition;
  	this.current = current;
  },
  createMetricInstance: function() {
  	var costHelper, costVal, gr, mi = new MetricInstance(this.definition, this.current);
  	if(mi.metricExists()) {
  		gs.debug("Metric already exists for this definition. Skipping for "+this.current.sys_id.toString());
  		return;
  	}
  	costVal = this._getFixedCost();
  	if(costVal == 0) {
  		gs.debug("Cost value is zero, skipping for "+this.current.sys_id.toString());
  		return;
  	}
  	gr = mi.getNewRecord();
  	gr.setValue('value', Number(costVal));
  	gr.setValue('calculation_complete', true);
  	gr.insert();
  },
  _getFixedCost: function() {
  	var totalCost = 0, costVal, resolveTime, gr = new GlideRecord('fixed_cost_definition');

  	if(!this.current.isValidField('sys_domain') || this.current.getValue('sys_domain') === 'global') {
  		//the table doesn't support domain separation or the current record is in global scope, apply only global models
  		gr.addQuery('sys_domain', 'global');
  	} else {
  		gr.addDomainQuery(this.current);
  	}
  	gr.addQuery('table', this.current.getTableName());
  	gr.addActiveQuery();
  	gr.orderBy('order');
  	gr.query();
  	while(gr.next()) {
  		if(!this._checkIfRecordFitsCostModel(gr)) {
  			gs.debug("Did not fit cost model "+gr.sys_id.toString());
  			continue;
  		}
  		costVal = gr.cost_value.getReferenceValue();
  		if(gr.getValue('category') === 'hourly_cost') {
  			resolveTime = this._getResolveTime();
  			gs.debug("Resolve time="+resolveTime);
  			if (resolveTime) {
  				totalCost = ((resolveTime / ( 60 * 60)) * costVal).toFixed(2);
  			}
  		} else {
  			totalCost = costVal;
  		}
  		return totalCost;
  	}

  	return 0;
  },
  _checkIfRecordFitsCostModel: function(costModel) {
  	var filter = costModel.getValue('filter');
  	if(!filter) {
  		return true;
  	}
  	return GlideFilter.checkRecord(this.current, filter);
  },
  _getResolveTime: function() {
  	var tableName = this.current.getTableName();
  	if (tableName === 'incident' || tableName === 'sc_request') {
  		return Number(current.getValue('calendar_stc'));
  	}

  	return gs.dateDiff(this.current.sys_created_on.getDisplayValue(), this.current.sys_updated_on.getDisplayValue(), true);

  },
  type: 'FixedCostAnalyticsHelper'
};

Sys ID

a4bf09e1ff40230048e505f6793bf1be

Offical Documentation

Official Docs: