Advanced Templates – Freemarker lists & hashes

I needed to roll up profitability numbers by sales rep and by office. I created a saved search that included all the detail at a line item level. Next, I engineered an Advanced PDF Template to “pretty print” the results. Then the real trick… rolling up the numbers for a summary. I needed to know that the rolled up numbers matched my detail exactly. The best way to do this… use the detail to print the summary!

My tool of choice was Freemarker and NetSuite’s Advanced PDF Template. Where I ran into trouble was finding documentation on Freemarker lists and hashes. Here is my cheat sheet.

In my example, I’m going to roll up the extended price, NetSuite’s calculated cost, and the vendor PO cost of an item. I’ll roll them up to the sales rep level.

In my Advanced PDF Template, I start with this Freemarker code.

<#assign salesReps = {} />
<#list results as result>

This creates a Freemarker list (not a hash) and loops through every line item, one at a time.

Then I check to see if there is a list entry for my sales rep. The rep’s name serves as a unique key in my list. If an entry doesn’t exist, I add one. This syntax is really quirky in Freemarker. I’m not even going to try to explain it! The magic happens at the “plus” sign.

<#if !salesReps[result.salesrep]??>
    <#assign salesReps = salesReps + {result.salesrep :
          {“ext_price” : result.ext_price, 
            “est_cost” : result.est_cost,
            “vpo_cost” : result.vpo_cost}} />

Note that the list has a key of the sales rep’s name (unique), followed by a hash of fields I want to roll up by sales rep.

In the <#else /> clause, I need to roll up the hash fields. Here’s how that works.

<#assign new_ext_price = salesReps[result.salesrep].ext_price + result.ext_price />
<#assign salesReps = salesReps + {result.salesrep : 
    {“est_price” : new_ext_price,…

And finally, printing the rolled up values…

<#list salesReps as name, values>
    <tr>
        <td>${name}</td>
        <td>${values.ext_price?string.currency}</td>
        …

In my search to understand Freemarker lists and hashes, I found these links.

Beyond these… the well ran dry!

 

4 thoughts on “Advanced Templates – Freemarker lists & hashes

  1. Vernita says:

    Hi There. I am running into the same issue of finding some tutorials or documentation on how Freemarker works for advanced pdfs. At the moment, I have created a variable in the header of the pdf that is supposed to return a value from a list of values in the body of the pdf.

    So far, it is printing nothing and I can’t see why. Do you have any suggestions of where I can find how to print calculated variables on an advanced pdf in Netsuite?

    Like

  2. Nina Leon says:

    Any chance you would post the full advanced template code?
    This looks like the elusive way to create Advanced PDF Templates for summary saved searches by (annoyingly) not summarizing them in the saved search but summarizing them in the Advanced PDF Template. As you mention, documentation is lacking but from a user standpoint, being able to run a saved search, filter as needed, and create a pdf seems key to using NetSuite in a way that feels normal.

    Like

    1. This is as much as I’m willing to share. I hope this helps. It shows how to loop through all results in the saved search and create an array of totals by sales rep.

      Once you’ve created your summary data, you can loop through that like this…

      Like

Leave a comment