Name
sn_gf.TargetProgressUtilSNC
Description
No description available
Script
var TargetProgressUtilSNC = Class.create();
TargetProgressUtilSNC.prototype = {
initialize: function(targetGr) {
this.targetGr = targetGr;
},
logTargetProgress: function() {
if (!this.targetGr || !this.targetGr.isValid() || this.targetGr.getTableName() != GoalFrameworkConstants.GOAL_TARGET_TABLE)
return;
var progressFieldsToBeLogged = {
'goal': this.targetGr.getValue('goal'),
'target': this.targetGr.getUniqueValue(),
'progress_value': this.targetGr.getValue('actual_value'),
'percent_complete': this.targetGr.getValue('percent_complete'),
'progress_date': new GlideDate()
};
var progressFieldsToBeupdated = {
'target': this.targetGr.getUniqueValue(),
'progress_value': this.targetGr.getValue('actual_value'),
'percent_complete': this.targetGr.getValue('percent_complete'),
};
var targerProgressGr;
targerProgressGr = this._getTodaysTargetProgressGr();
if (targerProgressGr && targerProgressGr.isValid()) {
for (var progressRecordField in progressFieldsToBeupdated) {
if (progressRecordField && progressFieldsToBeupdated[progressRecordField])
targerProgressGr.setValue(progressRecordField, progressFieldsToBeupdated[progressRecordField]);
}
return targerProgressGr.update();
} else {
targerProgressGr = new GlideRecord(GoalFrameworkConstants.GOAL_TARGET_PROGRESS_TABLE);
targerProgressGr.initialize();
for (var progressRecordField in progressFieldsToBeLogged) {
if (progressRecordField && progressFieldsToBeLogged[progressRecordField])
targerProgressGr.setValue(progressRecordField, progressFieldsToBeLogged[progressRecordField]);
}
return targerProgressGr.insert();
}
},
_getTodaysTargetProgressGr: function() {
var targerProgressGr = new GlideRecord(GoalFrameworkConstants.GOAL_TARGET_PROGRESS_TABLE);
if (targerProgressGr.isValid()) {
targerProgressGr.addQuery('target', this.targetGr.getUniqueValue());
targerProgressGr.addQuery('progress_date', new GlideDate());
targerProgressGr.query();
if (targerProgressGr.next())
return targerProgressGr;
}
return null;
},
clearTargetProgress: function() {
if (!this.targetGr || !this.targetGr.isValid() || this.targetGr.getTableName() != GoalFrameworkConstants.GOAL_TARGET_TABLE)
return;
var targerProgressGr = new GlideRecord(GoalFrameworkConstants.GOAL_TARGET_PROGRESS_TABLE);
if (targerProgressGr.isValid()) {
targerProgressGr.addQuery('target', this.targetGr.getUniqueValue());
targerProgressGr.query();
targerProgressGr.deleteMultiple();
}
},
isProgressCaptured: function() {
if (!this.targetGr || !this.targetGr.isValid() || this.targetGr.getTableName() != GoalFrameworkConstants.GOAL_TARGET_TABLE)
return;
var targerProgressGr = new GlideRecord(GoalFrameworkConstants.GOAL_TARGET_PROGRESS_TABLE);
if (targerProgressGr.isValid()) {
targerProgressGr.addQuery('target', this.targetGr.getUniqueValue());
targerProgressGr.setLimit(1);
targerProgressGr.query();
if (targerProgressGr.hasNext())
return true;
}
return false;
},
updateTargetProgressGoalWithNewTargetGoal: function() {
if (!this.targetGr || !this.targetGr.isValid() || this.targetGr.getTableName() != GoalFrameworkConstants.GOAL_TARGET_TABLE)
return;
var targerProgressGr = new GlideRecord(GoalFrameworkConstants.GOAL_TARGET_PROGRESS_TABLE);
if (targerProgressGr.isValid()) {
targerProgressGr.addQuery('target', this.targetGr.getUniqueValue());
targerProgressGr.query();
while (targerProgressGr.next()) {
targerProgressGr.setValue('goal', this.targetGr.getValue('goal'));
targerProgressGr.update();
}
}
},
updateTargetProgressWithNewTargetValue: function() {
if (!this.targetGr || !this.targetGr.isValid() || this.targetGr.getTableName() != GoalFrameworkConstants.GOAL_TARGET_TABLE)
return;
var targerProgressGr = new GlideRecord(GoalFrameworkConstants.GOAL_TARGET_PROGRESS_TABLE);
if (targerProgressGr.isValid()) {
targerProgressGr.addQuery('target', this.targetGr.getUniqueValue());
targerProgressGr.query();
while (targerProgressGr.next()) {
var newPercentageComplete;
var currentTargetValue = parseFloat(this.targetGr.getValue('target_value'));
var currentBaseValue = parseFloat(this.targetGr.getValue('base_value'));
var currentActualValue = parseFloat(targerProgressGr.getValue('progress_value'));
if (this.targetGr.getValue('type') == 'maximize')
newPercentageComplete = (currentActualValue - currentBaseValue) / (currentTargetValue - currentBaseValue);
else
newPercentageComplete = (currentBaseValue - currentActualValue) / (currentBaseValue - currentTargetValue);
targerProgressGr.setValue('percent_complete', newPercentageComplete*100);
targerProgressGr.update();
}
}
},
getLastActualUpdated: function(){
var lastActualUpdatedTime;
var targerProgressGr = new GlideRecord(GoalFrameworkConstants.GOAL_TARGET_PROGRESS_TABLE);
targerProgressGr.addQuery('target', this.targetGr.getUniqueValue());
targerProgressGr.orderByDesc('sys_updated_on');
targerProgressGr.setLimit(1);
targerProgressGr.query();
if (targerProgressGr.next())
lastActualUpdatedTime = targerProgressGr.sys_updated_on.getDisplayValue();
return lastActualUpdatedTime;
},
type: 'TargetProgressUtilSNC'
};
Sys ID
f269a1b153013010c8d4ddeeff7b12cb