Summation of values in freemarker - freemarker

I am new to programming and this is my first time using freemarker. I was wondering if there is a way to get the summation of the values.
My current code is this:
<td>Total Boxes:</td>
<td><#if invtransfer?has_content>
<#if record.custbody_hdr_sort_by == "Item Category">
<#list invtransfer?sort_by("x.item.class?split(':')[0]?trim") as x>
<#assign currCategory>${x.item.class?split(':')[0]?trim}</#assign>
<#if currCategory == cat>
<#if x.unit == "PIECE">
${(x.item.unitstype?split("/")[0]?trim?number) * (x.quantityuom?replace("-", "")?number)}
<#elseif x.unit == "BOX">
${x.quantityuom?replace("-", "")}
<#elseif x.unit == "PALLET">
${(x.item.unitstype?split("/")[1]?trim?number) * (x.quantityuom?replace("-", "")?number)}
</#if>
</#if>
</#list>
</#if>
</#if>
</td>
and my current output is this:
Total Boxes: 3 4 5
How do I add them all to get Total Boxes: 12?
Please help, Thanks.

Related

Convert html string with anchor tags into plain text in freemarker

Is there a way in freemarker to convert the below html string into plain-text?
Challange: data assign in below tag is dynamic including links and their placement.
<#assign data = '<li>List item 1 with some link</li><li>List item 2 with some link1 and some link2</li>' />
Expected: Need data in plain text format like below:
- List item 1 with some link <https://some-link.com>
- List item 2 with some link1 <https://some-link1.com> and some link2 <https://some-link2.com>
What I have achieved till now:
I am able to remove the complete html code using regex freemarker but it also removes the anchor tag urls which cause unexpected results.
${data?replace("<[^>]*>", "", "r")}
// result
- List item 1 with some link
- List item 2 with some link1 and some link2
Any help would really appreciate!!
Please find the solution below.
<#function htmlListToPlainText param>
<#assign str = param?replace('<ol>','')?replace('</ol>','')?split('</li>')/>
<#assign result>
<#list str as item>
<#if item?contains('<a')>
<#assign anchors = item?matches(r"href='(.*?)'") />
<#if anchors?size == 0>
<#assign anchors = item?matches(r'href="(.*?)"') />
</#if>
<#assign mitem = item?replace('</a>','[0]','f')?replace('</a>','[1]','f')?replace('</a>','[2]','f')?replace("<[^>]*>", "", "r") />
<#list anchors as m>
<#assign mitem = mitem?replace('[${m?index}]',' <${m?groups[1]}>','f') />
</#list>
-${mitem}
<#elseif item?has_next>
-${item?replace("<[^>]*>", "", "r")}
</#if>
</#list>
</#assign>
<#return result>
</#function>
How to Use
${htmlListToPlainText(YoursignalName)}
Limitation:
Handling upto 3 dynamic links in one <li> tag.

Determine If a String Is Present in a List or Map?

How do I determine if a list or map contains a specific string? For example (pseudo code):
<#if listofItems.contains("random-string") >
the map contains a key called random-string
</#if>
Lists
If it's a list:
<#if listOfItems?seq_contains("random-string")>
...
</#if>
Maps
If it's a map:
<#if someMap["random-string"]??>
...
</#if>
If it's a map and the key contains no special characters:
<#if someMap.randomString??>
...
</#if>
If it's a map and you are looking for the value of a key-value pair:
<#if someMap?values?seq_contains("random-string")>
...
</#if>
I think something of this sort should work
<#if listofItems['random-string']?? >
you are inside if block
</#if>

Calculate the sum of column values in Netsuite Advanced PDF/HTML Templates?

I want to make a subtotal of values corresponding to the same tax code in Netsuite Advanced PDF/HTML. Is it even possible? What are other ways to achieve my goal?
This is definitely possible. The templating engine that Netsuite uses under the hood is Apache FreeMarker, and I would highly recommend looking at the documentation there for a full details.
Here is some basic untested code that you can use to start:
<#assign tax1subtotal = 0 >
<#assign tax2subtotal = 0 >
<#assign tax3subtotal = 0 >
<#list record.item as item>
<#if item.taxcode == [taxcode1]>
<#assign tax1subtotal = tax1subtotal + item.tax1amt>
</#if>
<#if item.taxcode == [taxcode2]>
<#assign tax2subtotal = tax2subtotal + item.tax1amt>
</#if>
<#if item.taxcode == [taxcode3]>
<#assign tax3subtotal = tax3subtotal + item.tax1amt>
</#if>
</#list>
Tax Type 1 Subtotal: ${tax1subtotal}<br/>
Tax Type 2 Subtotal: ${tax2subtotal}<br/>
Tax Type 3 Subtotal: ${tax3subtotal}<br/>

How do I determine if a sequence contains the values of another sequence?

I have two sequences of integers. I want to determine if the sequence A contains a values of seuqence B, and then get the values of B that are inside A.
This is what I tried:
<#assign myArticles = [50901,50369] />
<#assign articles = [50369, 50379, 50901] />
<#list myArticles as article>
${articles?seq_contains(article)?string("yes", "no")}
</#list>
And it works (result "yes"), but when I use a List it doesn't work (result "no"):
<#assign articlesList = articleService.getArticles(groupId) />
<#assign articlesArray = [50901,50369] />
<#assign articlesId = []>
<#list articlesList as item>
<#assign articlesId = articlesId + [item.getArticleId()]>
<#-- articlesId gives me this ids: 50369, 50379, 50901 -->
</#list>
<#list articlesArray as article>
${articlesId?seq_contains(article)?string("yes", "no")}
</#list>
Why?

freemarker looping sequence error

I'm trying to retrieve data with this code
<tests>
<#if tests?exists>
<#list tests as object >
<test>
<#list object?keys as key >
<${key}>
<#if object[key]?exists>
<#if object[key]?is_hash> HASH
<#elseif object[key]?is_sequence>
<#list object[key] as hashKey>
</#list>
<#else> ${object[key]}</#if><#else>null
</#if>
</${key}>
</#list>
</test>
</#list>
</#if>
but getting an error ?size is unsupported for: freemarker.ext.beans.SimpleMethodModel
but <#elseif object[key]?is_sequence> sequence returns sequence. As I understand means that my object[key] is a sequence.
Any ideas?
That error occurs because as a result of some historical mishap, Java methods are sequences (with the built in ObjectWrapper-s, that is), but they don't implement ?size. (They are sequences so that foo.m[x] is equivalent with foo.m(x)). Add && !something?is_method to the condition avoid this.

Resources