RJMNS
02/16/2021, 10:29 PMJono
02/16/2021, 10:59 PMjen
02/16/2021, 11:04 PMjen
02/16/2021, 11:08 PMfunction undeliveredEmailNotifications(context) {
// Number of seconds' worth of time to check for undelivered emails.
var seconds = 14400; // Four hours
//seconds = 604800; // one week
//seconds = 1296000; // two weeks
// Start by getting the names & email addresses of all current employees
// so we can check for emails sent by them that were undelivered.
var employee_search = search.create({
type: 'employee',
filters: [
['isinactive', 'is', 'F'],
'AND',
['hiredate', 'onorbefore', 'today'],
'AND',
[['releasedate', 'isempty', ''], 'OR', ['releasedate', 'after', 'today']]
],
columns: [
search.createColumn({name: 'internalid', label: 'ID'}),
search.createColumn({name: 'entityid', label: 'Name'}),
search.createColumn({name: 'email', label: 'Email'})
]
});
var employees = [];
employee_search.run().each(function(result) {
employees[result.getValue('entityid')] = {internalid: result.getValue('internalid'), email: result.getValue('email'), undelivereds: []};
return true;
});
if(log_level == 1) log.debug({title: 'undeliveredEmailNotifications', details: 'employees: ' + JSON.stringify(employees)});
// Add a catch-all for any emails "sent from" something that doesn't look like
// the name of an employee.
employees['catchall'] = {email: 'catchall', undelivereds: []};
// Search undelivered emails.
var undelivered_email_search = search.create({
type: 'undeliveredemail',
filters: [['formulanumeric: (CAST(current_date AS DATE) - CAST({sentdate} AS DATE)) * 86400', 'lessthanorequalto', seconds]],
columns: [
search.createColumn({name: 'sentdate', label: 'Sent Date'}),
search.createColumn({name: 'reason', label: 'Reason'}),
search.createColumn({name: 'subject', label: 'Subject'}),
search.createColumn({name: 'from', label: 'From'}),
search.createColumn({name: 'recipients', label: 'Recipients'})
]
});
// For each undelivered email, add it to the list for the employee who sent it.
// Also add it to mt list if not already there...
undelivered_email_search.run().each(function(result) {
var from_string = result.getValue('from');
var from_name = '';
var from_email = '';
// Remove quote marks (in case there are some).
from_string = from_string.replace(/"/g,"");
// Separate out the NAME from the EMAIL (e.g. Bob Smith <bob@smith.com>)
if(from_string.indexOf('<') !== -1) {
var from_string_parts = from_string.split('<');
from_name = from_string_parts[0].trim();
from_string_parts = from_string_parts[1].split('>');
from_email = from_string_parts[0].trim();
} else {
from_name = from_string;
from_email = from_string;
}
if(log_level == 1) log.debug({title: 'undeliveredEmailNotifications', details: 'Found an undelivered email from ' + from_name});
// Check to see if this is an identified employee name.
if(employees[from_name] != null) {
employees[from_name].undelivereds.push({
from: from_string,
sentdate: result.getValue('sentdate'),
reason: result.getValue('reason'),
subject: result.getValue('subject'),
recipients: result.getValue('recipients')
});
} else {
// Add to the catch-all.
employees['catchall'].undelivereds.push({
from: from_string,
sentdate: result.getValue('sentdate'),
reason: result.getValue('reason'),
subject: result.getValue('subject'),
recipients: result.getValue('recipients')
});
}
return true;
});
// Send notifications to employees that their emails bounced.
for(var employee_name in employees) {
if(log_level == 1) log.debug({title: 'undeliveredEmailNotifications', details: 'employee_name is ' + employee_name});
// What is their ID?
var internalid = <my_id>; // catchall email goes to me.
if(employee_name != 'catchall')
internalid = employees[employee_name].internalid;
// Did they get any bounces?
if(employees[employee_name].undelivereds.length > 0) {
if(log_level == 1) log.debug({title: 'undeliveredEmailNotifications', details: employee_name + ' has undelivereds'});
// Build the message.
var subject = 'Undelivered Email: ' + employee_name;
var body = 'The following recent email sent through NetSuite was undelivered:<br/><br/>';
if(employees[employee_name].undelivereds.length > 1) {
subject = 'Undelivered Emails: ' + employee_name;
body = 'The following recent emails sent through NetSuite were undelivered:<br/><br/>';
}
for(var i = 0; i < employees[employee_name].undelivereds.length; i++) {
body += 'Date: ' + employees[employee_name].undelivereds[i].sentdate + '<br/>';
body += 'From: ' + employees[employee_name].undelivereds[i].from + '<br/>';
body += 'Subject: ' + employees[employee_name].undelivereds[i].subject + '<br/>';
body += 'Recipients: ' + employees[employee_name].undelivereds[i].recipients + '<br/>';
body += 'Reason: ' + employees[employee_name].undelivereds[i].reason + '<br/><br/>';
}
if(log_level == 1) log.debug({title: 'undeliveredEmailNotifications', details: 'sending an email from Jen to ' + internalid + ' with body ' + body});
email.send({
author: netsuite_notifications,
recipients: internalid,
subject: subject,
body: body
});
}
}
}
RJMNS
02/17/2021, 12:16 AMRJMNS
02/17/2021, 12:17 AMjen
02/17/2021, 12:19 AMjen
02/17/2021, 12:19 AMjen
02/17/2021, 12:19 AMjen
02/17/2021, 12:19 AMjen
02/17/2021, 12:19 AMRJMNS
02/17/2021, 12:21 AMRJMNS
02/17/2021, 12:21 AMJono
02/17/2021, 1:15 AMtdietrich
02/17/2021, 2:36 AMdansteg
02/17/2021, 4:04 AMRJMNS
02/17/2021, 3:43 PM