This article is relevant if you are using NetSuite’s native Advanced PDF technology and you need to perform date math or date calculations.
Background
Our client has a custom line date field they are seeking to use for line expected ship date. Because they want to use NetSuite’s native built-in pick ticket functionality, they want to manipulate the pick ticket to output only line information that fits within a particular date boundary. The goal is to not print lines on items that have an expected ship date 10 days out. Thus, they want to manipulate the template with business logic.
It is not obvious how to do date math in the Advanced PDF templates that leverage the FreeMarker template engine. So Mike, our in-house Advanced PDF Consultant, helped out.
Freemarker Template to Perform Date Math
The following code fragment should help the NetSuite Administrator / Developer see the pattern to develop their own “in-template” functions.
<!-- Function --> <!-- Calculate Days from a Specific Date --> <#function dateDiff date days> <#assign timeInMillisecond = (1000 * 60 * 60 * 24 * days) /> <#assign aDate = date?long + timeInMillisecond?long /> <#return aDate?number_to_date> </#function> <!-- Date Vars --> <#assign date_today = .now /> <#assign date_ten_days_from_today = dateDiff(date_today, 10) /> <!-- Line Item Loop --> <!-- Rule: If Expected Ship Date is greater than 10 days from now, then don't print those line items --> <#if item.expectedshipdate?has_content> <#if !(item.expectedshipdate?date > date_ten_days_from_today?date)> <tr> <td><span style="font-size:8px;">${item.inventorydetail}</span></td> <td><span style="font-size:8px;">${item.item}</span></td> <td><span style="font-size:8px;">${item.description}</span></td> <td><span style="font-size:8px;">${item.quantitycommitted}</span></td> <td><span style="font-size:8px;">${item.units}</span></td> <td><span style="font-size:8px;">${item.quantityonhand}</span></td> <td><span style="font-size:8px;">${item.location}</span></td> <td><span style="font-size:8px;">${item.expectedshipdate}</span></td> </tr> </#if> </#if>
Use our Prolecto Content Renderer Engine (CRE) to Breakthrough NetSuite Template Limitations
Most of these limitations are due to the way NetSuite implemented the FreeMarker engine to support Advanced PDF technology. We completely broke through most of these limitations by creating another framework that links (joins) saved searches together, allows you to use summary searches, and allows you to use not only FreeMarker, but Trimpath, thus opening a world of writing your own template modifiers giving you incredible control. Please see the article, Supercharge NetSuite Advanced PDF/HTML Templates, to learn more. If you are looking to gain more power in your NetSuite investment, let’s have a conversation.
I was wondering if you have a solution that works for adding months? Also do you have a suggestion for handling daylight saving time? We had a developer use the millisecond formula before and that gave the wrong date when it spanned the date of the time changed.
Hello Kenneth,
Here is the FreeMarker reference to Date objects
https://freemarker.apache.org/docs/ref_builtins_date.html
However, I don’t believe there is any built-in way to “Add Month”. This is where our Content Renderer Engine gives you full control over the situation. In our 2.0 version, it’s just as easy to use a bit of SQL to get the data you need.
https://blog.prolecto.com/2021/04/10/content-renderer-engine-2-0-with-netsuite-suiteql/
I may need to write a new article about this to clarify the datetime elements and hourly offsets. This 2015 article may help anchor the situation.
https://blog.prolecto.com/2015/01/30/netsuite-server-side-timezone-settings/
Marty