Skip to content

Instantly share code, notes, and snippets.

@rbw
Last active June 7, 2019 15:41
Show Gist options
  • Save rbw/5b44842a6626eeaf2b024a59d93752d1 to your computer and use it in GitHub Desktop.
Save rbw/5b44842a6626eeaf2b024a59d93752d1 to your computer and use it in GitHub Desktop.
var TaskMessages = Class.create();
TaskMessages.prototype = {
initialize: function(sysId) {
this.sysId = sysId;
this.exclude = [
'calendar_stc', 'sys_class_name', 'calendar_duration', 'sys_domain,location',
'business_duration', 'reassignment_count', 'upon_reject',
];
this._generateHistory();
},
_generateHistory: function() {
var ghs = new GlideHistorySet('task', this.sysId);
try {
if(ghs.getSummaryRecord() !== null) {
ghs.refresh();
} else {
ghs.generate();
}
} catch (e) {
gs.log(e);
throw new Error("Unexpected error when attempting to generate history for: ", this.sysId);
}
},
_getMessages: function(types, notTypes, updatesOnly) {
var hs = new GlideRecord('sys_history_set');
var hsId = null;
hs.orderByDesc('sys_created_on');
hs.addQuery('id', this.sysId);
hs.setLimit(1);
hs.query();
if(hs.next()) {
hsId = hs.getValue('sys_id');
} else {
throw new Error("No HistorySet for: ", this.sysId);
}
// Query history line by set id and filter out non-used fields.
var hl = new GlideRecord('sys_history_line');
hl.addQuery('set', hsId);
if(updatesOnly === true) {
hl.addQuery('update', '!=', 0);
}
hl.addQuery('field', 'NOT IN', this.exclude.join(','));
if(notTypes) {
hl.addQuery('field', 'NOT IN', notTypes);
}
if(types) {
hl.addQuery('field', 'IN', types);
}
hl.orderByDesc('update_time');
hl.query();
var lines = [];
while(hl.next()) {
var line = {};
line.sys_id = hl.getValue('sys_id');
line.new_value = hl.getValue('new');
line.old_value = hl.getValue('old');
line.user_name = hl.getValue('user_name');
line.user_company = hl.user.company.getDisplayValue('name');
line.update = hl.getValue('update');
line.label = hl.getValue('label');
line.created_on = hl.getValue('update_time');
line.field = hl.getValue('field');
lines.push(line);
}
return lines;
},
get events() {
return this._getMessages([], ['work_notes', 'comments'], true);
},
get workNotes() {
return this._getMessages(['work_notes'], [], false);
},
get comments() {
return this._getMessages(['comments'], [], false);
},
type: 'TaskMessages'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment