This topic contains 7 replies, has 0 voices, and was last updated by khultquist 7 years, 3 months ago.

  • Author
    Posts
  • #21675 Score: 0

    sney
    • Contributions: 0
    • Level 1

    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:

    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 stops

    Custom Action: ticks 'Apply WH Tax ?' on first line, all dependent WHT column fields are sourced, checks line 2 then stops

    First 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 updated

    I 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
    This is a cached copy. Click here to see the original post.

  • #21676 Score: 0

    khultquist
    • Contributions: 0
    • Level 1

    Hi. 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.

  • #21677 Score: 0

    sney
    • Contributions: 0
    • Level 1

    I 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…

  • #21678 Score: 0

    khultquist
    • Contributions: 0
    • Level 1

    You'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
    }
    }
    }
    }

  • #21679 Score: 0

    sney
    • Contributions: 0
    • Level 1

    Tried 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…

  • #21680 Score: 0

    khultquist
    • Contributions: 0
    • Level 1

    '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?

  • #21681 Score: 0

    sney
    • Contributions: 0
    • Level 1

    It 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

  • #21682 Score: 0

    khultquist
    • Contributions: 0
    • Level 1

    Ok 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.

You must be logged in to reply to this topic.