The N/RENDER module in NetSuite SuiteScript is used for rendering dynamic content, particularly for creating PDF files and HTML templates. It allows developers to generate customized documents that are based on data from NetSuite records, providing an automated way to produce reports, invoices, purchase orders, and other documents in a dynamic, flexible format.
This module is especially useful when you need to create sophisticated documents, with data pulled directly from NetSuite, and convert them into PDF or HTML formats.
Key Functions of N/RENDER
- Rendering PDF Files
- The
render()function in the N/RENDER module is commonly used for generating PDF files. This function allows you to load an HTML template, fill it with dynamic data, and then convert the HTML into a PDF. - Here’s a simple example where data is fetched from a sales order and a PDF is rendered:
define(['N/render', 'N/record'], function(render, record) {
function generatePDF() {
var salesOrder = record.load({
type: record.Type.SALES_ORDER,
id: 12345
});
var renderer = render.create();
renderer.setTemplateByScriptId({
scriptId: 'customscript_my_template', // Script ID of the template
});
renderer.addRecord('record', salesOrder); // Add sales order data to the template
var pdfFile = renderer.renderAsPdf(); // Generate PDF
log.debug('Generated PDF', pdfFile);
}
return {
execute: generatePDF
};
});
Explanation:
- We load a sales order record using the
record.load()function. - We create a
renderobject and set the template with thesetTemplateByScriptId()method. This template is an HTML-based FreeMarker template stored in NetSuite. - The
addRecord()method adds the loaded record data into the template, and finally, therenderAsPdf()method converts the HTML content into a PDF.
Rendering HTML Files
The N/RENDER module can also be used to render HTML content. It is often used in Suitelet scripts to generate HTML content that will be returned to the browser.
Example of rendering an HTML page:
define(['N/render'], function(render) {
function renderHTML() {
var renderer = render.create();
renderer.setTemplateByScriptId({
scriptId: 'customscript_my_html_template'
});
var htmlContent = renderer.renderAsString(); // Render HTML as string
log.debug('Rendered HTML', htmlContent);
}
return {
execute: renderHTML
};
});
Explanation:
- Similar to PDF rendering, this example loads an HTML template via the
setTemplateByScriptId()method. - The difference here is that instead of generating a PDF, the
renderAsString()method is used to output the content as a raw HTML string.
Working with Template Variables
The templates used by N/RENDER are typically FreeMarker templates. You can pass dynamic variables to the template using addRecord(), addField(), or addCustomRecord(). These variables can be used in the FreeMarker template to dynamically render content.
Here’s how you can add custom variables to the template:
define(['N/render'], function(render) {
function renderWithCustomData() {
var renderer = render.create();
renderer.setTemplateByScriptId({
scriptId: 'customscript_my_template'
});
// Adding custom variables to the template
renderer.addField({
name: 'customerName',
value: 'John Doe'
});
renderer.addField({
name: 'orderDate',
value: '2025-01-13'
});
var pdfFile = renderer.renderAsPdf(); // Generate PDF with custom fields
log.debug('Generated PDF with custom fields', pdfFile);
}
return {
execute: renderWithCustomData
};
});
Explanation:
- This example adds custom variables (
customerName,orderDate) using theaddField()method. - These fields can be accessed within the FreeMarker template using the respective field names.
Saving the Rendered File
Once the PDF or HTML file is generated, it can be saved in the File Cabinet for later retrieval or sent as an email attachment. Here’s how to save the PDF file:
define(['N/render', 'N/file'], function(render, file) {
function savePDF() {
var renderer = render.create();
renderer.setTemplateByScriptId({
scriptId: 'customscript_my_pdf_template'
});
var pdfFile = renderer.renderAsPdf();
var fileObj = file.create({
name: 'sales_order_12345.pdf',
fileType: file.Type.PDF,
contents: pdfFile.getContents(),
folder: 123 // Folder ID in File Cabinet
});
var fileId = fileObj.save();
log.debug('PDF saved', 'File ID: ' + fileId);
}
return {
execute: savePDF
};
});
Explanation:
- After rendering the PDF, the file is created using the
file.create()method. - The file is saved to the File Cabinet by calling
save().
Considerations When Using N/RENDER
- Template Management: Ensure your templates are properly managed and versioned in NetSuite. Using the correct template IDs is crucial for consistent results.
- Performance: Rendering large documents can be resource-intensive. For complex templates, consider optimizing the FreeMarker code to reduce processing time.
- File Size: Large generated PDFs can be bulky. Consider compressing files or splitting them into multiple documents if needed.
Conclusion
The N/RENDER module is a powerful tool for rendering dynamic content in NetSuite. It helps developers generate custom PDFs, HTML reports, and other documents by leveraging FreeMarker templates and adding data from NetSuite records. Whether you’re automating invoice creation, custom reports, or any other type of document generation, this module allows you to create sophisticated, dynamic documents within SuiteScript.