How to do Math on Sourced Values in Advanced Pdf

Recently I had a requirement to print a remaining to be shipped qty on a pick ticket. I know, you’re thinking that sounds a lot like qty backordered. I would agree with you but this user tends to ship uncommitted items so printing the qty backordered value is not a viable solution for this user.  I needed to show what was left to ship regardless of commit status. Basically, I had to print the difference of quantity and quantity fulfilled.

When I plugged this formula in on the pick ticket advanced pdf, I was puzzled when I got an error. The variable values were numerical and I was doing simple subtraction. Also, Freemarker, (the language used in advanced pdf) doesn’t have an issue performing math on numerical values hard coded within Freemarker. For instance, ${4-2} = 2, but an ${item.quantity} variable with a value of 4 minus ${item.quantityfulfilled} with a value of 2 would return an error.

I then remembered that variables sourced out of NetSuite are always returned as a string data type. I then reached into my bag of tricks and pulled out the Int built-in function for Freemarker. To my surprise, when I plugged in ${item.quantity?int-item.quantityfulfilled?int} I still got an error. So I went digging through the Freemarker website (https://freemarker.apache.org/) and found that I needed to combine functions. I needed to remove any possible commas from the string pulled from NetSuite and then turn that string into a number. I ended up with this formula:

 ${item.quantity?replace(“,”,””)?number – item.quantityfulfilled?replace(“,”,””)?number}

If you notice there are two built-ins working in tandem on each variable. The Replace built-in looks through a string for a particular character and replaces it with what you want. In the formula above, the Replace built-in is removing the comma in any string representing a number greater than 1,000. The Number built-in in the formula above is converting the string into a numerical value. This built-in only works when the string contains only digits. That is why the Replace built-in must be done first. I was able to perform the calculation that I needed without any errors with this formula. It can be used with other mathematical operations also. Feel free to try it in your advanced pdf customizations.