Using SuiteScript 2.x to Combine Multiple Data Sources in One Advanced Template

This example shows how to combine multiple data sources on one advanced printing template. The example includes inline comments describing what the script is doing, and links to the relevant help topics are provided. You can run this example directly in the Script Debugger.

Example of Using SuiteScript 2.x to Combine Multiple Data Sources in One Advanced Template

  • Subsidiaries are included, thus the example will work only in a OneWorld account.

  • Specific record numbers are referenced and must exist in the account.

The N/render Module example illustrates how to do the following:

  • Define and use a template directly from SuiteScript, without exposing the template to end users. You are not limited to the record-specific templates when using SuiteScript. You can enter strings that contain your template source code and provide them directly to the renderer.

  • Work with multiple record instances of different record types.

    • Load them.

    • Add them to the template.

    • Use them from the template.

  • Add a custom data source to the template, in this case a JSON object with a collection.

  • Include static barcodes and generate barcodes based on record values.

To view the example, copy the following code sample and paste it into the Script Debugger.

After you run the script, go to Documents > Files > SuiteScripts. The result of running the script is a PDF file named pdf-test.pdf.

             // BFO documentation: http://bfo.com/products/report/docs/userguide.pdf
// Freemarker docs: https://freemarker.apache.org/docs/ref.html
// render module documentation: https://<accountID>.app.netsuite.com/app/help/helpcenter.nl?fid=section_4412042824.html
/** * @NApiVersion 2.x */
require(['N/render', 'N/search', 'N/record', 'N/file'], function(render, search, record, file) { function RenderPdfTest() { var invoiceId = 22; // one of the existing invoices var subsidiaryParent = 1; var subsidiaryCz = 6; //get instance of TemplateRenderer - https://<accountID>.app.netsuite.com/app/help/helpcenter.nl?fid=section_4412065265.html var renderer = render.create(); //after you call the renderer, you can populate its data model by adding records, searches and custom data sources // .addCustomDataSource() - https://<accountID>.app.netsuite.com/app/help/helpcenter.nl?fid=section_4528541027.html // .addRecord() - https://<accountID>.app.netsuite.com/app/help/helpcenter.nl?fid=section_456543212890.html // .addSearchResults() - https://<accountID>.app.netsuite.com/app/help/helpcenter.nl?fid=section_456249023436.html // //add parent company and one child subsidiary renderer.addRecord('subsidiary', record.load({ type: record.Type.SUBSIDIARY, id: subsidiaryParent })); renderer.addRecord('subsidiarycz', record.load({ type: record.Type.SUBSIDIARY, id: subsidiaryCz })); //load Invoice and add it to the renderer's data model using name 'record' //(you can pick any name you want) renderer.addRecord('record', record.load({ type: record.Type.INVOICE, id: invoiceId })); //add custom data source jsonObj = { name: "John", age: 30, city: "Brno", collection: [ {txtId: 'ID: 1'}, {txtId: 'ID: 2'} ] }; renderer.addCustomDataSource({ format: render.DataSource.OBJECT, alias: "myJsonObject", data: jsonObj }); //here you can add a condition to load a template identified by TEMPLATE_ID //renderer.setTemplateById(TEMPLATE_ID); //ID of advanced invoice pdf template //or, you can supply the template source code directly // this way the template is not exposed to customers (because you can hide bundled SuiteScripts) // - https://<accountID>.app.netsuite.com/app/help/helpcenter.nl?fid=section_n3377764.html // //renderer.templateContent accepts any valid template source code string - it doesn't care about where it came from // - https://<accountID>.app.netsuite.com/app/help/helpcenter.nl?fid=section_453133789062.html renderer.templateContent = '<?xml version="1.0"?><!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">' + '<pdf>' + ' <head>' + ' <link name="NotoSans" type="font" subtype="truetype" src="${nsfont.NotoSans_Regular}" src-bold="${nsfont.NotoSans_Bold}" src-italic="${nsfont.NotoSans_Italic}" src-bolditalic="${nsfont.NotoSans_BoldItalic}" bytes="2" />' + ' </head>' + ' <body padding="0.5in 0.5in 0.5in 0.5in" size="Letter">' + ' Hello world! <br/>' + //subsidiary - for field IDs see Record Browser: //https://<accountID>.app.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2023_2/script/record/subsidiary.html ' ${subsidiary.name@label}: ${‌subsidiary.name} <br/>' + ' ${subsidiarycz.name@label}: ${‌subsidiarycz.name} <br/>' + //data from invoice - https://<accountID>.app.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2023_2/script/record/invoice.html ' ${record.entity@label}: ${‌record.entity} <br/>' + ' ${record.location@label}: ${‌record.location} <br/>' + //first item from item sublist of the invoice - (you can iterate through items using Freemarker syntax if needed) ' ${record.item[0].item@label}: ${record.item[0].item} <br/>' + //some bar codes - https://<accountID>.app.netsuite.com/app/help/helpcenter.nl?fid=section_N2868543.html ' QR content: ${record.item[0].item@label}: ${record.item[0].item}<br/>' + ' <barcode codetype="qrcode" showtext="false" height="150" width="150" value="${record.item[0].item@label}: ${record.item[0].item}" /><br/>' + ' <barcode codetype="qrcode" showtext="false" height="150" width="150" value="${record.item[0].item@label}: ${record.item[0].item}" /><br/>' + ' Code39 bar content: ${‌record.tranid}' + ' <barcode codetype="code39" showtext="false" height="30" width="150" value="${‌record.tranid}" /><br/>' + //some data from json object ' myJsonObject.name: ${‌myJsonObject.name} <br/>' + ' myJsonObject.age: ${‌myJsonObject.age} <br/>' + ' myJsonObject.collection[0].txtId: ${myJsonObject.collection[0].txtId} <br/>' + ' myJsonObject.collection[1].txtId: ${myJsonObject.collection[1].txtId} <br/>' + ' </body>' + '</pdf>'; // render PDF var newfile = renderer.renderAsPdf(); newfile.folder = -15; // ID of folder where file created newfile.name = "pdf-test"; var fileId = newfile.save(); } RenderPdfTest(); }
); 

          

Related Topics

Scripting with Advanced Templates
Using Custom Data Sources for Advanced Printing
Using SuiteScript for Transaction Records
Using SuiteScript to Apply Advanced Templates to Non-Transaction Records

General Notices