Skip to content

Instantly share code, notes, and snippets.

@jmbauguess
Last active August 4, 2016 13:08
Show Gist options
  • Save jmbauguess/666e7e302c51bf7da4ec to your computer and use it in GitHub Desktop.
Save jmbauguess/666e7e302c51bf7da4ec to your computer and use it in GitHub Desktop.
Updates SLAs based on a period of time
/**
* @description Updates SLAs based on conditions and stages
* @namespace
* @type {Class}
*/
var SLARunner = Class.create();
SLARunner.prototype = {
/**
* @description The name of the SLA table
* @type {String}
*/
TABLENAME: 'task_sla',
/**
* @description Main method
* @param {Number} number How many days ago to calculate
*/
run: function(number) {
var slas = this.getNonPausedSLAs(number);
while (slas.next()) {
this.calculate(slas);
}
},
/**
* @description Runs the ServiceNow SLA calculation code
* @param {GlideRecord} slaRecord A task_sla record
*/
calculate: function(slaRecord) {
try {
SLACalculatorNG.calculateSLA(slaRecord);
slaRecord.stage = this.getTaskSLAStage(slaRecord);
slaRecord.update();
} catch (e) {
gs.print(e);
}
},
/**
* @description Gets SLAs that need calculating
* @param {Number} number How many days ago to calculate
* @return {GlideRecord} Gliderecords of task_sla
*/
getNonPausedSLAs: function(number) {
var slas = new GlideRecord(this.TABLENAME);
slas.addQuery('stage', '!=', 'paused');
slas.addQuery('sla.collection', 'incident');
if (number) {
slas.addEncodedQuery('task.sys_updated_onONToday@javascript:gs.daysAgoStart(' + number + ')@javascript:gs.daysAgoEnd(0)');
}
slas.query();
return slas;
},
/**
* @description Gets the appropriate stage of the Task SLA
* @param {GlideRecord} slaRecord The sla record
* @return {String} The stage of the task_sla
*/
getTaskSLAStage: function(slaRecord) {
if (slaRecord.stage != 'in_progress') {
return slaRecord.stage;
}
if (slaRecord.has_breached) {
return 'breached';
}
if (slaRecord.end_time) {
return 'achieved';
}
return slaRecord.stage;
},
/**
* @description Updates every SLA for incident that is not paused
*/
__updateEverySLAEver: function() {
this.run();
},
'type' : 'SLARunner'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment