API Name: global.MIDServerUtil

var MIDServerUtil;

(function() {

MIDServerUtil = {
createOrUpdateIssue: createOrUpdateIssue,
resolveExistingIssues: resolveExistingIssues,
getUnresolvedIssueCount: getUnresolvedIssueCount

* Insert or update an issue based on MID, source and message. Existing resolved issues are ignored.

function createOrUpdateIssue(midSysId, issueSource, issueMessage) {
var issueSysId;

var issue = new GlideRecord('ecc_agent_issue');
issue.addQuery('mid_server', midSysId);
issue.addQuery('source', issueSource);
issue.addQuery('message', issueMessage);
issue.addQuery('state', '!=', 'resolved');

if (issue.next()) {
issueSysId = issue.getValue('sys_id');
// issue exists, just update last_detected and count
issue.setValue('last_detected', new GlideDateTime());
issue.setValue('count', (+ issue.getValue('count')) + 1);
} else {
issue.setValue('mid_server', midSysId);
issue.setValue('source', issueSource);
issue.setValue('message', issueMessage);
issue.setValue('last_detected', new GlideDateTime());
issueSysId = issue.insert();

return issueSysId;

* Resolve all open (acknowledged, new) issues for a given MID Server, source and (optionally) message.
* If a message is given, only issues that match the message will be resolved.

function resolveExistingIssues(midSysId, issueSource, issueMessage) {
var openIssues = new GlideRecord('ecc_agent_issue');
openIssues.addQuery('mid_server', midSysId);
openIssues.addQuery('source', issueSource);
openIssues.addQuery('state', '!=', 'resolved');
if (JSUtil.notNil(issueMessage))
openIssues.addQuery('message', issueMessage);
openIssues.setValue('state', 'resolved');

* Get unresolved issue count for a given MID Server.

function getUnresolvedIssueCount(midSysId) {
var gr = new GlideRecord('ecc_agent_issue');
gr.addQuery('mid_server', midSysId);
return gr.getRowCount();