Tip 48 – Consolidate(Combine) Lines by Item in Advanced PDF Layout in NetSuite

Recently when I was working on a PDF customization, I got an interesting requirement and I am little surprised how come I didn’t have such requirement to work on as it’s such a common requirement.

Requirement: Customize a Transaction(Sales Order/Purchase Order) PDF template to have consolidate(combine) lines when there is duplicate lines in the record.

Now duplicate lines can be a single column or combination of multiple columns.

Scenario 1: Combine lines where there are duplicate Items

Scenario 2: Combine lines where there are same Items and descriptions

Scenario 3: Combine lines where there are same Items, descriptions and rates

Now we all not, its very easy to combine lines in script.

But think about doing it in Advanced PDF template using Freemarker.

It was really tough when I started working on it. I didn’t find much when I searched in internet.

So, as I achieved it thought of sharing this so that someone like me can save her/his time.

Now Let’s consider the solution for Scenario 1, where we need to just combine lines by Item.

Paste the below code, just before you have the table for Item sublist table (<#if record.item?has_content><table class="itemtable" style ="width : 100%;">).

I am adding the full code which is not visible also and so that we can analyze it.

<#assign arrItems=[] />
<#if record.item?has_content>

<#assign index = 0 /> <#assign arrIndex = [] /> <#list record.item as item> <#assign skip = false /> <#list arrIndex as indexList> <#if index == indexList> <#assign skip = true />
<#if skip == false> <#assign existingItem = item.item /> <#assign found = false /> <#assign newQty = item.quantity /> <#assign newAmount = item.amount /> <#assign count = 0 /> <#list record.item as loopItem> <#if count > index > <#if existingItem == loopItem.item > <#assign found = true /> <#assign arrIndex = arrIndex + [loopItem_index] /> <#assign newQty = newQty + loopItem.quantity /> <#assign newAmount = newQty * loopItem.rate /> <#assign objItem = {“item”: loopItem.item, “description”: loopItem.description, “quantity”: loopItem.quantity, “rate”: loopItem.rate, “amount”: loopItem.amount, “processed”: “T”}/>

<#assign loopItem = objItem /></#if></#if>
<#assign count = count + 1 />
<#assign objItem = {“item”: item.item, “description”: item.description, “quantity”: newQty, “rate”: item.rate, “amount”: newAmount, “duplicate”: “T”}/>
<#assign arrItems += [objItem] />
<#assign item = objItem />
<#assign index = index + 1 />
</#list></#if>

In the above code objItem holds all parameters that you need your table to show. In my example, I have added item, description, rate, quantity and amount, to show, that’s why my object holds that many parameters including “duplicate” parameter to filter later. If you need to show more or less fields, adjust your object accordingly.

Now after this, let’s change the Item sublist as below:

Now your consolidate line logic is done.

Try this and let me know, if you have any concern.

Now for Scenario 2: As you need both Item and Description field combination as unique to combine replace above logic(for item field combine) with below logic

<#assign arrItems=[] />
<#if record.item?has_content>

<#assign index = 0 /> <#assign arrIndex = [] /> <#list record.item as item> <#assign skip = false /> <#list arrIndex as indexList> <#if index == indexList> <#assign skip = true />
<#if skip == false> <#assign existingItem = item.item /> <#assign existingdec = item.description /> <#assign found = false /> <#assign newQty = item.quantity /> <#assign newAmount = item.amount /> <#assign count = 0 /> <#list record.item as loopItem> <#if count > index > <#if existingItem == loopItem.item && existingdec == loopItem.description> <#assign found = true /> <#assign arrIndex = arrIndex + [loopItem_index] /> <#assign newQty = newQty + loopItem.quantity /> <#assign newAmount = newQty * loopItem.rate /> <#assign objItem = {“item”: loopItem.item, “description”: loopItem.description, “quantity”: loopItem.quantity, “rate”: loopItem.rate, “amount”: loopItem.amount, “processed”: “T”}/>

<#assign loopItem = objItem /></#if></#if>
<#assign count = count + 1 />
<#assign objItem = {“item”: item.item, “description”: item.description, “quantity”: newQty, “rate”: item.rate, “amount”: newAmount, “duplicate”: “T”}/>
<#assign arrItems += [objItem] />
<#assign item = objItem />
<#assign index = index + 1 />
</#list></#if>

Hope next time when you will have such requirement, this will help you.

Happy Coding 🙂

Thanks

Ashabari Jena