Jira Code: UMAR-136
Creating an email widget using the parameters arrived via URL. An email message has been creating using the redirect module and setting the transaction(id) in the email widget to automatically attach the transaction in the corresponding transaction record. Setting the email template in the email by creating it as a pdf using the rendering.
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
*/
define(['N/redirect', 'N/record', 'N/email', 'N/render', 'N/config', 'N/search', 'N/file'],
function (redirect, record, email, render, config, search, file) {
/**
* Definition of the Suitelet script trigger point.
*
* @param {Object}
* context
* @param {ServerRequest}
* context.request - Encapsulation of the incoming request
* @param {ServerResponse}
* context.response - Encapsulation of the Suitelet response
* @Since 2015.2
*/
function onRequest(context) {
try {
// Getting the record id
var recId = context.request.parameters.recId;
var CurrId = context.request.parameters.CurrId;
var custId = context.request.parameters.custId;
var custcont = context.request.parameters.custcont;
var custcontcc = context.request.parameters.custcontcc;
var customerId = context.request.parameters.custemailId;
var customerContCC = context.request.parameters.custemailIdCC;
var sendStatus = context.request.parameters.sendStatus;
var vendorId = context.request.parameters.contact;
var vendorContCC = context.request.parameters.contactCC;
var temp = context.request.parameters.temp;
var usage = context.request.parameters.usage;
var subType = context.request.parameters.subType;
var pdfNm = null;
if (sendStatus == 1) { //sending quotation from quotation record using button 'Quotation'
pdfNm = 'Quotation';
var fileId = createDocumentFile(recId, 109, pdfNm);
// Redirect to a Email message
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
transaction: recId,
l: 'T',
// iscustom : 't',
usage: 'SendQuotation',
// recepient: customerId,
ccbcclist: customerContCC,
templatetype: 'EMAIL',
entity: customerId,
//subject : subject,
template: temp,
testId: recId,
mediaitem: fileId,
sjType: subType
}
});
} else if (sendStatus == 2) { //send PO from purchase order using the button 'Send PO'
var fileId;
if (CurrId == 1) {
pdfNm = "PurchaseOrder_Parts";
fileId = createDocumentFileForPurchaseOrder(recId, 114, pdfNm);
}
// else {
// pdfNm = "Commission_Invoice";
// fileId = createDocumentFileForInvoice(recId, 123, pdfNm);
// }
log.debug("send po", fileId);
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
transaction: recId,
l: 'T',
// iscustom : 't',
usage: 'SendPO',
templatetype: 'EMAIL',
entity: vendorId,
ccbcclist: vendorContCC,
template: temp,
testId: recId,
sjType: CurrId,//subjob type when send PO.
mediaitem: fileId,
}
});
} else if (sendStatus == 3) {//send dispatch details
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
record: recId,
recordtype: 184,
l: 'T',
// iscustom : 't',
usage: 'Dispatch',
templatetype: 'EMAIL',
entity: custcont,
ccbcclist: custcontcc,
template: 29,
testId: recId
}
});
} else if (sendStatus == 4) {//send delivery instructions
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
record: recId,
recordtype: 184,
l: 'T',
// iscustom : 't',
usage: 'deliveryinstructions',
templatetype: 'EMAIL',
entity: vendorId,
ccbcclist: vendorContCC,
template: temp,
testId: recId
}
});
} else if (sendStatus == 5) {
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
record: recId,
recordtype: 184,
l: 'T',
// iscustom : 't',
usage: 'RequestDeliveryinstructions',
templatetype: 'EMAIL',
entity: custcont,
ccbcclist: custcontcc,
template: temp,
testId: recId
}
});
} else if (sendStatus == 6) {
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
transaction: recId,
l: 'T',
// iscustom : 't',
usage: 'deliveryinstructions',
templatetype: 'EMAIL',
entity: custId,
// ccbcclist: contactCC,
template: 12,
testId: recId
}
});
} else if (sendStatus == 7) {
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
record: recId,
recordtype: 184,
l: 'T',
// iscustom : 't',
usage: 'requestfrReadiness',
templatetype: 'EMAIL',
entity: vendorId,
ccbcclist: vendorContCC,
template: temp,
testId: recId
}
});
} else if (sendStatus == 8) {
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
record: recId,
recordtype: 184,
l: 'T',
// iscustom : 't',
usage: 'RequestDespatchDetails',
templatetype: 'EMAIL',
entity: vendorId,
ccbcclist: vendorContCC,
template: temp,
testId: recId
}
});
} else if (sendStatus == 9) { //PO proforma invoice
log.debug('CurrId', CurrId);
var tempalate = 120;
var name = 'Proforma_invoice';
var fileId = createproformaInvDocumentFile_Subjob(CurrId, tempalate, name);
/*var fileId=createDocumentFile(recId,tempalate);
log.debug({
title : 'fileId',
details : fileId
});*/
// Redirect to a Email message
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
transaction: recId,
l: 'T',
// iscustom : 't',
usage: 'proformaInvoice_PO',
templatetype: 'EMAIL',
entity: vendorId,
ccbcclist: vendorContCC,
mediaitem: fileId,
template: 39,// sandbox 34 while porduction 39
testId: recId
}
});
} else if (sendStatus == 10) { //SEND proforma from sales order
var tempalate = 126; // 120 in sandbox while 126 in production
pdfNm = 'Invoice_for_Prepayment';
// var fileId = createDocumentFile(recId, tempalate, pdfNm);
var fileId = createproformaInvDocumentFile_Subjob(recId, tempalate, pdfNm)
log.debug({
title: 'fileId',
details: fileId
});
// Redirect to a Email message
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
transaction: recId,
l: 'T',
// iscustom : 't',
usage: 'Sendproforma',
// recepient: customerId,
ccbcclist: customerContCC,
templatetype: 'EMAIL',
entity: customerId,
template: 34,
mediaitem: fileId,
testId: recId
}
});
} else if (sendStatus == 11) {//send customer confirmation PO.
var tempalate = 121;
var name = 'Order Confirmation';
//creating send confirmation PO(parts) using SO template.
var fileId = createproformaInvDocumentFile_Subjob(CurrId, tempalate, name);
// Redirect to a Email message
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
transaction: recId,
l: 'T',
// iscustom : 't',
usage: 'send_customer_confirmation',
templatetype: 'EMAIL',
entity: custcont,
ccbcclist: custcontcc,
template: temp,
testId: recId,
sjType: subType,
mediaitem: fileId
}
});
} else if (sendStatus == 12) {
/* var tempalate=119;
var fileId=(recId,tempalate);
log.debug({
title : 'fileId',
details : fileId
});
*/
// Redirect to a Email message
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
// record : recId,
transaction: recId,
l: 'T',
// iscustom : 't',
usage: 'RequestDeliveryinstructions',
templatetype: 'EMAIL',
entity: custcont,
ccbcclist: custcontcc,
template: 12,
testId: recId
}
});
} else if (sendStatus == 13) { //PO packing list
var tempalate = 115;
var fileId = createpackingListDocumentFile_Subjob(CurrId, tempalate);
// Redirect to a Email message
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
transaction: recId,
l: 'T',
// iscustom : 't',
usage: 'po_packingList',
templatetype: 'EMAIL',
entity: vendorId,
ccbcclist: vendorContCC,
template: 32,
testId: recId,
mediaitem: fileId
}
});
} else if (sendStatus == 14) { //PROFORMA INVOICE FROM SUBJOB
var tempalate = 128; //120 in sandbox while 128 in production
var name = 'Proforma_invoice';
var fileId = createproformaInvDocumentFile_Subjob(recId, tempalate, name);
log.debug({
title: 'fileId',
details: fileId
});
// Redirect to a Email message
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
record: CurrId,
recordtype: 184,
// transaction: recId,
l: 'T',
// iscustom : 't',
usage: 'Sendproforma_subjob',
// recepient: customerId,
ccbcclist: customerContCC,
templatetype: 'EMAIL',
entity: customerId,
template: 39, // 34 in sandbox while 39 in production
mediaitem: fileId,
testId: recId
}
});
} else if (sendStatus == 15) { //PACKING LIST FROM SUBJOB
var tempalate = 127; // 115 in sandbox while 127 in production
var fileId = createpackingListDocumentFile_Subjob(recId, tempalate);
log.debug({
title: 'fileId',
details: fileId
});
// Redirect to a Email message
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
record: CurrId,
recordtype: 184,
l: 'T',
// iscustom : 't',
usage: 'Sendpacking_subjob',
// recepient: customerId,
ccbcclist: customerContCC,
templatetype: 'EMAIL',
entity: customerId,
template: 39, // 32 in sandbox while 39 in production
mediaitem: fileId,
testId: recId
}
});
} else if (sendStatus == 16) {
log.debug("1", "Im at send status");
log.debug("usage", usage);
var fileId;
if (usage == "send_salesInvoice") {
pdfNm = "Sales_Invoice";
fileId = createDocumentFileForInvoice(recId, 117, pdfNm);
}
else {
pdfNm = "Commission_Invoice";
fileId = createDocumentFileForInvoice(recId, 123, pdfNm);
}
log.debug("fileId", fileId);
// Redirect to a Email message
redirect.toRecord({
type: record.Type.MESSAGE,
id: null,
parameters: {
transaction: recId,
mediaitem: fileId,
l: 'T',
// iscustom : 't',
usage: usage,
templatetype: 'EMAIL',
entity: custcont,
ccbcclist: custcontcc,
template: temp,
testId: recId,
}
});
}
} catch (e) {
log.debug({
title: e.name,
details: e.message
});
}
}
return {
onRequest: onRequest
};
/*
* Function to set attachment
*/
function createDocumentFile(recId, tempalate, pdfNm) {
try {
//render The Quote to End user Form
var renderer = render.create();
var subsidiaryDtls = search.lookupFields({
type: search.Type.ESTIMATE,
id: recId,
columns: ['subsidiary']
});
var subsidiaryId = subsidiaryDtls.subsidiary[0].value;
log.debug('subsidiaryId', subsidiaryId);
renderer.setTemplateById(tempalate);
var companyInfo = config.load({
type: config.Type.COMPANY_INFORMATION
});
renderer.addRecord({
templateName: 'record',
record: record.load({
type: record.Type.ESTIMATE,
id: recId
})
});
renderer.addRecord({
templateName: 'subsidiary',
record: record.load({
type: record.Type.SUBSIDIARY,
id: subsidiaryId
})
});
//<#if subsidiary.logo?length != 0><img src="${subsidiary.logo@url}" style="width: 200px; height: 80px;float: left; margin: 7px" /> <#else> <#if companyInformation.logoUrl?length != 0> <img src="${companyInformation.logoUrl}" style="width: 80px; height: 80px;float: left; margin: 7px" /> </#if> </#if><p style="font-size: 10pt;">${subsidiary.name}<br />${subsidiary.mainaddress_text}</p>
/* renderer.addRecord({
templateName: 'companyInformation',
record: companyInfo
});
*/
/* renderer = renderer.transaction({
entityId: 3424
});*/
log.debug({
title: "companyInfo",
details: companyInfo
});
var endUserForm = renderer.renderAsPdf();
endUserForm.folder = 7862;
//endUserForm.name = 'Proforma_invoice' + recId + '.pdf';
endUserForm.name = pdfNm + recId + '.pdf';
var fileId = endUserForm.save();
return fileId;
/* return {
size: endUserForm.size,
fileId: endUserForm.save()
}*/
} catch (e) {
log.debug({
title: e.name,
details: e.message
});
}
}
function createDocumentFileForInvoice(recId, tempalate, pdfNm) {
try {
log.debug('recId', recId);
var subsidiaryDtls = search.lookupFields({
type: search.Type.INVOICE,
id: recId,
columns: ['subsidiary']
});
var subsidiaryId = subsidiaryDtls.subsidiary[0].value;
var objRecord = record.load({
type: record.Type.SUBSIDIARY,
id: subsidiaryId,
isDynamic: false
});
log.debug('subsidiaryId', subsidiaryId);
var fileID = objRecord.getValue({
fieldId: 'logo'
});
var fileObj = file.load({
id: fileID
});
log.debug("fileObj", fileObj);
var url = fileObj.url;
url = url.replace(/&/g, '&');
log.debug('fileObj.url', url);
var renderer = render.create();
renderer.setTemplateById(tempalate);
var companyInfo = config.load({
type: config.Type.COMPANY_INFORMATION
});
var invoice = record.load({
type: record.Type.INVOICE,
id: recId
});
log.debug("invoice", invoice);
renderer.addRecord({
templateName: 'record',
record: invoice
});
renderer.addRecord({
templateName: 'companyInformation',
record: companyInfo
});
renderer.addCustomDataSource({
format: render.DataSource.JSON,
alias: "subsidiary",
data: JSON.stringify({ logo: url })
});
var endUserForm = renderer.renderAsPdf();
log.debug("end user form", endUserForm);
endUserForm.folder = 7862;
endUserForm.name = pdfNm + '_' + recId + '.pdf';
log.debug(endUserForm.name);
var fileId = endUserForm.save();
log.debug(fileId);
return fileId;
}
catch (e) {
log.debug("Exception in create document in invoice", e)
}
}
/*
* Function to set attachment proforma invoice
*/
function createproformaInvDocumentFile_Subjob(recId, tempalate, names) {
try {
var subsidiaryDtls = search.lookupFields({
type: search.Type.SALES_ORDER,
id: recId,
columns: ['subsidiary']
});
var subsidiaryId = subsidiaryDtls.subsidiary[0].value;
//render The Quote to End user Form
var renderer = render.create();
renderer.setTemplateById(tempalate);
var companyInfo = config.load({
type: config.Type.COMPANY_INFORMATION
});
renderer.addRecord({
templateName: 'record',
record: record.load({
type: record.Type.SALES_ORDER,
id: recId
})
});
renderer.addRecord({
templateName: 'subsidiary',
record: record.load({
type: record.Type.SUBSIDIARY,
id: subsidiaryId
})
});
renderer.addRecord({
templateName: 'companyInformation',
record: companyInfo
});
/* renderer = renderer.transaction({
entityId: 3424
});*/
log.debug({
title: "companyInfo",
details: companyInfo
});
var endUserForm = renderer.renderAsPdf();
endUserForm.folder = 7862;
// endUserForm.name = 'Proforma_invoice_SO' + recId + '.pdf';
log.debug("end user form2", endUserForm);
endUserForm.name = names + '_' + recId + '.pdf';
var fileId = endUserForm.save();
log.debug(fileId);
return fileId;
/* return {
size: endUserForm.size,
fileId: endUserForm.save()
}*/
} catch (e) {
log.debug({
title: e.name,
details: e.message
});
}
}
/*
* Function to set attachment packing list
*/
function createpackingListDocumentFile_Subjob(recId, tempalate) {
try {
/*var transactionFile = render.packingSlip({
entityId: parseInt(recId),
printMode: render.PrintMode.HTML,
inCustLocale: true
});*/
var subsidiaryDtls = search.lookupFields({
type: search.Type.SALES_ORDER,
id: recId,
columns: ['subsidiary']
});
var subsidiaryId = subsidiaryDtls.subsidiary[0].value;
var objRecord = record.load({
type: record.Type.SUBSIDIARY,
id: subsidiaryId,
isDynamic: false
});
var fileID = objRecord.getValue({
fieldId: 'logo'
});
var fileObj = file.load({
id: fileID
});
var url = fileObj.url;
url = url.replace(/&/g, '&')
log.debug('fileObj.url', url);
//render The Quote to End user Form
var renderer = render.create();
renderer.setTemplateById(tempalate);
var companyInfo = config.load({
type: config.Type.COMPANY_INFORMATION
});
renderer.addRecord({
templateName: 'record',
record: record.load({
type: record.Type.SALES_ORDER,
id: recId
})
});
renderer.addRecord({
templateName: 'companyInformation',
record: companyInfo
});
renderer.addCustomDataSource({
format: render.DataSource.JSON,
alias: "subsidiary",
data: JSON.stringify({ logo: url })
});
var endUserForm = renderer.renderAsPdf();
endUserForm.folder = 7862;
endUserForm.name = 'Packing_List_SO' + recId + '.pdf';
var fileId = endUserForm.save();
return fileId;
} catch (e) {
log.debug({
title: e.name,
details: e.message
});
}
}
function createDocumentFileForPurchaseOrder(recId, tempalate, pdfNm) {
try {
log.debug('recId', recId);
var subsidiaryDtls = search.lookupFields({
type: search.Type.PURCHASE_ORDER,
id: recId,
columns: ['subsidiary']
});
var subsidiaryId = subsidiaryDtls.subsidiary[0].value;
var objRecord = record.load({
type: record.Type.SUBSIDIARY,
id: subsidiaryId,
isDynamic: false
});
log.debug('subsidiaryId', subsidiaryId);
var fileID = objRecord.getValue({
fieldId: 'logo'
});
var fileObj = file.load({
id: fileID
});
log.debug("fileObj", fileObj);
var url = fileObj.url;
url = url.replace(/&/g, '&');
log.debug('fileObj.url', url);
var renderer = render.create();
renderer.setTemplateById(tempalate);
var companyInfo = config.load({
type: config.Type.COMPANY_INFORMATION
});
var purchaseOrder = record.load({
type: record.Type.PURCHASE_ORDER,
id: recId
});
log.debug("purchase order", purchaseOrder);
renderer.addRecord({
templateName: 'record',
record: purchaseOrder
});
renderer.addRecord({
templateName: 'companyInformation',
record: companyInfo
});
renderer.addCustomDataSource({
format: render.DataSource.JSON,
alias: "subsidiary",
data: JSON.stringify({ logo: url })
});
var endUserForm = renderer.renderAsPdf();
log.debug("end user form", endUserForm);
endUserForm.folder = 7862;
endUserForm.name = pdfNm + '_' + recId + '.pdf';
log.debug(endUserForm.name);
var fileId = endUserForm.save();
log.debug(fileId);
return fileId;
}
catch (e) {
log.debug("Exception in create document in invoice", e)
}
}
});
RELATED SCRIPT
Custom Email Widget on custom button actionhttp://www.jobinandjismi.in/2019/04/10/custom-email-widget-on-custom-button-action/