Using Custom Data Sources for Advanced Printing

You can combine custom data stored outside of NetSuite with data stored in NetSuite to be displayed together in printed forms for NetSuite transactions. This capability is supported by the SuiteScript 2.0 render module, which includes a method to support XML and JSON data sources in advanced PDF/HTML templates. For more information about the added method, see N/render Module. You can use custom data sources in all server scripts.

Data from JSON and XML is handled as a string, because there is no information about the data type. You can use JavaScript or FreeMarker functions for formatting, if required. For example, you may want to ensure that dates all use the same format.

To include a custom data source:

  1. Customize a standard template and use the source code view to add the new fields.

    The sections with fields from the custom data source have to be surrounded by the FreeMarker tags <#if ALIAS?has_content> </#if>. The element is required because custom data sources cannot be displayed in the template editor. If you view a preview of the template, the external data source is not shown.

    Because FreeMarker supports only XML, JSON data sources have the same restrictions as XML, for example, a property cannot start with a digit. The JSON data source is converted to XML for printing.

    You must know what information is included in the external data source and use syntax like the following:

                    <?xml version="1.0"?>
    <!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">
    <pdf>
    <body> Custom Data Sources: <#if XML?has_content> ${XML.book.title}<br /> ${XML.book.chapter[1].title} </#if> <br /> <#if JSON?has_content> ${JSON.book.title}<br /> ${JSON.book.chapter[1].title} </#if> <br /> <#if JSON_STR?has_content> ${JSON_STR.book.title} </#if> <br /> <#if XML_STR?has_content> ${XML_STR.book.title} </#if>
    </body>
    </pdf> 
    
                  
  2. Create a SuiteScript 2.0 file that uses the addCustomDataSource method in the renderer object. Use code like the following:

                    renderer.addCustomDataSource({format: render.DataSource.XML_DOC, alias: "XML", data: xmlObj});
    renderer.addCustomDataSource({format: render.DataSource.XML_STRING, alias: "XML_STR", data: xmlString});
    renderer.addCustomDataSource({format: render.DataSource.OBJECT, alias: "JSON", data: jsonObj});
    renderer.addCustomDataSource({format: render.DataSource.JSON, alias: "JSON_STR", data: jsonString}); 
    
                  

The form is processed as follows:

  1. The customized template is loaded into the renderer.

  2. Data from the NetSuite record is loaded.

  3. Data from the custom data source is added (XML, JSON, XML_STR, and JSON_STR in the previous example).

  4. The form is printed using the renderer.

Example

For example, you want to print gift certificates for employees that combine name and address information stored in NetSuite with data from an external survey. First, you customize an advanced template using the source code view of the Template Editor, and you add fields from the XML file that contains exported results from the survey. Then you create a SuiteScript file that uses the render module to do the following: load the customized template, add data from the NetSuite record, add custom data from the XML file, and print the gift certificate forms. Data from the two sources are combined to print personalized certificates for employees..

To view an example with code samples, see Example of Using Custom Data Sources for Advanced Printing.

Related Topics

Scripting with Advanced Templates
Using SuiteScript for Transaction Records
Using SuiteScript to Apply Advanced Templates to Non-Transaction Records
Using SuiteScript 2.x to Combine Multiple Data Sources in One Advanced Template

General Notices