Requirement
Add a button (labelled “Send email “) in the Bill payment record. On the button click the Payment Voucher should be emailed to the supplier. The recipient email address can be taken from the supplier record. The payment voucher is the print template in Bill payment.
Solution
Button created on bill payment record. on Button click a mail should be send to the vendor and a secondary recipient on the bill payment record (Fetch from a custom field) through suitlet call.
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define([
'N/record',
'N/error',
'N/ui/serverWidget',
'N/url',
'N/runtime',
'N/search'
],
/**
* @param{record} record
*/
(record,error,serverWidget,url,runtime,search) => {
/**
* Defines the function definition that is executed before record is loaded.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
* @param {Form} scriptContext.form - Current form
* @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
* @since 2015.2
*/
const beforeLoad = (scriptContext) => {
if (scriptContext.type == 'view') {
try{
var newRecord = scriptContext.newRecord;
var type = newRecord.type;
var form = scriptContext.form;
var TestUpdatePrice = form.addButton
({
id: "custpage_sendemail",
label: "Send Email",
functionName: "onButtonClick"
});
form.clientScriptFileId = 340233;
}catch (err) {
log.error({title: "Error in Update Price", details: err.message});
}
}
}
return {beforeLoad}
});
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/currentRecord', 'N/https', 'N/search','N/url'],
function(currentRecord, https,search, url) {
/**
* Function to be executed after page is initialized.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
*
* @since 2015.2
*/
function pageInit(scriptContext) {}
function onButtonClick() {
var rec = currentRecord.get();
var recordId = Number(rec.id);
var recordType = rec.type;
var suitletUrl=url.resolveScript({
scriptId: 'customscript860',
deploymentId: 'customdeploy1',
params:{
recid:recordId
}
});
https.get({ url: suitletUrl})
location.reload();
}
return {
pageInit:pageInit,
onButtonClick: onButtonClick
};
});
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
define(['N/email', 'N/record', 'N/render', 'N/search','N/encode', 'N/file', 'N/redirect', 'N/ui/message'],
/**
* @param{email} email
*/
(email, record, render, search, encode, file, redirect, message) => {
function sendEmailToVendor(id,emailToSend,vendorId,secondaryRecipient){
try{
log.debug("**START EMAILING**");
var subject = "Payment Voucher"//create body "please find the attached payment voucher"
var body ='Hi,'
+ '<br>'
+ '<br>'
+ 'Please see the attached payment voucher.'
+ '<br>'
+ '<br>'
var File = render.transaction({
entityId: parseInt(id),
printMode: render.PrintMode.PDF,
inCustLocale: true
});
File.name = 'Payment Voucher';
if(secondaryRecipient){
email.send({
author:18359,//Surya vineeth ToDo change ID.
recipients:emailToSend,secondaryRecipient,
subject: subject,
body: body,
attachments: [File],
relatedRecords: {
transactionId: parseInt(id),
entityId: vendorId
}
});
}else{
email.send({
author:18359,
recipients:emailToSend,
subject: subject,
body: body,
attachments: [File],
relatedRecords: {
transactionId: parseInt(id),
entityId: vendorId
}
});
}
}catch (e) {
log.debug("error@sendEmailToVendor",e)
}
}
/**
* Defines the Suitelet script trigger point.
* @param {Object} scriptContext
* @param {ServerRequest} scriptContext.request - Incoming request
* @param {ServerResponse} scriptContext.response - Suitelet response
* @since 2015.2
*/
const onRequest = (scriptContext) => {
try{
var id = scriptContext.request.parameters.recid
var objRec = record.load({id: id, type: 'vendorpayment'});
var vendorId = objRec.getValue({fieldId:"entity"});
var secondaryRecipient = objRec.getValue({fieldId:"custbody23"});
if(vendorId){
//create search in vendor record to fetch the email ID
var fieldLookUp = search.lookupFields({
type: search.Type.VENDOR,
id: vendorId,
columns: ['email', 'companyname']
});
var emailToSend = fieldLookUp.email;
var vendorName = fieldLookUp.companyname;
if(emailToSend!==''){
sendEmailToVendor(id,emailToSend,vendorId,secondaryRecipient);
}
}
}catch (e) {
log.debug("error@onrequest",e);
}
}
return {onRequest}
});