Categories
Cloud Apps

How To Loop Through A List In Advanced PDF/HTML Template

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 -->
${i.quantity}
<#elseif i.quantity == record.i[item_index-1].quantity>
<!-- if the item matches the same as the previous quantity -->
${i.quantity}
<#else>
<!-- if the quantity of this item differs with the previous -->
<b>${i.quantity}</b>
</#if>
</#list>
view raw ns-looping-list.html hosted with ❤ by GitHub

The important elements to be mindful of when writing this code is:

  • Don’t use ${ and/or } within the if statements – these are for placing those data elements directly into the HTML.
  • Be wary on using just < or > or <= or >= 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 (gt is >, lt is <, gte is >=, lte is <=), 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.

Categories
Cloud Apps

Problem Saving Temple: Index 2, Size 2 In NetSuite

If you’ve been tinkering around with the wonderful Advanced PDF/HTML Template within NetSuite there are times when you’ll receive some ambiguous errors.

I received one today which had me scratching my head for quite some time, it was:

Problem saving template: Index: 2, Size: 2

Whenever you encounter this error it’s trying to tell you there’s an error in your table structure with the number of rows and cells – this will need fixing otherwise it will not save your template.

To try to debug where the error lay go back to your template and click on the Save button again to see if a different prompt occurs – if you’re lucky you’ll be given another ambiguous error, but this time you’re going to try to read this new error to determine where the FreeMarker parser is actually encountering problems.

With this line number go back and copy your parsed code and paste it into your favourite text editor to try to locate the line.

Even though this line will not likely be where the error is it will likely put you in the right area. When you are here you need to begin checking your tr and td tags and any rowspan or colspan calls within those tags.

In my case the second error was pointing me to the footer macro area of which I had the following code which produced the original ambiguous error:

<table>
    <tr>
        <td rowspan="2"></td>
    </tr>
    <tr>
        <td></td>
    </tr>
</table>

From the above you can see with my first td tag I made it span over 2 rows, however, with the second td tag it didn’t know where to go as I hadn’t created an additional column for it. In the end I amended my code to the following which made it no longer throw an error:

<table>
    <tr>
        <td></td>
        <td></td>
    </tr>
</table>

I just removed the rowspan parameter as I found I didn’t really need it and ended up combining it into one row.

Categories
Cloud Apps

Advanced PDF/HTML Template A4 Pixel Dimension With NetSuite

If in NetSuite you’re designing a template and need to know how wide you can go with a table or other HTML element such that it fits snugly on to the page (in pixels), then the maximum width I found with NetSuite’s default page margins is 675px.

Knowing this can help you set the width size of two (or more) adjoining tables that need to be squeezed in together on the same horizontal as shown in the diagram below:

A4 Template Width boxes
A4 width in Advanced Templates is 675 pixels

Unfortunately the default template view will look as though you can squeeze two tables with widths that total 816px, but when it comes to print it’s a different story.

Hopefully this helps as you develop your Advanced PDF/HTML Templates – it took a few trial and errors before I hit it.