Send a email notification to all Sales Rep once a month.
We need to send the customer sales information of the previous month to the corresponding Sales Rep.
This email notification should contain the Customer Name and Customer Email, Sales Order document Number, Sales Amount which is attached as a CSV File to the email.
If there is no Sales rep for the customer, such Customer’s Sales Information is send to a static NetSuite Admin, along with a message that to add Sales Rep for the corresponding customers
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
define(['N/email', 'N/file', 'N/search'],
/**
* @param{email} email
* @param{file} file
* @param{search} search
*/
(email, file, search) => {
const getInputData = (inputContext) => {
var customerSearchObj = search.create({
type: "customer",
filters:
[
["transaction.type","anyof","SalesOrd"],
"AND",
["transaction.trandate","within","thismonth"]
],
columns:
[
search.createColumn({name: "altname", label: "Name"}),
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({
name: "tranid",
join: "transaction",
label: "Document Number"
}),
search.createColumn({
name: "amount",
join: "transaction",
label: "Amount"
}),
search.createColumn({name: "salesrep", label: "Sales Rep"}),
search.createColumn({
name: "email",
join: "salesRep",
label: "Email"
}),
search.createColumn({
name: "internalid",
join: "salesRep",
label: "Internal ID"
})
]
});
return customerSearchObj;
}
const map = (mapContext) => {
log.debug({title: 'getMapData', details: mapContext})
var searchResult = JSON.parse(mapContext.value);
if (searchResult.values.salesrep && searchResult.values.salesrep.value !== '') {
salesRepId = searchResult.values['email.salesRep'];
}
else {
salesRepId='admin';
}
mapContext.write({
key: salesRepId,
value: searchResult.values
});
}
function convertToCSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
//var array=objArray;
var str = 'Name'+','+'Internal ID'+','+'Tran id'+','+'Amount'+','+'Sales rep'+','+'Sales Email'+','+'Sales ID'+',\n';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
array[i][index] = JSON.stringify(array[i][index]);
line += array[i][index];
}
str += line + '\r\n';
log.debug({title:"hlo",details:str});
}
return str;
}
const reduce = (reduceContext) => {
log.debug({title: 'getReduceData', details: reduceContext})
var output=[];
var salesRep=reduceContext.key;
var value=[];
value=reduceContext.values.map(JSON.parse)
if(salesRep.toString()!='admin') {
for (var k = 0; k < value.length; k++) {
var need = {
cusname: value[k].altname,
internal: value[k].internalid.value,
tranid: value[k]['tranid.transaction'],
amount: value[k]['amount.transaction'],
sales: value[k].salesrep.text,
email: value[k]['email.salesRep'],
saleIn: value[k]['internalid.salesRep'].value
}
output.push(need)
}
}
else{
for (var k = 0; k < value.length; k++) {
var need = {
cusname: value[k].altname,
internal: value[k].internalid.value,
tranid: value[k]['tranid.transaction'],
amount: value[k]['amount.transaction'],
sales: '',
email: '',
saleIn: ''
}
output.push(need)
}
}
log.debug({title:'det',details:value})
var csv = convertToCSV(output);
try {
log.debug("Inside try block")
var fileObj = file.create({
name: salesRep.toString()+'.csv',
fileType: file.Type.CSV,
folder: 653,
contents: csv
});
var file_Id=fileObj.save();
log.debug({title:"file_id",details:file_Id})
var body='';
if(salesRep.toString()=='admin'){
body="add sales rep to customers";
}
else {
body="information of sales order"
}
email.send({
author: 'athul',
recipient: [salesRep],
subject: 'Previous Month Sales Information',
body: body,
attach: [file_Id]
});
// log.debug("mail send")
}catch (e){
log.debug("nothing working")
}
}
const summarize = (summaryContext) => {
log.debug({title: 'getSummarizeData', details: summaryContext})
}
return {getInputData, map, reduce, summarize}
});