0

Developing with Freemarker in NetSuite.

I have a List of Items on an Invoice… all items with the same 2nd value (Package) are to be grouped. I have not found a way to skip an item (<#CONTINUE> not working) so I thought of creating a copy the list record, step through it and add only the ones I want:

*
    <#assign mylist = record.item>
    <#list record.item?sort_by("custcolzab_comp_name","custcolzab_ev_supp_plan","item") as item>
        <#if item.custcol_ev_package != cPackage>
                <#assign mylist = mylist + item>
        </#if>
        <#assign cPackage = item.custcol_ev_package>
    </#list>
*

The above SEEMS to work, mylist is a HASH, although a List would be better. But now I cannot get mylist to print

  • That's what `mylist?filter(item -> item.custcol_ev_package != cPackage)` is for in FreeMarker, though I'm not sure if the NetSuite fork already has it. – ddekany Sep 25 '21 at 07:55
  • Also in your example you are adding together the item hashes. That's not what you want, that just creates a single new merged hash. For example `{'foo': 11, 'bar': 12} + {'foo': 21}` is `{ 'foo': 21, 'bar': 12}`. – ddekany Sep 25 '21 at 07:59
  • What do you mean by `<#continue>` not working? Before `?filter`, the usual solution was to not print some items by skipping it with `<#if ...><#continue>#if>`. Although it's only useful if you wanted to print (or otherwise process) the list right away. – ddekany Sep 25 '21 at 08:03
  • Ddekany. Thank you. <#continue> does not seem to be a valid command in NetSuite... Errors on saving saying unrecognized. I cannot just filter the list because I also need to sum the items with the same package. I was hoping to step through the list and only add records that I want to keep and then use the new list for my output. – Jim Schweder Sep 25 '21 at 20:13
  • If you don't have `#continue`, then I assume you don't have `?filter` either (Netsuite really lags behind because of not using vanilla FreeMarker). But otherwise I don't see how it would interfere with summing. Now, actually you can add sequences like `<#assign listSoFar = listSoFar + [newItemToAdd]>`. But it will create a list that's O(N^2) to traverse, so... you better just `#list` and skip items with `#if`. – ddekany Sep 26 '21 at 05:55

1 Answers1

0

You can use a sequence. See How to remove duplicate elements in a array using freemarker?

So your solution would look like:

<#assign seen_group = []>
<#list record.item?sort_by("custcolzab_comp_name","custcolzab_ev_supp_plan","item") as item>
   <#assign cPackage = item.custcol_ev_package>
   <#if seen_group?seq_contains(cPackage)> <!-- no if body is intentional; skips seen style -->
   <#else>
     <#assign seen_group = seen_group + [cPackage]>
     <h2>Do something with ${cPackage}</h2>
     <#list record.item as pkgItem>
        <#if pkgItem.custcol_ev_package == cPackage>
          <p>Package Item: ${pkgItem.item}</p>
        </#if>
     </#list>
   </#if>
</#list>
bknights
  • 14,408
  • 2
  • 18
  • 31