[FIXED] How to resolve an ArrayIndexOutOfBoundsException in Netsuite Advanced PDF Template

Issue

I’m working on a template that will group results from a saved search building on these very helpful stackoverflow posts:

How to remove duplicate elements in a array using freemarker?

How can I group a list in an advanced pdf/html sheet in netsuite/freemarker?

The template I’ve created works fine for me, but when a different user tries to print with it they get an "unexpected error" from Netsuite, and when I logged into that user’s account and tried to open the template and save it, I got this error:

The template cannot be saved due to the following errors:
Exception during template merging.com.netledger.templates.TemplateServiceException: Exception during template merging.java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 10

This other user has the same Netsuite admin role I do, and is using the same browser (Firefox). I even logged into her Netsuite account on my computer in Firefox and replicated the error, so it seems tied to her NS account(?).

My template is meant to simply group and add page breaks to the results of a saved search that returns salesperson commissions per transaction. Here is the code:

<body padding="0.5in 0.5in 0.5in 0.5in" size="Letter-landscape">
<#assign entitygroup = []>
  <#list results as group><!--if the saved search results weren't sorted, they should have been be sorted here-->
    <#assign groupID = group.formulatext> <!--"formulatext" is the sales rep or partner or dept, from the saved search-->
    <#if entitygroup?seq_contains(groupID)><!-- do nothing / don't add it to the sequence-->
      <#else><#assign entitygroup = entitygroup + [groupID]><!--add this entity to the sequence so it will be skipped next time -->
<table>
<tr><td colspan="9" style="font-weight:bold; font-size:12px;">${group.formulatext}</td></tr>
<tr>
    <th style="width:10%;">${group.tranid@label}</th>
    <th style="width:10%;">${group.trandate@label}</th>
    <th style="width:10%;">${group.closedate@label}</th>
    <th style="width:20%;">${group.companyname@label}</th>
    <th align="right" style="width:10%;">${group.netamountnotax@label}</th>
    <th align="right" style="width:10%;">${group.totalcostestimate@label}</th>
    <th align="right" style="width:10%;">${group.tranestgrossprofit@label}</th>
    <th align="right" style="width:10%;">${group.formulapercent@label}</th>
    <th align="right" style="width:10%;">${group.formulacurrency@label}</th>
</tr>
  <#assign total_tot = 0>
  <#assign total_cost = 0>
  <#assign total_profit = 0>
  <#assign total_comm = 0>
  <#list results as result>
    <#if result.formulatext == groupID><!-- if the "entity" (rep, partner, dep) matches the current group-->
<tr>
    <td style="width:10%;">${result.tranid}</td>
    <td style="width:10%;">${result.trandate}</td>
    <td style="width:10%;">${result.closedate}</td>
    <td style="width:20%;">${result.companyname}</td>
    <td align="right" style="width:10%;">${result.netamountnotax}</td>
    <td align="right" style="width:10%;">${result.totalcostestimate}</td>
    <td align="right" style="width:10%;">${result.tranestgrossprofit}</td>
    <td align="right" style="width:10%;">${result.formulapercent}</td>
    <td align="right" style="width:10%;">${result.formulacurrency}</td>
</tr>
  <#assign total_tot = total_tot + result.netamountnotax>
  <#assign total_cost = total_cost + result.totalcostestimate>
  <#assign total_profit = total_profit + result.tranestgrossprofit>
  <#assign total_comm = total_comm + result.formulacurrency>
    </#if><!-- if the "entity" (rep, partner, dep) matches the current group-->
    </#list><!--loop for the lines data-->
    <tr style="font-weight:bold;">
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td align="right"><strong>Totals:</strong></td>
      <td align="right">${total_tot}</td>
      <td align="right">${total_cost}</td>
      <td align="right">${total_profit}</td>
      <td>&nbsp;</td>
      <td align="right">${total_comm}</td>
    </tr>
        </table>        
        <pbr />
      </#if>
        </#list><!-- loop for the "entity" grouping / goes back up to assign the next group-->
</body>

Any help is greatly appreciated, including taking a different approach to this problem!

[Additional info after more testing:]
As I’ve tried to run this down it seems that the way I’m doing my comments might have something to do with the out of bounds error, perhaps causing the template editor to think I’ve left out something so the query on the array had no results?
Logged in as the other admin user I deleted the entire body of my template, saved, then re-added it, and the error did not reoccur. Maybe the template engine reinterpreted my comments correctly (or as I intended)?
I’ve run into this error on another template now and re-did all of my comments in the freemarker way ("<#–") and again it stopped throwing the error, so that’s a little more conclusive.
I hope this is helpful to somebody. I’m still working on these templates, so I’ll update here if I get a more definitive answer.

Solution

After working with this and another complex template I feel pretty confident that the inconsistent behavior I noted in my question was caused by using the wrong commenting tags: <!– instead of <#–. Netsuite’s template editor encourages the html-style comment by putting the text in a consistent brownish color (when using freemarker comments the text colors seem to ignore the comment tag completely) so it’s a bit misleading. At least for my templates changing all the comments to the <#– tag saved the day.

Answered By – reklawp

Answer Checked By – David Marino (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published