-1

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.

reklawp
  • 1
  • 2

2 Answers2

0

Your error is likely because you are trying to save your template as an email template of some sort but are using N/render.TemplateRenderer and not N/render.mergeEmail.

I just store my custom templates as files to avoid validation errors on save. Prior to saving I use https://try.freemarker.apache.org/ where running the template takes some time.

Unexpected errors generally mean a syntax error or some data that you are expecting isn't there. You might try logging your non-record data sources or have the user who is seeing the error try running your saved search directly to see if the UI reports issues accessing the data.

bknights
  • 14,408
  • 2
  • 18
  • 31
  • Thanks for your response. I'm saving the template in Netsuite's Advanced PDF Template editor, so I don't think I have a choice there. But thanks for the suggestion on the template tester. – reklawp Aug 26 '22 at 15:20
  • Also, some clarification: I did the entire process, from saved search to report printing as well as editing the template, logged in as the other admin user. And maybe my html-style comments made you think I was doing an email template? See my edits to the original post for what I think might be going on. – reklawp Aug 26 '22 at 15:51
0

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.

reklawp
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 14 '22 at 02:10