Skip to content

Instantly share code, notes, and snippets.

@jmbauguess
Created August 26, 2015 15:31
Show Gist options
  • Save jmbauguess/46ddb5ee7665d15d0e96 to your computer and use it in GitHub Desktop.
Save jmbauguess/46ddb5ee7665d15d0e96 to your computer and use it in GitHub Desktop.
/**
* @description Searches for email notifications based on certain criteria
* @namespace
* @type {Class}
*/
var NotificationsTester = Class.create();
NotificationsTester.prototype = {
/**
* @description The name of the email table
* @type {String}
*/
TABLE_NAME: 'sys_email',
/**
* @description An encoded query for emails created today that are in the outbox or sent folders
* @type {String}
*/
ENCODED_QUERY : 'mailbox=sent^ORmailbox=outbox^sys_created_onONToday@javascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)',
/**
* @description Calls the same code from the Get/Send Email module
*/
getSendEmail: function() {
var pop3 = new GlidePOP3ReaderJob(),
smtp = new GlideSMTPSenderJob();
pop3.execute();
gs.eventsProcess();
smtp.execute();
},
/**
* @description Gets emails that contain a string containing a recipient
* @param {String} recipient A string of a recipient
* @return {GlideRecord} A list of email records
* @example
* var nt = new NotificationsTester(),
* emails = nt.getEmailsByRecipient('jmbauguess');
*/
getEmailsByRecipient: function(recipient) {
return this.getEmails('recipients', 'contains', recipient);
},
/**
* @description Gets emails that contain subject text
* @param {String} subject A string of a subject to search for
* @return {GlideRecord} A list of email records
* @example
* var nt = new NotificationsTester(),
* emails = nt.getEmailsBySubject('Change Request');
*/
getEmailsBySubject: function(subject) {
return this.getEmails('subject', 'contains', subject)
},
/**
* @description Gets emails that are for a certain record
* @param {String} instance The sys_id of a record
* @return {GlideRecord} A list of email records
* @example
* var changeRecord = new GlideRecord('change_request');
* changeRecord.get('number', 'CHG000001');
* var nt = new NotificationsTester(),
* emails = nt.getEmailsByInstance(changeRecord.sys_id);
*/
getEmailsByInstance: function(instance) {
return this.getEmails('instance', '=', instance);
},
/**
* @description Main method - called by other specific instances at times
* @param {String} queryField What field to query
* @param {String} queryOperator What query operation to use
* @param {String} queryValue What value to query for
* @return {GlideRecord} A list of email records
* @example
* var nt = new NotificationsTester(),
* emails = nt.getEmails('content_type', '=', 'multipart/mixed');
*/
getEmails: function(queryField, queryOperator, queryValue) {
this.getSendEmail();
var emails = new GlideRecord(this.TABLE_NAME);
emails.addEncodedQuery(this.ENCODED_QUERY);
emails.addQuery(queryField, queryOperator, queryValue);
emails.query();
return emails;
},
'type' : 'NotificationsTester'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment