Mastering NetSuite Render Methods in Templates

In NetSuite SuiteScript 2.1, the N/render module provides powerful capabilities to dynamically generate documents—especially PDFs—using templates. This article dives deep into the render methods used in templates, illustrated through a real-world use case: sending a cancellation email with a PDF summary of a sales order.

Overview of the Render Workflow

The rendering process typically follows these steps:

  1. Load the template file from the File Cabinet.
  2. Create a renderer instance using render.create().
  3. Assign template content via templateContent.
  4. Bind records using addRecord().
  5. Inject custom data using addCustomDataSource().
  6. Render the output using renderAsString() or renderToString().

Key Render Methods Explained

1. render.create()

This initializes a new renderer object.

javascript

var renderer = render.create();

This object acts as the engine that binds data to the template and produces the final output.

2. renderer.templateContent

Assigns the raw template content (usually HTML or XML) directly from a loaded file.

javascript

renderer.templateContent = templateFile.getContents();

This method is ideal when the template is stored in the File Cabinet and needs to be dynamically loaded.

3. renderer.addRecord({ templateName, record })

Binds a NetSuite record to the template. This is essential for templates that use record tags to access standard fields.

javascript

renderer.addRecord({
    templateName: 'record',
    record: newRecord
});

You can bind multiple records with different aliases, such as entity for customer data.

4. renderer.addCustomDataSource({ format, alias, data })

Injects custom data into the template. This is useful for passing structured objects, arrays, or computed values not available in standard records.

javascript

renderer.addCustomDataSource({
    format: render.DataSource.OBJECT,
    alias: 'customData',
    data: {
        fullName: fullName,
        transactionNumber: tranId,
        itemSummary: itemSummary,
        holdReason: holdReason
    }
});

The alias becomes the reference name inside the template, allowing access like ${customData.fullName}.

5. renderer.renderAsString()

Generates the final output as a string—typically HTML or PDF content.

javascript

var pdfFile = renderer.renderAsString();

This string can be used directly in emails or saved as a file.

Real-World Use Case: Order Cancellation PDF

In the provided script, the render module is used to:

  • Load a cancellation template.
  • Bind the sales order (newRecord) and customer (customerRecord).
  • Inject custom variables like item summary, total, and hold reason.
  • Render a PDF and email it to the customer.

This approach ensures a personalized, professional communication with rich transactional details.

Tips for Template Design

  • Use ${record.fieldId} to access bound record fields.
  • Use <#list customData.itemSummary as item> to iterate over arrays.
  • Test templates in the File Cabinet using preview mode before deploying.

Conclusion

The N/render module transforms how NetSuite developers generate dynamic documents. By mastering methods like addRecord, addCustomDataSource, and renderAsString, you can build robust, data-driven templates that elevate customer communication and operational efficiency.

Leave a comment

Your email address will not be published. Required fields are marked *