0

Currently, I have a table of values/attributes in my Custom Transaction Form. I want to write an Advanced PDF Template, so when I print the form as PDF, some values are grouped to different sections.

For example, my form has four attributes, called Store, Product ID, Prices, and NumOfSales, like the following.

  1. AA, 123, 2.89, 10
  2. AA, 123, 2.89, 12
  3. BB, 123, 1.99, 29
  4. BB, 124, 4.00, 9
    I want to write an advanced pdf template so that the printed result looks like the following.
  5. AA, 123, 2.89, 22
  6. BB, 123, 1.99, 29
    124, 4.00, 9

I am wondering if Advanced PDF Template - Freemaker has group_by() and sum() functions OR if I need to use SuiteScript to achieve that. If I need to used SuiteScript, how to combine Advanced PDF Template and Script together. Thanks for your help.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
RhysJ
  • 153
  • 1
  • 3
  • 19

2 Answers2

2

The full Freemarker documentation is available here.

There are no built-in functions such as group_by() or sum(), and though you could put something together using <#list> directives etc., you would have a much easier time doing this in JavaScript using a library like lodash.

For more information in how to combined SuiteScript and Advanced PDF/HTML Templates, see the help section topic Using SuiteScript to Apply Advanced Templates to Non-Transaction Records, or if you are using SS2.0 see the N/render module.

michoel
  • 3,725
  • 2
  • 16
  • 19
1

If you can do the processing/grouping prior to passing data to freemarker you are better off. However if you are doing something like extending the standard transaction forms that isn't a simple option.

You can simulate grouping by using sequence operations. (see http://freemarker.org/docs/ref_builtins_sequence.html)

Then:

<#assign seen_style = []>
<#list record.item?sort_by('custcol_style') as lineitem>
    <#assign lineStyle = lineitem.custcol_style>
    <#if seen_style?seq_contains(lineStyle)>
    <#else>
        <#assign seen_style = seen_style + [lineStyle]>
        <#assign styleTotal = 0>
        <#list record.item?sort_by('custcol_size') as styleItem>
           <#if lineStyle == styleItem.custcol_style>
              <#assign styleTotal = styleTotal + styleItem.quantity>
          </if>
        </#list>
       <div>${lineStyle} has ${styleTotal}</div>
   </#if>
</#list>
bknights
  • 14,408
  • 2
  • 18
  • 31
  • You can paste your code by editing your original question. – bknights May 17 '17 at 18:56
  • re:data in transaction form already. Yes that the the use case I am offering a solution to. You weren't explicit about that but doing the grouping before hitting the template is easier if you are generating output from code. It doesn't sound like you are doing that. – bknights May 17 '17 at 18:57
  • Also 'groupby' is not a Netsuite field nor the format of a custom column field. – bknights May 17 '17 at 18:59
  • https://stackoverflow.com/questions/76251321/sum-of-invoice-amount-based-on-currencies-in-netsuite-pdf-template i am facing a similar issue @bknights – Addy May 17 '23 at 13:51