ANS-1293 · PRINTING, PDF & EMAIL TEMPLATES
How to Include Customer Aging Data in NetSuite Advanced PDF/HTML Invoice Templates?
This guide provides a script-based workaround to extract customer aging information from basic transaction form layouts and embed it into custom Advanced PDF/HTML invoice templates.
Short answer
To add an aging table to an Advanced PDF/HTML invoice template, customize a basic transaction form HTML layout with a marker for the <NLAGING> tag. Create a custom transaction form using this layout for HTML generation. A Suitelet script then extracts the aging table from the generated HTML and injects it into your Advanced PDF/HTML template before rendering the final PDF.
Scenario
NetSuite users often require the inclusion of customer aging details directly within their Advanced PDF/HTML invoice templates. While Advanced PDF/HTML templates offer extensive customization, direct access to the tag or similar aging data fields is not natively available for dynamic insertion. This presents a challenge for businesses needing comprehensive invoice layouts.
Solution
Customize a Basic Transaction Form HTML Layout to add a marker. While NetSuite encourages the use of Advanced PDF/HTML Templates for printing and notes that Basic layouts will be deprecated, this workaround leverages a Basic layout for data extraction.nn
Navigate to Customization > Forms > Transaction Form HTML Layouts, then click Customize on 'Classic HTML Transaction Layout'.n
Under the Templates tab, in the BODY field, locate the tag . This tag is used by NetSuite to retrieve aging data from the customer record; its internal mechanism is not exposed.n
The tag is typically enclosed within a
. Identify this table and insert 'Aging' or any other unique string marker that will be used later in your script to identify and extract the table.n
Save the layout.nn
Create a Custom Transaction Form that uses the layout created above for HTML generation. Note that selecting 'Basic' for 'Printing Type' is part of a deprecated methodology for general document printing; however, it is utilized here to generate the HTML stream containing the aging data.nn
Go to Customization > Forms > Transaction Forms, then click Customize on any standard Invoice form.n
Select 'Basic' for 'Printing Type'.n
Select the layout created in Step 1 for 'HTML Layout'.n
Save the custom transaction form.nn
Upload your Advanced HTML/PDF template for Invoice in File Cabinet.nn
Navigate to Customization > Forms > Advanced PDF/HTML Templates.n
Click Edit on your desired template.n
In Source Code mode, add a string marker (e.g., 'Aging') where you intend to insert the aging table.n
Copy the entire template source code and create a .html file with this code.n
Go to Documents > Files > File Cabinet, then upload the .html file.nn Note: If the template includes a URL with an ampersand (&), it must be replaced with & to prevent errors. For example, an img tag for a company logo might need adjustment:n
n Change the line as follows:n
nn
Suitelet Script. The following script is provided in SuiteScript 2.x, which is NetSuite's recommended version for new development. The original approach utilized SuiteScript 1.0 functions, but this version has been updated to align with current best practices.nn
javascriptn/**n * @NApiVersion 2.xn * @NScriptType Suiteletn */ndefine(['N/render', 'N/file', 'N/record', 'N/http'], function(render, file, record, http) {nn function onRequest(context) {n if (context.request.method === 'GET') {n var invoiceId = 344150; // Invoice to printn var customFormId = 184; // Internal ID of Custom form created in Step 2n var advancedTemplateFileId = 1045754; // Internal ID of your advanced template file uploaded to File Cabinetnn try {n // Generate HTML stream from the custom transaction form (Basic layout)n var htmlFile = render.print({n entityId: invoiceId,n printMode: render.PrintMode.HTML,n formId: customFormId,n recordType: record.Type.INVOICEn });n var stream = htmlFile.getContents();nn // Slice Aging table out using the string markern stream = stream.slice(stream.indexOf('Aging'), stream.length);n var agingTable = stream.slice(stream.indexOf('<table'), stream.indexOf('</table>') + '</table>'.length);nn // Replace some properties as they are not quotedn var graytext = /class=smallgraytext/gi;n var printtext = /class=printtexttable/gi;n var alignProp = /right/gi;n agingTable = agingTable.table.replace(graytext, '');n agingTable = agingTable.replace(printtext, '');n agingTable = agingTable.replace(alignProp, '"center"');nn // Load the Advanced PDF/HTML template filen var templateFile = file.load({n id: advancedTemplateFileIdn });n var templateString = templateFile.getContents();nn // Insert Aging table into the Advanced templaten if (templateString.indexOf('Aging') !== -1) {n var temp1 = templateString.slice(0, templateString.indexOf('Aging'));n var temp2 = templateString.slice(templateString.indexOf('Aging') + 'Aging'.length, templateString.length);n templateString = temp1.concat(agingTable, temp2);n }nn // Render the final PDFn var renderer = render.create();n renderer.templateContent = templateString;n renderer.addRecord({n name: 'record',n record: record.load({n type: record.Type.INVOICE,n id: invoiceIdn })n });nn var pdfFile = renderer.renderAsPdf();nn context.response.setHeader({n name: 'Content-Type',n value: 'application/pdf'n });n context.response.writeFile(pdfFile);nn } catch (e) {n log.error({n title: 'Error printing invoice',n details: en });n context.response.write('Error: ' + e.message);n }n }n }nn return {n onRequest: onRequestn };n});nTalk to a consultantExpert NetSuite Support
Need help with this NetSuite issue?
Printing, PDF & Email Templates consulting and configuration support
