shea brennan
11/21/2024, 3:54 PMAdam Weaver
11/21/2024, 6:31 PMFred Pope
11/25/2024, 4:32 PM/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
define(['N/render', 'N/email', 'N/file', 'N/record', 'N/runtime'],
function(render, email, file, record, runtime) {
function onRequest(context) {
if (context.request.method !== 'GET') return;
try {
const recordId = context.request.parameters.recordId;
const recordType = context.request.parameters.recordType;
// Load and render PDF
const template = render.create();
template.setTemplateByScriptId('CUSTTMPL_YOUR_TEMPLATE_HERE');
template.addRecord('record', record.load({
type: recordType,
id: recordId
}));
const pdfFile = template.renderAsPdf();
// Save PDF as file
const fileId = pdfFile.save({
folder: YOUR_FOLDER_ID,
fileName: `${recordType}_${recordId}.pdf`
});
// Send email
email.send({
author: runtime.getCurrentUser().id,
recipients: ['<mailto:recipient@example.com|recipient@example.com>'],
subject: 'Transaction PDF',
body: 'Please find the transaction PDF attached.',
attachments: [file.load({ id: fileId })]
});
// Update record
record.submitFields({
type: recordType,
id: recordId,
values: {
'custbody_pdf_sent': true,
'custbody_pdf_sent_date': new Date()
}
});
context.response.write('PDF generated and sent successfully');
} catch (e) {
log.error('Error in PDF generation suitelet', e);
context.response.write('Error: ' + e.message);
}
}
return {
onRequest: onRequest
};
});
shea brennan
11/26/2024, 2:25 PMFred Pope
12/09/2024, 6:32 PM