Name

sn_grc.ScriptIndicatorStrategyBase

Description

Script strategy for indicator.

Script

var ScriptIndicatorStrategyBase = Class.create();
ScriptIndicatorStrategyBase.prototype = Object.extendsObject(IndicatorStrategy,{
  
  /*
  Gets the variables (result.passed and result.value) set in the script field of the current record,
  and puts these values in the Indicator result record.
  */
  run: function() {
  	var resultId = '';
  	var table = this.indicatorRecord.reference_table;
  	var fields = this.indicatorRecord.supporting_data_fields.toString().split(",");
  	var evaluator = new GlideScopedEvaluator();
  	var indicatorResult = {passed: null, value: null};
  	evaluator.putVariable('result', indicatorResult);
  	evaluator.putVariable('current', this.indicatorRecord.item);
  	evaluator.putVariable('target', this.indicatorRecord.target);
  	evaluator.putVariable('target_type', this.indicatorRecord.target_type);
  	evaluator.putVariable('current', this.indicatorRecord.item);
  	evaluator.evaluateScript(this.indicatorRecord, 'script',{});
  	if(indicatorResult.passed != null && indicatorResult.value != null) {
  		resultId = this.addResult(indicatorResult.passed, "", indicatorResult.value, "", this.indicatorRecord.target_type.getDisplayValue(), this.indicatorRecord.target);
  		if(indicatorResult.supportingDataIds != null)
  			this.insertSupportingData(resultId, table, fields, indicatorResult.supportingDataIds);
  		else
  			gs.debug("No supporting data provided for " + this.indicatorRecord.number);
  	}
  	else {
  		gs.debug("Skipping record " + this.indicatorRecord.number + " as result.passed or result.value is null");
  	}
  	evaluator.reset();
  	return resultId;
  },
  
  insertSupportingData: function(resultId, tableName, fields, supportingDataIds) {
  	var indicatorResult = new GlideRecord('sn_grc_indicator_result');
  	indicatorResult.get(resultId);
  	
  	// null check
  	if(tableName == null || tableName == '') {
  		gs.debug("Skipping supporting data for the indicator result " + indicatorResult.label + " because table is not defined");
  		return;
  	}

  	var record = new GlideRecord(tableName);

  	// Get all fields if supporting data does not have fields
  	if(!fields || fields.length == 0)
  		fields = this.getAllFields(record);
  	
  	var isValid = true;
  	
  	// Get the supporting data record
  	for(var i = 0; i < supportingDataIds.length; i++) {
  		isValid = record.get(supportingDataIds[i]);
  		
  		// null check
  		if(!isValid) {
  			gs.debug("Could not find the record " + i + " for table " + tableName);
  			break;
  		}
  		
  		for(var j = 0; j < fields.length; j++) {
  			var fieldName = fields[j];
  			var fieldValue = record.getElement(fields[j]).getDisplayValue();
  			this.insertSupportingDataRecord(tableName, record.getUniqueValue(), record.getElement(fieldName).getName(), fieldName, fieldValue, resultId);
  		}
  	}
  },

  type: 'ScriptIndicatorStrategyBase'
});

Sys ID

2f1772dac3320200dd921a4112d3ae56

Offical Documentation

Official Docs: