This topic contains 7 replies, has 0 voices, and was last updated by khultquist 7 years, 6 months ago.
-
AuthorPosts
-
May 11, 2017 at 11:53 pm #5739
sneyI’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:
Code:
for (i = 1; i This is a cached copy. Click here to see the original post. -
May 12, 2017 at 2:24 am #5740
khultquistHi. For issue 1, consider changing this to a user event script (before submit) so that dependent fields can be updated. This will have an added benefit of running on records not created thru the UI. You do have to change the functions from getcurrentlineitem to getlineitem and specify the line number, but this is also much faster and will be a better user experience.
For issue 2, the best suggestion I have is to create a variable for number of items:
Code:
var lines = nlapiGetLineItemCount(‘item’);
for (i = 1; i <= lines; i++) {
var lc = nlapiGetLineItemValue('item', i, 'custcoltracklandedcost');
var wtax = nlapiGetLineItemValue('item', i, 'custcol_4601_witaxapplies');
if (lc == 'F' && wtax =='F')
{nlapiSetLineItemValue('item', i, 'custcol_4601_witaxapplies', 'T');
}
}
erictgrubaugh replied on 05/12/2017, 08:16 AM: Seconding the move to a UE script.
-
May 14, 2017 at 8:11 pm #5741
sneyI moved the script to a user event (before submit), and I created variables where possible. Thanks for those tips, it’s a cleaner implementation
It executes without error, however now while it now seems to be completing the loop on all lines, it does not source dependent fields on all lines…?
Results on same lines as in original post:
Line 1 – ‘Apply WH Tax ?’ ticked, all dependent fields sourced and WH Tax calculated (all correct)
Line 2 – ‘Apply WH Tax ?’ not ticked, no WH Tax field affected (all correct)
Line 2 – ‘Apply WH Tax ?’ ticked (correct), but no other fields sourced and WH Tax not calculated (incorrect)
Line 3 – ‘Apply WH Tax ?’ ticked (correct), but no other fields sourced and WH Tax not calculated (incorrect)
It’s ticking ‘Apply WH Tax ?’ on all lines so I know it’s looping right up until the last line, and also setting the field correctly.
My current user event script:
Code:
function BeforeSubmit(type)
{
if (type == ‘create’)
{
var newRecord = nlapiGetNewRecord();var lines = newRecord.getLineItemCount(‘item’);
for (i = 1; i <= lines; i++)
{
var tlc = newRecord.getLineItemValue('item', 'custcoltracklandedcost', i);if (tlc != "T") // If the item is not physically shipped
{
newRecord.setLineItemValue('item', 'custcol_4601_witaxapplies', i, 'T'); // Apply withholding tax
}
}
}
}
If it wasn't updating the check box on all lines I'd know it was actually stopping or exiting the loop – but that's not the case. After the first line, for some reason setting that field is not triggering dependent fields… -
May 15, 2017 at 7:30 am #5742
khultquistYou’re on the right track, but nlapiGetNewRecord is going to cause you problems
nlapiGetNewRecord()
Available in beforeLoad, beforeSubmit, and afterSubmit user event scripts. You are not allowed to submit the current or previous record returned by nlapiGetNewRecord.
Your code would be cleaner without reloading the record at all, just use the NS functions on the current record.
Code:
function BeforeSubmit(type)
{
if (type == ‘create’)
{
var lines = nlapiGetLineItemCount(‘item’);
for (i = 1; i <= lines; i++)
{
var tlc = nlapiGetLineItemValue('item', i, 'custcoltracklandedcost');if (tlc != 'T') // If the item is not physically shipped
{
nlapiSetLineItemValue('item', i, 'custcol_4601_witaxapplies', 'T') // Apply withholding tax
}
}
}
} -
May 15, 2017 at 4:51 pm #5743
sneyTried your suggestion thanks (except I always have the line number ‘i’ as the last parameter as per the Reference Guide) – now it still ticks each line correctly, but does not source dependent fields on any line, even Line 1!
Even if I have the code set the ‘WH Tax Code’ field as well, it still doesn’t fill in the other fields – and they can only be sourced, they are grayed out in the UI.
If I go back into Edit mode on the invoice and select any of the lines that have the check box ticked, it immediately fills in all required WH Tax fields…so it’s as if the UI triggers dependent sourcing but the script does not (although – in my previous code it did for the first line only).
Also tried the script on beforeLoad and it behaved the same…
-
May 16, 2017 at 8:03 am #5744
khultquist‘Apply WH Tax ?’ {custcol_4601_witaxapplies} appears to be a custom field… how exactly does it trigger the WH tax to calculate, is there another script or workflow?
-
May 16, 2017 at 4:33 pm #5745
sneyIt is part of the Withholding Tax bundle (ID 47459) offered by NetSuite. It also includes LOTS of client and user event script files.
Are you suggesting the way NetSuite has implemented withholding tax means it can only be used in the UI? I thought the general rule was as long as you imitate the UI in the right sequence, you can automate it as well? All you do in the UI after a transaction has loaded is go to a line, tick ‘Apply WH Tax ?’, and the rest of the fields fill in (obviously triggered by the client scripts that are part of the bundle).
So I would think setting this field in a script would do the same thing…or is that wishful thinking
-
May 16, 2017 at 8:25 pm #5746
khultquistOk that makes sense… User Event scripts will not trigger other UE scripts or client scripts, so you will have to go back to the client script (or possibly a scheduled script). Hopefully you can get the client script to work using variables and not exiting the loop prematurely.
-
AuthorPosts
You must be logged in to reply to this topic.