Jira Code: Prof -33
A NetSuite Customization to notify the warehouse manager about the back ordered items in the sales order. So that warehouse manager can proceed with the order processing.
A user event script is deployed in item receipt and item adjustment record. Whenever there is an item which is back ordered then the script will identify the order and send the details to the warehouse manager.
Scripts used are user event script and html (Email Template)
PROF-33 UE BackOrdered Notify
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
define(['N/file', 'N/url', 'N/search', 'N/runtime', 'N/record', 'N/https', 'N/ui/serverWidget', 'N/email'],
function(file, url, search, runtime, record, https, serverWidget, email) {
var main = {
beforeSubmit: function(scriptContext) {
if (scriptContext.type == "create") {
log.debug("scriptContext.type ", scriptContext.type);
var records = scriptContext.newRecord;
var itemadjustment = {};
itemadjustment.type = records.type;
itemadjustment.itemdetails = main.gettheitems(records, records.type);
log.debug("itemadjustment", itemadjustment);
var salesord = main.getthebackorderd(itemadjustment.itemdetails.recdataarray);
main.createcontent(salesord, itemadjustment);
}
},
gettheitems: function(recid, rectype) {
//log.debug("recid ", recid);
log.debug("rectype ", rectype);
var loadrec = recid;
var Tranid = loadrec.getValue({
fieldId: 'tranid'
});
var recdataarray = [];
if (rectype == "itemreceipt") {
var sublisttype = "item";
var numLines = loadrec.getLineCount({
sublistId: sublisttype
});
} else {
var sublisttype = "inventory";
var numLines = loadrec.getLineCount({
sublistId: sublisttype
});
}
for (var i = 0; i < numLines; i++) {
var recdata = {};
log.debug("sublisttype", sublisttype)
recdata.item = loadrec.getSublistValue({
sublistId: sublisttype,
fieldId: 'item',
line: i
});
recdata.itemname = loadrec.getSublistText({
sublistId: sublisttype,
fieldId: 'itemname',
line: i
});
recdataarray.push(recdata);
}
return {
recdataarray: recdataarray,
Tranid: Tranid
}
},
createcontent: function(salesorderdetails, inventoryadjustment) {
log.debug("salesorderdetails", salesorderdetails);
log.debug("inventoryadjustment", inventoryadjustment);
var content;
var fileObj = file.load({
id: 18913
});
if (fileObj.size < 10485760) {
var Template = fileObj.getContents();
}
var salesrow = "";
for (var i = 0; i < salesorderdetails.length; i++) {
var url = "https://system.netsuite.com/app/accounting/transactions/salesord.nl?id=<replaceurls>&whence="
var url = url.replace("<replaceurls>", salesorderdetails[i].internalid)
salesrow += '<tr><td><a href = "' + url + '" > ' + salesorderdetails[i].traind + ' </a></td></tr>'
}
var itemrow = "";
for (var i = 0; i < inventoryadjustment.itemdetails.recdataarray.length; i++) {
var fieldLookUp = search.lookupFields({
type: "item",
id: inventoryadjustment.itemdetails.recdataarray[i].item,
columns: ['name']
});
log.debug("fieldLookUp", fieldLookUp)
itemrow += '<li>' + fieldLookUp.name + '</li>'
}
if (inventoryadjustment.type == "inventoryadjustment") {
var types = "Inventory Adjustment";
} else {
var types = "Item Reciept";
}
content = Template.replace("<-ReplaceWithRecord->", inventoryadjustment.itemdetails.Tranid).replace("<-ReplacewithURL->", salesrow).replace("<-Replcaewithitems->", itemrow).replace("<-replacewithtype->", types);
var fileObj = file.create({
name: 'Inventorytemplate.html',
fileType: file.Type.HTMLDOC,
contents: content,
encoding: file.Encoding.UTF8,
folder: '-10',
isOnline: true
});
var fileid = fileObj.save();
try {
// create cc
var cc_array = [];
var sales_rep;
for (var i = 0; i < salesorderdetails.length; i++) {
//sales_rep=salesorderdetails[i].salesrep;
if (cc_array.indexOf(salesorderdetails[i].salesrep) == -1) {
cc_array.push(salesorderdetails[i].salesrep)
}
}
log.debug("cc_array", cc_array);
var salesrep_email;
var email_array = [];
for (var j = 0; j < cc_array.length; j++) {
salesrep_email = search.lookupFields({
type: search.Type.EMPLOYEE,
id: cc_array[j],
columns: ['email']
});
salesrep_email = salesrep_email.email;
//log.debug("salesrep_email",salesrep_email);
//log.debug("salesrep_emailll",salesrep_email.email);
if ((salesrep_email != "") && (salesrep_email != null) && (salesrep_email != undefined)) {
email_array.push(salesrep_email);
}
}
log.debug("email_array", email_array);
} catch (e) {
log.debug("cc err", e);
}
if (salesorderdetails.length > 0)
main.sendemail(content, email_array);
},
sendemail: function(content, email_array) {
//rgutierrez@proficium.com
log.debug("email_array", email_array);
if (email_array.length > 0) {
email.send({
author: "-5",
recipients: "RGutierrez@proficium.com",
cc: email_array,
subject: 'Inventory Adjustment',
body: content
});
} else {
email.send({
author: "-5",
recipients: "RGutierrez@proficium.com",
subject: 'Inventory Adjustment',
body: content
});
}
log.debug("mail send", 'mail send')
},
getthebackorderd: function(itemdetails) {
var itemarray = ["item.internalid", "anyof"];
for (var i = 0; i < itemdetails.length; i++) {
itemarray.push(itemdetails[i].item);
}
// var itemarray = itemarray.toString();
// var filter = ["item.internalid", "anyof", itemarray];
var filter = [
["type", "anyof", "SalesOrd"],
"AND", ["item.quantitybackordered", "greaterthan", "0"],
"AND", itemarray,
"AND", ["status", "anyof", "SalesOrd:B", "SalesOrd:A", "SalesOrd:D"]
]
log.debug("filtetr", filter)
var salesorderSearchObj = search.create({
type: "salesorder",
filters: [
filter
],
columns: [
search.createColumn({ name: "entity", label: "Name" }),
search.createColumn({ name: "internalid", label: "Internal ID" }),
search.createColumn({ name: "tranid", label: "Document Number" }),
search.createColumn({ name: "salesrep", label: "Sales Rep" }),
search.createColumn({
name: "quantitybackordered",
join: "item",
label: "Back Ordered"
})
]
});
var searchResultCount = salesorderSearchObj.runPaged().count;
log.debug("salesorderSearchObj result count", searchResultCount);
var salesorderarray = [];
salesorderSearchObj.run().each(function(result) {
var salesobj = {};
var back = result.getValue({ name: "quantitybackordered", join: "item" });
if (back > 0) {
salesobj.internalid = result.getValue("internalid");
salesobj.traind = result.getValue("tranid");
salesobj.salesrep = result.getValue("salesrep");
}
salesorderarray.push(salesobj);
return true;
});
var salesorderarray = main.removeDuplicates(salesorderarray, "internalid");
return salesorderarray;
},
removeDuplicates: function(originalArray, prop) {
var newArray = [];
var lookupObject = {};
for (var i in originalArray) {
lookupObject[originalArray[i][prop]] = originalArray[i];
}
for (i in lookupObject) {
newArray.push(lookupObject[i]);
}
log.debug("newArray", newArray)
return newArray;
}
};
for (var key in main) {
if (typeof main[key] === 'function') {
main[key] = trycatch(main[key], key);
}
}
function trycatch(myfunction, key) {
return function() {
try {
return myfunction.apply(this, arguments);
} catch (e) {
log.debug("e in " + key, e);
}
}
};
return main;
});
HTML Email template file:
<!DOCTYPE html>
<html>
<head>
<style>
#tablecontent {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 35%;
}
#tablecontent td,
#tablecontent th {
border: 1px solid #ddd;
padding: 8px;
text-align: center;
}
#tablecontent tr:nth-child(even) {
background-color: #f2f2f2;
text-align: center;
}
#tablecontent tr:hover {
background-color: #ddd;
}
#tablecontent th {
padding-top: 12px;
padding-bottom: 12px;
text-align: center;
background-color: #3b5998;
color: white;
}
</style>
</head>
<body>
<h1><-replacewithtype-> - <-ReplaceWithRecord-></h1>
<h2>Items</h2>
<ol>
<!-- <li>Items1</li>
<li>Items2</li>
<li>Items3</li> -->
<-Replcaewithitems->
</ol>
<table id="tablecontent">
<tr>
<th>Sales Orders</th>
</tr>
<tr>
<!--<td><a href="<-Replace->"> <-ReplaceWithSOName-></a></td>-->
<-ReplacewithURL->
</tr>
</table>
</body>
</html>