Superdooperhero
Superdooperhero

Reputation: 8096

NetSuite FreeMarker Advanced PDF HTML Template IF ELSE Error

I have the following NetSuite Advanced PDF HTML Template code that is giving me an error:

<#if record.item?has_content>

<table class="itemtable" style="width: 100%;"><!-- start items --><#list record.item as item><#if item_index==0>
<thead>
  <tr>
  <th colspan="4">Item Code</th>
  <th colspan="12">Item Description</th>
  <th align="right" colspan="2">UOM1</th>
  <th align="right" colspan="3">${item.quantity@label}</th>
  <th align="right" colspan="3">UOM2</th>
  <th align="right" colspan="4">Unit Price (excl. VAT)</th>
  <th align="right" colspan="3">${item.amount@label}</th>
  </tr>
</thead>
</#if><tr>
  <td colspan="4">${item.item}</td>
  <td colspan="12">${item.description}</td>
  <td align="right" colspan="2">${item.custcolsyn_uom}&nbsp;${item.custcolsyn_unit_measure}</td>
  <td align="right" colspan="3">${item.quantity}</td>
  <td align="right" colspan="3">${item.units}</td>
  <td align="right" colspan="4"><#if item.rate?has_content>${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}<#else>&nbsp;</#if></td>
  <td align="right" colspan="3">${item.amount}</td>
  </tr>
  </#list><!-- end items --></table>
</#if>

The problem lies with the line:

<td align="right" colspan="4"><#if item.rate?has_content>${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}<#else>&nbsp;</#if></td>

It looks like FreeMarker is evaluating the following part

${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}

even when the line item does not have anything for the rate. Surely

<#if item.rate?has_content>

should prevent that evaluation from happening. I was trying to only keep 2 decimals worth of currency data and all other methods I tried lost the currency symbol.

We are on the latest version of NetSuite (2018.2).

The error message is:

The template cannot be printed due to the following errors: 

Error on line 239, column 95 in template.

Detail...

Range start index 0 is out of bounds, because the sliced string has only 0 character(s). (Note that indices are 0-based).
The blamed expression:
==> 0..1 [in template "template" at line 239, column 128]

----
FTL stack trace ("~" means nesting-related):
- Failed at: ${item.rate?keep_after_last(".")[0..1]} [in template "template" at line 239, column 95]
----


Please contact your administrator.

Anybody have any ideas of what I'm doing wrong or how I can fix this?

Upvotes: 2

Views: 6662

Answers (2)

soorapadman
soorapadman

Reputation: 4509

Since you said rate null You can use the ?? test operator:

<#if item.rate??></#if>

Also if you want to check multiple condition you can check <#if item.rate?? && item.rate?has_content></#if>

For money format you can refer this link docs . If you want custom format you can do try this

${item.rate:M2}

Upvotes: 1

Krypton
Krypton

Reputation: 5231

The Freemarker ?has_content function can give unexpected results - especially when you don't control the underlying data model (which is the case with NetSuite). Essentially what's happening is that the item.rate being passed is not null and does not meet ?has_content's definition of "empty", even though it looks empty. You can use ?length gt 0 instead:

  <td align="right" colspan="4"><#if item.rate?length gt 0>${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}<#else>&nbsp;</#if></td>

This would still be a problem if, for example, an item.rate was for some reason passed to the template with no ".", as it would still end up with an empty string, for which the [0..1] index would be invalid.

So, you could test whether the item.rate ?contains the decimal separator instead:

<td align="right" colspan="4"><#if item.rate?contains(".")>${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}<#else>&nbsp;</#if></td>

But the simplest thing to do might be to use Freemarker's built in currency string formatting instead:

<td align="right" colspan="4">${item.rate?string.currency}</td>

Upvotes: 4

Related Questions