TemplateRenderer Object Members in SuiteScript for Custom
The TemplateRenderer object enables advanced PDF and HTML form generation in SuiteScript, enhancing custom template functionalities.
The TemplateRenderer object is a key component of the N/render module in SuiteScript, which facilitates the creation of dynamic PDF and HTML documents from templates. This functionality is crucial for developers looking to customize reports, invoices, and other documents within NetSuite.
What Is the TemplateRenderer Object?
The TemplateRenderer object encapsulates capabilities for generating forms using advanced templates. It provides methods to add various data sources, including JSON, XML, queries, and records, simplifying the rendering process for different document types.
Key Methods of the TemplateRenderer Object
Below are the prominent methods associated with the TemplateRenderer object:
| Method Name | Return Type | Description |
|---|---|---|
TemplateRenderer.addCustomDataSource(options) | void | Adds a custom XML file or JSON object as a data source for the template. |
TemplateRenderer.addQuery(options) | void | Uses a Query as the data source for the renderer. |
TemplateRenderer.addRecord(options) | void | Binds a specific record to a template variable. |
TemplateRenderer.addSearchResults(options) | void | Binds results from a search to a template variable. |
TemplateRenderer.renderAsPdf() | Object | Produces a PDF document from the advanced template. |
TemplateRenderer.renderPdfToResponse(options) | void | Sends the generated PDF content as a response. |
TemplateRenderer.renderAsString() | string | Returns the content of the template as a string. |
TemplateRenderer.setTemplateById(options) | void | Specifies the template using its internal ID. |
TemplateRenderer.setTemplateByScriptId(options) | void | Specifies the template using its script ID. |
TemplateRenderer.renderToResponse(options) | void | Sends HTML template content as a response. |
Using the TemplateRenderer
To utilize the TemplateRenderer, you can initialize a new object and configure it with the appropriate template and data. For example:
var renderer = render.create();renderer.setTemplateById({ templateId: 1234 });renderer.addRecord({ record: customerRecord });var pdfFile = renderer.renderAsPdf();This code demonstrates how to create a PDF from a template by setting a specific template ID and binding a customer record.
Best Practices
- Always ensure that the data sources used are correctly formatted either as JSON or XML.
- Use
renderPdfToResponse()for immediate user feedback when generating PDFs in response to HTTP requests. - Optimize template designs for PDF contexts to ensure compatibility and performance during rendering.
Who This Affects
This content is particularly useful for:
- Developers: Looking to create custom forms using SuiteScript.
- Administrators: Managing templates for reporting and invoicing.
- Finance Teams: Generating automated statements and documents.
Key Takeaways
- The TemplateRenderer object is essential for dynamic document generation in SuiteScript.
- Several methods facilitate adding data sources and rendering outputs in both PDF and HTML formats.
- Proper usage enhances the automation of reports and custom templates in NetSuite.
Frequently Asked Questions (4)
Do I need to enable a feature flag to use the TemplateRenderer object?
What data formats can be used with TemplateRenderer's addCustomDataSource method?
How can I bind a specific NetSuite record to a template using the TemplateRenderer?
Is it possible to render a template directly to an HTTP response in HTML format?
Was this article helpful?
More in SuiteScript
- SuiteScript 2.1 Enhancements in NetSuite February Updates
SuiteScript 2.1 now supports async features and PATCH method. Discover the latest API and SuiteProcurement improvements.
- Scheduling Map/Reduce Script Deployments in NetSuite
Learn to schedule map/reduce script submissions, including one-time and recurring options in NetSuite.
- Binary File Support in N/https Module for SuiteScript
SuiteScript enhances capabilities with binary file support in the N/https module, allowing improved data handling in external communications.
- API Governance Units Calculation in NetSuite 2026.1
NetSuite 2026.1 introduces examples illustrating API governance unit calculations for both user event and scheduled scripts.
