/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
* @NModuleScope SameAccount
*/
define(["N/file","N/search","N/email"],
function(file, search, email) {
/**
* Definition of the Scheduled script trigger point.
*
* @param {Object} scriptContext
* @param {string} scriptContext.type - The context in which the script is executed. It is one of the values from the scriptContext.InvocationType enum.
* @Since 2015.2
*/
function execute(scriptContext) {
var columns = new Array();
var htmlString = '';
columns[0] = search.createColumn({name: "internalid", label: "Internal ID"});
columns[1] = search.createColumn({name: "item", label: "Item"});
columns[2] = search.createColumn({name: "quantity", label: "Quantity"})
columns[3] = search.createColumn({name: "quantityshiprecv", label: "Quantity Fulfilled/Received"});
columns[4] = search.createColumn({
name: "formulanumeric",
formula: "{quantity}-{quantityshiprecv}",
label: "Quantity to be Fulfilled"
})
for(var i = 0; i < columns.length;i++){
if(i > 0 && i != columns.length-1){
log.debug("1")
htmlString += "<td><b>" + columns[i].label +"</b></td>"
} else if(i == columns.length-1){
log.debug("2")
htmlString += "<td><b>" + columns[i].label +"</b></td><tr>"
}
else if(i == 0){
log.debug("3")
htmlString += "<tr><td><b>" + columns[i].label +"</b></td>"
}
}
var salesOrdSearch = search.create({
type: "salesorder",
filters:
[
["type","anyof","SalesOrd"],
"AND",
["mainline","is","F"],
"AND",
["taxline","is","F"],
"AND",
["item.type","anyof","Assembly","InvtPart","Group","Kit","NonInvtPart","Service"],
"AND",
["status","anyof","SalesOrd:D","SalesOrd:E","SalesOrd:B"]
],
columns: columns
});
var searchResultCount = salesOrdSearch.runPaged().count;
var resultSet = salesOrdSearch.run()
resultSet.each(function(result){
for (var i = 0; i < salesOrdSearch.columns.length;i++){
var searchResult = result.getText({
name: salesOrdSearch.columns[i].name
});
if (searchResult == null){
var searchResult = result.getValue({
name: salesOrdSearch.columns[i].name
});
}
if(i > 0 && i != columns.length-1){
htmlString += '<td>' + searchResult +"</b></td>"
} else if(i == 0){
htmlString += "<tr><td>" + searchResult +"</b></td>"
}else {
htmlString += "<td>" + searchResult +"</b></td><tr>"
}
}
return true;
});
var recipientsID = [112042];
email.send({
author: 112042,
recipients: [112042],
subject: 'Saved Search in Tabular Form with HTML',
body: '<table border=0>'+htmlString+"</table>"
});
log.debug("Completed");
}
return {
execute: execute
};
});