Jira Code: FHI-11
This script is to send an email from donotreply@fully.com to each customer who has not received the PDF of the transaction document. Once the mail is sent, the custom checkbox field is checked to indicate that the email is sent.
Scheduled script: FHI-11 JJ SS Email Notification
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
* @NModuleScope SameAccount
*/
/*******************************************************************************
* CLIENTNAME:Fully
* FHI-11
* Invoice Reminder
* **************************************************************************
* Date : 25-03-2019
*
* Author: Jobin & Jismi IT Services LLP
* Script Description : This script is to send email notification with attached pdf print once the transaction is
* created.
* Date created : 25-03-2019
*
* REVISION HISTORY
*
* Revision 1.0 ${25-03-2019} nd : created
*
*
******************************************************************************/
define(['N/search','N/email','N/render','N/record','N/runtime'],
function(search,email,render,record,runtime) {
//function to reschedule the script.
function rescheduleScriptandReturn(startRange, endRange, n) {
try {
//endrange is the last range of the just finished search.
//Next search range when rescheduled will start from this endRange
var mrTask = task.create({
taskType: task.TaskType.SCHEDULED_SCRIPT,
scriptId: "customscript_fhi11_jj_ss_emailnotificati",
deploymentId: "customdeploy_fhi11_jj_ss_emailnotificati",
params: {
custscript_startrange: startRange,
custscript_endrange: endRange,
custscript_tot_count: n
}
});
var scriptTaskId = mrTask.submit();
} catch (err) {
log.error({
title: 'error on rescheduleScriptandReturn',
details: err
});
}
}
/**
* 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) {
try{
//get the current script
var scriptObj = runtime.getCurrentScript();
////get the script parameter startRange
var startRange = scriptObj.getParameter({ name: 'custscript_jj_startrange' });
var endRange = scriptObj.getParameter({ name: 'custscript_jj_endrange' });
var n = scriptObj.getParameter({ name: 'custscript_jj_total_count' });
if (startRange == '' || startRange == null) {
n = 0;
}
var transactionSearchObj = search.load({
id: 'customsearch_transaction_document_sent'
});
var searchResultCount1 = transactionSearchObj.runPaged().count;
var end=0;
//when script initially run startrange will be blank so set startrange as 0.
if (startRange == '' || startRange == null) {
if(searchResultCount1 < 100)
{
startRange = 0;
endRange = searchResultCount1;
end=1;
}
else
{
startRange = 0;
endRange = 100;
}
}
startRange = parseInt(startRange);
endRange = parseInt(endRange);
var result;
result = transactionSearchObj.run().getRange({
start: startRange,
end: endRange
});
for (var i = 0; i<result.length;i++) {
var customer_email = result[i].getValue({
name: "email"
});
var transaction_id = result[i].getValue({
name: "internalid"
});
var tran_type = result[i].getValue({
name: "recordtype"
});
var customer_id = result[i].getValue({
name: "internalid",
join: "customer"
});
//to render the pdf of the transaction
var renderer = render.create();
var transactionFile = render.transaction({
entityId: parseInt(transaction_id),
printMode: render.PrintMode.PDF,
inCustLocale: true
});
var mergeResult = render.mergeEmail({
templateId: 27,
transactionId: parseInt(transaction_id),
entity: {
type: 'customer',
id: parseInt(customer_id)
}
});
var subject = mergeResult.subject;
email.send({
author: 4920725,//the id of employee with email id 'donotreply@fully.com'
recipients: customer_email,
subject:subject,
body: mergeResult.body,
attachments: [transactionFile]
});
//to set the custom field 'TRANSACTION EMAIL SENT'
var id = record.submitFields({
type:tran_type,
id: parseInt(transaction_id),
values: {
custbody_transaction_email : true
}
});
}
var Countt = searchResultCount1 / 100;
var Count = Math.floor(Countt);
if(end!=1)
{
//rescheduling the script based on the count
if (n < Count - 1) {
n++;
rescheduleScriptandReturn(startRange + 100, endRange + 100, n);
} else if (n == Count - 1) {
n++;
var lastCount = parseFloat(searchResultCount1) - parseFloat(endRange);
rescheduleScriptandReturn(startRange + 100, endRange + lastCount, n);
} else {
}
}
}catch(er){
log.error('error @ execute',er)
}
}
return {
execute: execute
};
});