I'm trying to write a basic client side script that loops through every line, checks if the item has 'track landed cost' ticked and if not, applies withholding tax (WHT).
The reason is that we need to start accounting for WHT but tax law in the country in question only applies withholding tax for items that aren't physically shipped - so only on downloadable software, professional services, support contracts, etc.
Rather than ensure our accounts team manually applies WHT on every line correctly, I thought I'd make a script to do it automatically, ensuring they don't forget or get confused if an item is physically shipped or not.
For reference, my test invoice has four lines, and the below indicates whether WHT should be applied:
Line 1 - Yes
Line 2 - No
Line 3 - Yes
Line 4 - Yes
Custom fields on the lines definitely show that only Line 2 has 'Track Landed Cost' ticked, so WHT should be applied to Lines 1, 3 and 4.
Our WHT setup has 'ENABLE TAX LOOKUP ON TRANSACTIONS' ticked, so all we have to do in the UI is tick 'Apply WH Tax ?', and the rest is filled in automatically (WH Tax Code, WH Tax Rate, WH Tax Amount, etc). Literally tick and OK the line, that's it.
I have attempted to get my code running on PAGEINIT and as a custom action attached to a button in Edit mode.
Here's my code:
for (i = 1; i <= nlapiGetLineItemCount('item'); i++) { nlapiSelectLineItem('item', i); if (nlapiGetCurrentLineItemValue('item', 'custcoltracklandedcost') == "F") { nlapiSetCurrentLineItemValue('item', 'custcol_4601_witaxapplies', 'T', true, true); } } alert('Finished applying WHT'); Results:On PAGEINIT: ticks 'Apply WH Tax ?' on first line, none of the dependent WHT column fields are sourced, checks line 2 then stopsCustom Action: ticks 'Apply WH Tax ?' on first line, all dependent WHT column fields are sourced, checks line 2 then stopsFirst issue, on PAGEINIT it doesn't seem to trigger any field changed event - no dependent fields are being filled in properly. If run after the UI loads though, this problem doesn't exist.Second issue, it's exiting the FOR loop prematurely. Adding an extra alert() to show me the line number it's on proves this, I see '1', then '2', then 'Finished Applying WHT'. My testing:- ensured all lines met the IF condition, no change- removed IF statement altogether (attempting to apply WHT to all lines with no condition), no change- added an alert() to show me the total number of lines after each loop (in case for some reason applying WHT changed the number of 'lines'), no change and always alerted '4' lines correctly- tried having the script add 'wht' to a custom transaction column field of type Text instead, no change only first two lines were updatedI guess I can live with requiring the person to click a button, but not having it loop through all lines correctly makes it pointless. I've tried adding nlapiCommitLineItem in various places, that actually disables the sourcing of the dependent fields (?). Also tried removing using SelectLineItem and using GetLineItem and SetLineItem, and that didn't work either.Any ideas what is going on here?Cheers