When working with NetSuite’s Advanced PDF/HTML Templating system it’s difficult in being able to find documentation on the syntax structure for working with logic in the templates.
One of the requirements I had was being able to determine whether the quantity ordered on an item has the same quantity ordered as the previous item. This required the ability to determine that the item being iterated through with the
#list function was not the first item (otherwise it wouldn’t have been able to compare to the previous item – as there would be none!) and if the item was NOT the second to be able to compare the previous item’s quantity to its current quantity (if it’s then different to do stuff).
Here’s how the template in this respective area then worked:
|<#list record.item as i>|
|<#if i_index == 0>|
|<!-- it's the first item so just enter the quantity -->|
|<#elseif i.quantity == record.i[item_index-1].quantity>|
|<!-- if the item matches the same as the previous quantity -->|
|<!-- if the quantity of this item differs with the previous -->|
The important elements to be mindful of when writing this code is:
- Don’t use
}within the if statements – these are for placing those data elements directly into the HTML.
- Be wary on using just
>=in your logical expressions, otherwise you’ll receive an error – something to the effect of:
The template cannot be saved due to the following errors: Error on line 133, column 34 in template. Expecting a boolean (true/false) expression here. Expression i.quantity does not evaluate to true/false . it is an instance of com.netledger.templates.model.NumberModel
To get around these problems do any one of the following:
- Wrap your logical expression containing your greater-than or less-than sign in brackets, such as
<#if ( i.quantity > record.item[item_index-1].quantity )>...</#if>
- Use the shortcode expressions (
<=), such as
<#if i.quantity gt record.item[item_index-1].quantity>...</#if>
In fact if you use any sort of logical operator in your expression it’s probably best practice wrapping it in brackets:
<#if ( ( i.quantity > record.item[item_index-1].quantity ) && ( i.quantity > 1 ) ) >...</#if>
More information on the syntax used in NetSuite’s Advanced PDF/HTML Templating system can be found at the FreeMarker website.