mchaiwimol
11/29/2017 - 12:13 AM

Notify KB authors when articles are expiring

Notify authors of expiring articles.

Script Include: KBNotify
Email Script: expiring.articles
Scheduled Job: Knowledge Article Expiring Notifications
Notification: Expiring Articles Notification

Prerequisite:

1. Create an event registry called "kb.expiring".

Table: Knowledge [kb_knowledge]
Fired by: Scheduled Job - Knowledge Article Expiring Notifications
Description: Notify authors that the KB article is about to expire

To use:

Supply the number of days in the arguments. For example, 15 days
new KBNotify().notifyAuthors(15);
Subject: 

Your KB article(s) is about to expire in ${event.parm2} days.

Body:

The following article(s) will expire in ${event.parm2} days. Please review and make any necessary updates before it is automatically retired.

${mail_script:expiring.articles}
new KBNotify().notifyAuthors(15);
new KBNotify().notifyAuthors(30);
new KBNotify().notifyAuthors(60);
(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
	
	var author = event.parm1;
	var days = event.parm2;
	var articleDesc = new KBNotify().printExpiringArticles(author, days);
	
	template.print(articleDesc);
	
})(current, template, email, email_action, event);
var KBNotify = Class.create();
KBNotify.prototype = {
	initialize: function() {
	},
	
	//Generate event for each author that has expiring articles
	
	notifyAuthors: function(days) {
		var authors = this._findAuthors(days);
		
		if (authors != '') {
			for (var i = 0; i < authors.length; i++) {
				gs.eventQueue('kb.expiring', null, authors[i], days);
			}
		} else {
			gs.info("There are no articles expiring in {0} days", days);
		}
	},
	
	//Find the authors that has kb articles that are expiring
	
	_findAuthors: function(days) {
		var arr = [];
		var arrayUtil = new ArrayUtil();
		
		var gr = new GlideRecord('kb_knowledge');
		gr.addQuery('workflow_state', 'published');
		gr.addQuery('valid_to', this.getFutureDate(days));
		gr.query();
		
		while (gr.next()) {
			arr.push(gr.author + '');
		}
		
		return arrayUtil.unique(arr);
	},
	
	//Get future day by supplying the number of days in the argument
	
	getFutureDate: function(days) {
		return gs.daysAgo(-days);
	},
	
	//Return a list of expiring articles
	
	printExpiringArticles: function(author, days) {
		
		var articleDesc = '';
		var link;
		
		var gr = new GlideRecord('kb_knowledge');
		gr.addQuery('workflow_state', 'published');
		gr.addQuery('valid_to', this.getFutureDate(days));
		gr.addQuery('author', author);
		gr.query();
		
		while (gr.next()) {
			
			link = this.getKBLink(gr.getLink(), gr.number);
			
			if (articleDesc == '') {
				articleDesc = link + ": " + gr.short_description;
			} else {
				articleDesc += "<br />" + link + ": " + gr.short_description;
			}
		}
		
		return articleDesc;
	},
	
	//Get the URL link to the kb article
	
	getKBLink: function(link, kbNumber) {
		var str = '<a href="' + gs.getProperty('glide.servlet.uri') + link + '">' + kbNumber + '</a>';
		return str;
	},
	
	type: 'KBNotify'
};