-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_expiration_email.js
78 lines (78 loc) · 3.16 KB
/
send_expiration_email.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
define(['N/search', 'N/email', 'N/url'], function(search, email, url){
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
*/
function execute(scriptContext) {
const items = [];
const lots = [];
const dates = [];
const urls = [];
var emailBody = "";
var expiredSearch = search.load({ //searches for inventory items that expire within 2 weeks
id: 'customsearchalmost_expired'
});
expiredSearch.run().each(function(result){
if(result != null && result != ''){
var currentItem = result.getText({
name: "item",
summary: "GROUP"
});
var currentLot = result.getText({
name: "inventorynumber",
summary: "GROUP"
});
var lotId = result.getValue({
name: "inventorynumber",
summary: "GROUP"
});
var currentURL = url.resolveRecord({
recordType: "inventorynumber",
recordId: parseInt(lotId),
isEditMode: true
});
var currentDate = result.getValue({
name: "expirationdate",
summary: "GROUP"
});
items.push(currentItem); //add data to arrays
lots.push(currentLot);
dates.push(currentDate);
urls.push(currentURL);
}
return true;
});
log.error("Items", items);
log.error("Lots", lots);
log.error("Dates", dates);
log.error("URLs", urls);
if(items.length > 0){ //execute if there will be inventory that expires within two weeks
emailBody = "<b>Please Recertify The Following Expiration Dates:</b><br><br>";
for(var i = 0; i < items.length; i++){
var currentItem = items[i].toString();
var currentLot = lots[i].toString();
var currentDate = dates[i].toString();
var currentURL = urls[i].toString();
emailBody += " - <b><a href=" + currentURL + ">" + currentItem + "</a></b>: Lot " + currentLot + " (Expires " + currentDate + ")<br>";
}
try{ //try to send email
email.send({
author: -5, //internal ID of user
recipients: ["fakeemail@gmail.com"],
subject: "Ingredient Expiration Recertification",
body: emailBody
});
log.error("Email", emailBody);
log.error("Email Sent");
}
catch(err){
log.error("Email", emailBody);
log.error("Email was not sent");
}
}
log.error("Number of Ingredients that Expire Within 2 Weeks", items.length); //log the number almost expired ingredients
}
return {
execute: execute
}
});