What is FreeMarker?
Apache FreeMarker is a valuable template engine that NetSuite uses in its Advanced PDF Layout feature. It allows users to further customize basic templates for NetSuite records or email templates. The most common use for FreeMarker is, its use of conditional statements and loops within templates which can be very helpful when creating heavily customized templates.
A very common question we are asked when customizing Transaction templates is:
“What are the best practices for number formatting in FreeMarker?”
One of the best features of FreeMarker is its ability to perform calculations within the PDF/HTML based on the record. However, when manipulating data from NetSuite’s currency fields, the currency format is lost.
For example, here is a PDF where the formatting is not touched, but for the Total we used a custom calculation within the FreeMarker code, so the format is lost. Also note that the Rate is seen as a large Decimal number, which most Customers do not want to see, this can also be manipulated using FreeMarker.
Here is an example of the code used to calculate the Total:
How do we fix this to show it as a Currency?
The most basic way to format a number as Currency is to add ?string.currency onto the end of the variable that is being used. This will bring any numbers to 2 decimal places and will also put a $ next to the number. Also, negative numbers will no longer have a – in front of them and will instead be surrounded by parentheses. All of this is very helpful for USD PDFs.
Here is the code adjustment made:
And here is the PDF for the USD customer:
What about for Non-USD Customers?
Unfortunately, this feature does not take into account of the transaction currency, as we can see with this example of the same code used on a Customer that uses GBP as their currency.
An easy way we can get around this is to use ?string(“,##0.00”) instead of ?string.currency to get the number to 2 decimal places.
Then we also need to get the currency from the record we are using. Once we have the currency, we can have conditionals inside of the FreeMarker to assign the current Currency Symbol to the record’s currency.
NetSuite has a native field called currencysymbol that we can use to assign the needed currency symbol:
Then our code will look like this when calculating whatever totals we need to for the template:
Now our PDF will look like this:
We hope you found this trick useful!
If you have any NetSuite customization needs, PLEASE FEEL FREE TO CONTACT US!