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
*/
/************************************************************************************************
**Monthly Sales Notification for Sales Rep
*********************************************************************************************
*
* Author: Jobin And Jismi IT Services LLP
*
* Date Created : 07-April-2022
*
* Created By: VAIRAMUTHU, Jobin And Jismi IT Services LLP
*
* Description : Monthly Sales Notification for Sales Rep
*
* REVISION HISTORY
***********************************************************************************************/
define(['N/email', 'N/file', 'N/record', 'N/search'],
/**
* @param{email} email
* @param{file} file
* @param{record} record
* @param{search} search
*/
function(email, file, record, search)
{
const getInputData = (inputContext) =>
{
try
{
log.debug({title: 'getInputData', details: inputContext});
const salesorderSearch = search.create({
type: "salesorder",
filters:
[
["type", "anyof", "SalesOrd"], "AND",
["trandate", "within", "previousonemonth"], "AND",
["mainline","is","T"]
],
columns:
[
search.createColumn({name: "entityid", join: "customer", label: "Name"}),
search.createColumn({name: "internalid", join: "customer", label: "Internal ID"}),
search.createColumn({name: "email", join: "customer", label: "Email"}),
search.createColumn({name: "tranid", label: "Document Number"}),
search.createColumn({name: "amount", 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"}),
]
});
const searchResultCount = salesorderSearch.runPaged('').count;
log.debug({title:"salesorderSearch result count",details:searchResultCount});
return salesorderSearch;
}
catch(e)
{
log.debug({title:"error in getInputdata",details:e})
}
}
const map = (mapContext) =>
{
try
{
var srId;
log.debug({title:'mapContext',details:mapContext});
const data = JSON.parse(mapContext.value);
log.debug({title: 'data', details: data});
if (data.values.salesrep && data.values.salesrep.value !== '')
{
srId = data.values['email.salesRep'];
}
else
{
srId='admin';
}
mapContext.write({key: srId, value: data.values});
}
catch (error)
{
log.debug({title:"error in map",details:error})
}
}
function convertToCSV (objArray)
{
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
for (var i = 0; i < array.length; i++)
{
var line = '';
for (var index in array[i])
{
if (line !=='') line += ','
line += array[i][index];
}
str += line + '\r';
}
return str;
}
const reduce = (reduceContext) =>
{
try
{
log.debug({title:'ReduceContext',details:reduceContext});
const values = reduceContext.values.map(JSON.parse);
log.debug({title:"values", details:values});
const dataObj = JSON.stringify(values,['entityid.customer', 'email.customer', 'tranid','amount']);
log.debug({title:"Data obj", details:dataObj});
const csvContent = convertToCSV( dataObj);
log.debug ({title:"CSV Content",details:csvContent});
const csvFile = file.create({name: 'customer_so.csv', contents:'Customer_name,Customer_email,SalesOrder_No,Amount\r\n'+ csvContent, fileType: file.Type.CSV});
const salesrepId= reduceContext.key;
log.debug({title: 'salesrepId', details: salesrepId});
var idofReceiver , body ;
if (salesrepId.toString() ==='admin')
{
idofReceiver = -5;
body ='Sales order Details.This Customer has no sales rep,please add the sales rep';
}
else
{
idofReceiver =salesrepId ;
body ='Sales order Details';
}
email.send({
author: -5,
recipients:idofReceiver,
subject: 'Notification',
body: body,
attachments: [csvFile]
});
}
catch (err)
{
log.debug({title:"error in reduce",details:err})
}
}
const summarize = (summaryContext) =>
{
log.debug({title:'summarize',details:summaryContext})
}
return{getInputData, map, reduce, summarize};
});