ANS-0795 · SUITESCRIPT DEVELOPMENT
How to Generate Dynamic Content Links and Pages in NetSuite Suitelets?
Leverage data URIs within NetSuite Suitelets to dynamically create downloadable files or temporary web pages without requiring physical file storage.
Short answer
To generate dynamic content without a physical URI in NetSuite, encode your data (e.g., CSV or HTML) into BASE64. Embed this encoded data directly into an anchor tag's href attribute using a data: URI scheme for downloads, or open it in a new window using window.open for dynamic web pages within SuiteScript 2.1.
Scenario
NetSuite users may require the ability to generate dynamic content, such as downloadable CSV files or temporary HTML pages, without creating and storing physical files or dedicated URIs. This is particularly useful for on-the-fly data exports or for aggregating multiple transaction print pages into a single, client-side view.
Solution
To generate dynamic content without a physical URI in NetSuite, the process involves encoding the data into BASE64 and then embedding it within a data: URI. This approach supports both direct file downloads and the creation of temporary, client-side web pages.
Generate and Encode Data:
First, prepare your desired content (e.g., CSV string, HTML string). This content must then be encoded into BASE64 format to reduce its size and ensure proper handling within the URI. In SuiteScript 2.1, the N/encode module is used for BASE64 encoding.
Implement for CSV File Download (Suitelet Example):
A Suitelet can be used to present a link that, when clicked, downloads a dynamically generated CSV file. The data: URI scheme is embedded directly into an anchor tag's href attribute.
javascript/** * @NApiVersion 2.1 * @NScriptType Suitelet */define(['N/ui/serverWidget', 'N/encode'], function(serverWidget, encode) { function onRequest(context) { if (context.request.method === 'GET') { var form = serverWidget.createForm({ title: 'My New Form', hideNavBar: false }); // Placeholder for dynamic CSV content generation var myContent = 'Header1,Header2nValue1,Value2nValue3,Value4'; var encodedContent = encode.convert({ string: myContent, inputEncoding: encode.Encoding.UTF_8, outputEncoding: encode.Encoding.BASE_64 }); var field = form.addField({ id: 'custpage_download', type: serverWidget.FieldType.INLINEHTML, label: 'Download CSV' }); field.defaultValue = '<a href="data:text/csv;base64,' + encodedContent + '" download="myFile.csv">myFile.csv</a>'; context.response.writePage(form); } } return { onRequest: onRequest };});This Suitelet will render a page with a link labeled "myFile.csv". When a user clicks this link or right-clicks and selects "Save As...", a physical CSV file named "myFile.csv" will be downloaded, containing the dynamically generated data.
Implement for Dynamic HTML Page (Client-Side Opening):
To dynamically generate and open an HTML page in a new browser tab or window, the HTML content is first encoded into BASE64. The resulting data: URI can then be used with window.open(). This is useful for aggregating multiple transaction print pages or displaying temporary, client-side generated content.
javascript// This example demonstrates generating HTML content and opening it in a new browser tab.// Note: The N/encode module is a server-side module. For purely client-side encoding// within a browser, the native btoa() function would typically be used.// This snippet assumes 'encode' is available in the current scope, for instance,// if the 'uriContent' is prepared server-side and then passed to a client script.var html = '<html><head><title>This is a fake web page</title></head><body><p>blablablablabla.. here there could be links, images, links, whatever we can do in html</p></body></html>';var uriContent = 'data:text/html;base64,' + encode.convert({ string: html, inputEncoding: encode.Encoding.UTF_8, outputEncoding: encode.Encoding.BASE_64});window.open(uriContent);Executing this code will open a new browser tab or window displaying the dynamically generated HTML content.Performance Consideration:It is important to note that using data: URIs means all the content is loaded into memory on the client side. For very large files or complex HTML structures, this might have a visible performance impact on the user's browser.
Expert NetSuite Support
Need help with this NetSuite issue?
SuiteScript Development consulting and configuration support
