Finnster
Finnster

Reputation: 107

Freemarker Date Field to display last business day of the month

I am VERY new to Freemarker. Apparently, it is the template model of choice in NetSuite. I am tasked with updating an email template to display a line of text that includes the last business day of the month.

I see how to display the field in documentation that I have found and here is my code for that.

<#assign aDateTime = .now>
<#assign aDate = aDateTime?date>
<span>PLEASE EXPEDITE ORDER PROCESSING AND SHIP OVERNIGHT TO GUARANTEE DELIVERY BY EOD 
THE LAST BUSINESS DAY OF THE MONTH ${aDate}</span>

My question, since I clueless here, is how to display the last business day of the month?

EDITED TO ADD: I have found that the add ins for Freemarker date manipulation can be of some assistance here (based on another thread here). The issue is getting the right logic down to show the date. Here is an example of subtracting 18 days from a certain date:

${(mydate?long - 18 * 86400000)?number_to_date?string("yyyy-MM-dd")}

So I know there is a way to do what I am trying to do with this, but I just can't wrap my head around it.

Any and all assistance would be greatly appreciated.

Thanks.

Upvotes: 0

Views: 1798

Answers (2)

Finnster
Finnster

Reputation: 107

I figured this out. I used a thread showing how javascript picked the last day of the month (link here) and then converted that code to Freemarker. Here is the Freemarker code snippet:

<#assign aMonth = .now?string("M")>
<#assign aNewMonth = aMonth?number>
<#assign aYear = .now?string("yyyy")>
<#assign aDay = 0>
<#assign aNewDate = "" + (aNewMonth + 1) + "/" + aDay + "/" + aYear>
<#assign aLastDay = aNewDate?date>
MONTH: ${aNewMonth}<br />
YEAR: ${aYear}<br />
${aLastDay?string.full}<br />

This is the LAST day of the month. I do some if then else logic to determine if the string is a Saturday or Sunday and then print out the correct last business day of the month.

Upvotes: 0

bknights
bknights

Reputation: 15402

There are a couple of approaches here depending on how the template is being called. If you are using this as a standard template triggered on a record (transaction) then you need to get the last business day of the month value into a custom field on the record. You could do that by creating a custom record of months that has a start and end date and a last business day. Then you make your last business day field a Custom Body Date Field Non-Stored Defaulted from a saved search

The saved search uses either today's date or some date from the transaction to lookup the date's month's last business day. Of course if that is based on the transaction's date then you can set the value with a script.

If you can set it with a script the custom record solution still works. What I've done in the past though is build a function that can do business date calculations based on the account's location (US or Australia) and the observed statutory holidays.

Both approaches require occasional updates. The function of course could also be used to generate upcoming last business dates.

A couple of examples of business day calculations are in my github repo for Netsuite things. calculateShippingDates and calcNSWBusinessDays are examples of generating calculating spans of business days. calculateShippingDates actually has a last business day of month calc.

HTH

Upvotes: 1

Related Questions