- CALL : (+1) 407-273-1001
- Main Office : (+1) 407-273-1001
- NetSuite NetSuite Customization
- Jun 18
- 4 mins read
How to Send Multiple NetSuite Invoices in One File
Print Multiple Invoices Using NetSuite
**Update** we have since developed a bundle that can easily create custom NetSuite PDF’s with multiple invoices.
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 + '
<pbr/>'; } } xml = xml + '</body></pdf>'; 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.
<pbr/>
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
Jeremy McCourt is an content producer in the enterprise software industry that focuses on NetSuite and related cloud-based software solutions.
Related Posts
Hire the Top NetSuite Implementation Partner! (Best Guide 2024)
Contact us! It’s been said that a NetSuite implementation is only as good as the team that implements it. I don’t know if that’s 100% accurate, but there definitely is some truth to it. ERP…
- May 26
- 6 mins read
Hire the Top NetSuite Integration Partner (Best 2024 Guide)
Contact us! How to Choose the Best NetSuite Integration Partner When it comes to integrating NetSuite with other systems and applications, choosing the right partner is crucial. With so many options available, it can be…
- May 26
- 6 mins read
Comments (10)
Jo in OKC
Mar 19, 2015Thanks for the code and the explanation of what’s going on.
I don’t see anywhere in the code where you’re adding the page break. Should the first instance of the following line
xml = xml + ”;
be
xml = xml + ”;
?
Also, what’s the purpose of the 2nd instance of xml = xml + ”; ? Do you really need a PDF Page break at the end of the file? It seems like that would cause a blank page at the end.
Just FYI, but I think you have a typo. You’re missing a single quote in the nlapiLoadRecord call. You have open quote but no close quote.
Jo in OKC
Mar 19, 2015Eek.
I meant
Should the first instance of the following line
xml = xml + ”;
be
xml = xml + ”;
?
Jo in OKC
Mar 19, 2015Well, the comments are eating the tags. I guess that’s what happened to it. Trying again with a backslash to hopefully escape the tag-eater.
Should the first instance of the following line
xml = xml + ”;
be
xml = xml + ”;
?
Jo in OKC
Mar 19, 2015Trying again with an extra single quote to hopefully escape the tag-eater.
Should the first instance of the following line
xml = xml + ”;
be
xml = xml + ”’;
?
Herb Joseph
Mar 23, 2015Word press stripped it out again. But please look at the code sample above. It shows the accurate code now.
Martin
May 11, 2015What does the function ‘getTemplate’ do? NetSuite doesn’t know this function.
Georg
May 11, 2015Hello,
this is great stuff!
But: is getTemplate a function? Can you share it? How can I load the template I want to use?
Regards,
Georg
Herb Joseph
May 11, 2015Hey guys, sorry, I should have put in some more description about the getTemplate function. At this time there isn’t (or at least I can’t find it) a NetSuite function to return the source of the template. You can implement this function in a number of ways: one you can put the template code in a file in the file cabinet and load the file or you can hard code the template source in the script. Neither of these options are ideal, just work arounds. There needs to be a way to load the template dynamically, but this might be much lower on NetSuite’s list of priorities.
Colombian Tavo
May 03, 2016A couple of typo corrections. About to test it.
var invoices = [‘5846′,’5795′,’5282′,’5773′];
var xml = “”; //First one
for(var i = 0; i < invoices.length; i++){
var invoice = nlapiLoadRecord("invoice", invoices[i]); // second one
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);
Colombian Tavo
May 03, 2016Reading more carefully it seems that on the first apearence of the xml variable (line 2: var xml) there’s not just a inicialization to an empty variable but a asignation of the opening tags of body and pdf elements, that must be something like:
var xml = “”;
Not sure if html tag cleaner will let it look correctly, so I add KT letters to make it visible, erase it if you want to use that line.