This topic contains 8 replies, has 0 voices, and was last updated by david.smith 7 years, 9 months ago.

  • Author
    Posts
  • #21889

    dmashburn3

    Hi Everyone,

    I'm trying to use this field change trigger for the first time. I've run into a situation where it is performing the action I desire, however, I have a prompt inside the script that ends up popping up continuously.

    Code:
    function onFieldChange(type,name){
    if(nlapiGetFieldValue('custbody_containerbillcount') != ''){
    var totalBillAmount = nlapiGetFieldValue('usertotal');
    var containerCount = nlapiGetFieldValue('custbody_containerbillcount');
    var memoField = nlapiGetLineItemValue('expense','memo',1);
    var x = confirm("Do you want to split bill amount of "+ totalBillAmount + " into "+ containerCount + " line items.");
    if( x == true){
    for(var j = 1; j <= containerCount; j++){
    nlapiLogExecution('debug','Details','j = '+j);
    nlapiSelectLineItem('expense',j);
    nlapiSetCurrentLineItemValue('expense','account',487);
    nlapiSetCurrentLineItemValue('expense','amount',parseFloat(totalBillAmount)/parseInt(containerCount));
    nlapiSetCurrentLineItemValue('expense','memo',memoField);
    nlapiSetCurrentLineItemValue('expense','class',45);
    nlapiSetCurrentLineItemValue('expense','department',1);
    nlapiSetCurrentLineItemValue('expense','location',1)
    nlapiCommitLineItem('expense');
    }
    } else {
    alert('Action cancelled');
    }
    }
    }
    When I change the field value custbody_containerbillcount it successfully prompts for the action, I click ok, it manipulates my lines, but then it prompts again.

    I assume this is because the script is still sitting there and seeing that the initial if condition is met? In which case, how do I write a condition for a field to have a number entered, but then exit once the actions are complete?

    Thanks
    This is a cached copy. Click here to see the original post.

  • #21890

    david.smith

    Use the name parameter.

    Code:

    function onFieldChange(type,name){
    if(name=='custbody_containerbillcount' && nlapiGetFieldValue('custbody_containerbillcount') != ''){

  • #21891

    dmashburn3

    Thanks David! Now all those samples I looked at make sense. I just needed someone to put it in some context.

  • #21892

    dmashburn3

    Now my script actually isn't doing any of my actions. It can't seem to get past the if statement.

    Code:
    function onFieldChange(name){
    nlapiLogExecution('debug','Details','Test1');
    if (name == 'custbody_containerbillcount' && nlapiGetFieldValue('custbody_containerbillcount') != ''){
    nlapiLogExecution('debug','Details','Test2');
    var totalBillAmount = nlapiGetFieldValue('usertotal');
    var containerCount = nlapiGetFieldValue('custbody_containerbillcount');
    var memoField = nlapiGetLineItemValue('expense','memo',1);
    var x = confirm("Do you want to split bill amount of "+ totalBillAmount + " into "+ containerCount + " line items.");
    if( x == true){
    for(var j = 1; j <= containerCount; j++){
    nlapiLogExecution('debug','Details','j = '+j);
    nlapiSelectLineItem('expense',j);
    nlapiSetCurrentLineItemValue('expense','account',487);
    nlapiSetCurrentLineItemValue('expense','amount',parseFloat(totalBillAmount)/parseInt(containerCount));
    nlapiSetCurrentLineItemValue('expense','memo',memoField);
    nlapiSetCurrentLineItemValue('expense','class',45);
    nlapiSetCurrentLineItemValue('expense','department',1);
    nlapiSetCurrentLineItemValue('expense','location',1)
    nlapiCommitLineItem('expense');
    }
    } else {
    alert('Action cancelled');
    }
    }
    }
    Execution log shows Test1 when the page is loaded, as well as another log when the line is changed, however it is not showing Test2?

  • #21893

    dmashburn3

    Also tried removing the second qualifier : nlapiGetFieldValue('custbody_containerbillcount') != ''

    Same result

  • #21894

    khultquist

    My guess is that nlapiGetFieldValue('custbody_containerbillcount') is NULL, but debug it like this:

    Code:
    function onFieldChange(name){
    var containerbillcount = nlapiGetFieldValue('custbody_containerbillcount');
    nlapiLogExecution('debug', 'name', name);
    nlapiLogExecution('debug', 'containerbillcount', containerbillcount);
    if (name == 'custbody_containerbillcount' && containerbillcount != ''){
    nlapiLogExecution('debug','Details','Test2');
    var totalBillAmount = nlapiGetFieldValue('usertotal');
    var containerCount = nlapiGetFieldValue('custbody_containerbillcount');
    var memoField = nlapiGetLineItemValue('expense','memo',1);
    var x = confirm("Do you want to split bill amount of "+ totalBillAmount + " into "+ containerCount + " line items.");
    if( x == true){
    for(var j = 1; j <= containerCount; j++){
    nlapiLogExecution('debug','Details','j = '+j);
    nlapiSelectLineItem('expense',j);
    nlapiSetCurrentLineItemValue('expense','account',487);
    nlapiSetCurrentLineItemValue('expense','amount',parseFloat(totalBillAmount)/parseInt(containerCount));
    nlapiSetCurrentLineItemValue('expense','memo',memoField);
    nlapiSetCurrentLineItemValue('expense','class',45);
    nlapiSetCurrentLineItemValue('expense','department',1);
    nlapiSetCurrentLineItemValue('expense','location',1)
    nlapiCommitLineItem('expense');
    }
    } else {
    alert('Action cancelled');
    }
    }
    }

  • #21895

    dmashburn3

    Interesting. The name is debugging 3 times on the page load.

    Debug
    containerbillcount
    2/1/2017
    1:29 pm
    Danny Mashburn

    Remove

    View
    Debug
    name
    2/1/2017
    1:29 pm
    Danny Mashburn
    landedcostsource5
    Remove

    View
    Debug
    Details
    2/1/2017
    1:29 pm
    Danny Mashburn
    Test1
    Remove

    View
    Debug
    containerbillcount
    2/1/2017
    1:29 pm
    Danny Mashburn

    Remove

    View
    Debug
    name
    2/1/2017
    1:29 pm
    Danny Mashburn
    landedcostsource3
    Remove

    View
    Debug
    Details
    2/1/2017
    1:29 pm
    Danny Mashburn
    Test1
    Remove

    View
    Debug
    containerbillcount
    2/1/2017
    1:29 pm
    Danny Mashburn

    Remove

    View
    Debug
    name
    2/1/2017
    1:29 pm
    Danny Mashburn
    landedcostsource2
    Remove

    View
    Debug
    Details
    2/1/2017
    1:29 pm
    Danny Mashburn
    Test1
    Remove

    Sorry for the table view, the image upload seems to be calling all my files corrupted.

    That's before I even try to edit the field. Any ideas?

    Also, I did get the script to work by passing the manipulations down into another function like this:

    Code:
    function onFieldChange(type, name){
    nlapiLogExecution('debug','Details','Test1');
    var containerbillcount = nlapiGetFieldValue('custbody_containerbillcount');
    nlapiLogExecution('debug', 'name', name);
    nlapiLogExecution('debug', 'containerbillcount', containerbillcount);
    if (name === 'custbody_containerbillcount'){
    doThis();
    }
    }

    function doThis(){
    nlapiLogExecution('debug','Details','Test3');
    var totalBillAmount = nlapiGetFieldValue('usertotal');
    var containerCount = nlapiGetFieldValue('custbody_containerbillcount');
    var memoField = nlapiGetLineItemValue('expense','memo',1);
    var x = confirm("Do you want to split bill amount of "+ totalBillAmount + " into "+ containerCount + " line items.");
    if( x == true){
    for(var j = 1; j <= containerCount; j++){
    nlapiLogExecution('debug','Details','j = '+j);
    nlapiSelectLineItem('expense',j);
    nlapiSetCurrentLineItemValue('expense','account',487);
    nlapiSetCurrentLineItemValue('expense','amount',parseFloat(totalBillAmount)/parseInt(containerCount));
    nlapiSetCurrentLineItemValue('expense','memo',memoField);
    nlapiSetCurrentLineItemValue('expense','class',45);
    nlapiSetCurrentLineItemValue('expense','department',1);
    nlapiSetCurrentLineItemValue('expense','location',1)
    nlapiCommitLineItem('expense');
    }
    } else {
    alert('Action cancelled');
    }
    }
    I don't really know why this works, and I'm curious about the landedcostsource2,3,5 but for now its working at least.

  • #21896

    khultquist

    With your latest code, you're no longer checking for

    nlapiGetFieldValue('custbody_containerbillcount') != ''

    so that's probably the difference.

  • #21897

    david.smith

    For client scripts I like to use the console for debugging rather than constantly switching tabs to see the script logs. You can also perform most of you code directly in the console to see the values.


    ironside replied on 02/23/2017, 10:36 PM: side note: If one uses NFT, you get console logging by default for any client script, server logging by default for server-side scripts.

You must be logged in to reply to this topic.