How to Send Multiple NetSuite Invoices in One File

**Update** we have since developed a bundle that can easily create custom NetSuite PDF’s with multiple invoices. You can install the bundle for free!

And you can also check out this demo video for more info. Please keep reading this blog article if you would like to learn more about the technology this bundle is based on!

First of all, I feel the need to provide this notice: This content is very technical. So if all you want to know is: “can I use a script to send multiple invoices all in one file/attachment”, the answer is yes. If you are interested in the tech stuff then read on.

One of the most basic requirements of an ERP system is the creation and management of transactions such as sales orders, purchase orders, invoices, etc. An ERP system must give its users a way of printing and sharing transactions. In NetSuite, it is really simple to print out a transaction. Refer to the following invoice:

A user only has to click in the print icon and a PDF version of the invoice will appear in a new browser window/tab:

NetSuite goes a step further and makes it really easy to customize the PDF by using popular frameworks from FreeMarker and Big Faceless Organization.

The following 2 images show the NetSuite ‘Advance HTML/PDF template’ editor. The first view shows a graphical user interface that gives the user a really easy way of modifying an invoice PDF.

The second image shows a HTML editor that gives a HTML developer the ability to completely customize the invoice PDF. This view also shows some of the elements of the FreeMarker template. A developer can access the NetSuite objects used to create the PDF, for example, record.total will return the total dollar amount of the invoice.

After the template is completed and saved it can be attached to the invoice form and will be presented to the user when the print button is clicked or if the user decides to send the invoice PDF via email.

So sending a single transaction via email is pretty simple using the NetSuite UI. Sending multiple transactions as multiple attachments is also straightforward. But how can multiple transactions be combined into a single attachment/file? Turns out that can be done by using the NetSuite scripting API.

var invoices = ['5846','5795','5282','5773'];               
var xml = ‘';

for(var i = 0; i < invoices.length; i++){
var invoice = nlapiLoadRecord(‘invoice, invoices[i]);
var renderer = nlapiCreateTemplateRenderer();
renderer.setTemplate(getTemplate(invoice));
renderer.addRecord('record', invoice);
xml = xml +  renderer.renderToString();
if(i < invoices.length - 1){
xml = xml + ''; } } xml = xml + ''; var file = nlapiXMLToPDF(xml);

In the code sample above the following sentence could be replaced by the results of saved search.

var invoices = ['5846','5795','5282','5773'];

But for this example they represent the internal ids of the invoices that will be merged into one document. The script then goes on to loop through the list of invoice ids and load each invoice into memory:

for(var i = 0; i < invoices.length; i++){
var invoice = nlapiLoadRecord(‘invoice’, invoice[i]);

Next the script calls the NetSuite nlapiCreateTemplateRenderer to create an instance of the nlobjTemplateRenderer. This object accepts the template code the developer created as a ‘raw’ string and it also accepts a NetSuite record object and uses FreeMarker to combine the two.

var renderer = nlapiCreateTemplateRenderer();
renderer.setTemplate(getTemplate(invoice));
renderer.addRecord('record', invoice);

The renderToString function of the nlobjTemplateRenderer returns XML that is understood by the Big Faceless Organization’s API. And luckily, Big Faceless Organization gives us an XML tag to create a PDF page break.

This is the key to having each transaction on its own page. All that is left to do is call a NetSuite API that takes the XML and runs it through the Big Faceless Organization’s API to create a PDF file.

var file = nlapiXMLToPDF(xml);

A developer can now do anything with this file such as email, save it, etc. I would love to hear your thoughts about any possible enhancements that could be made to this script or any comments about how this could be used. Please leave any comments in the box below, and if you have any questions about NetSuite Consulting, you can always ask the experts at eMerge Technologies