Important NetSuite Issue:: For the Scriptable Cart...
# suitecommerce
l
Important NetSuite Issue:: For the Scriptable Cart with SuiteCommerce and Recursion issue, I have implemented what ever given in the NetSuite's documentation, Note that I do not have any customizations, However When I changed the address on checkout page I can see that log from "customRecalc" i.e "nlapiLogExecution('DEBUG', 'action', action);" is printing 6 time, which means with the below code Recursion is still happening, This is very important issue on our end, I need help on this, can someone from NetSuite team give me idea on how I can handle this? Please update the below code only as, I will use that for my another implementation. Thanks in Advanced Link- https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/article_0151255693.html#subsect_30145728616 code-
Copy code
// Global variable to prevent recursion
var IS_PROCESSING = false;

// Custom Recalculation function
function customRecalc(type, action) {
    if (nlapiGetContext().getExecutionContext() !== 'webstore') {
        return true;
    }
    nlapiLogExecution('DEBUG','type',type);
    if (type === 'item' && (action === 'commit' || action === 'remove')) {
        if (IS_PROCESSING) {
            return true;
        }

        try {
            IS_PROCESSING = true;
            // The specific function you want to execute
            nlapiLogExecution('DEBUG', 'action', action);
        } catch (e) {
            nlapiLogExecution('ERROR', 'Error in customRecalc', e.toString());
        } finally {
            IS_PROCESSING = false;
        }
    }
}

// Custom Field Change function
function customFieldChange(type, name) {
    if (nlapiGetContext().getExecutionContext() !== 'webstore') {
        return true;
    }

    if (name === 'shippingcost') {
        if (IS_PROCESSING) {
            return true;
        }

        IS_PROCESSING = true;
        nlapiSetFieldValue('shippingcost', 0);  // Set shipping cost to 0 under certain conditions
        IS_PROCESSING = false;
    }
}

// Custom Post Sourcing function
function customPostSourcing(type, name) {
    if (nlapiGetContext().getExecutionContext() !== 'webstore') {
        return true;
    }

    if (name === 'shippingtaxcode') {
        if (IS_PROCESSING) {
            return true;
        }

        IS_PROCESSING = true;
        nlapiSetFieldValue('shippingtax1rate', 0);  // Set shipping tax rate to 0 under certain conditions
        IS_PROCESSING = false;
    }
}
s
It's been a while since I worked with scriptable cart - but, is it printing six items because you have six line items in the sales order?
l
@Steve Goldberg, NO I only have one line item, and two addresses, When I changed the address on checkout page it's printing 6 time. It always prints 6 times, that mean code recursion is still happening
I have another account with only plan SCA and not other customization, I tried this code there but Yes, there is Recursion issue I feel..
I have some another customization which I am working on with scripable cart must needed, and this is causing slowness issue with that customization, I am worried now.
I need a help, I think documentation is missing something, OR I am missing something.
@Steve Goldberg Sorry to bother you and tagged directly here, Do you have any idea on fixing this?
s
I don't, sorry
n
I don't see the full picture with this but suspect there's 6 times the code is entered and as you initialise IS_PROCESSING to false it's able to run the code 6 times. Why the code is being entered 6 times I don't know, and I'm no SCA dev, but, each time the code is initiated you reset the global variable, so that might be the issue. I don't know enough about SCA and the architecture to help you debug further, other than to say log out the value of the IS_PROCESSING directly after it's declared and see if it logs out as false 6 times.
l
@NElliott, I updated the code based on your suggestion see below, However logs are still printing 6 time, and it always prints 6 time in any case that mean what ever you written withing that code block runs 6 time in case of SCA and Scriptable cart, you can find more detail about this issue here- https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/article_0151255693.html#subsect_30145728616
Copy code
// Global variable to prevent recursion
var IS_PROCESSING = false;

// Custom Recalculation function
function customRecalc(type, action) {
    if (nlapiGetContext().getExecutionContext() !== 'webstore') {
        return true;
    }
    

    if (type === 'item' && (action === 'commit' || action === 'remove')) {
        if (IS_PROCESSING) {
            return true;
        }

        try {
            IS_PROCESSING = true;
            // The specific function you want to execute
            nlapiLogExecution('DEBUG', 'IS_PROCESSING initial', IS_PROCESSING);
            nlapiLogExecution('DEBUG', 'action', action);
        } catch (e) {
            nlapiLogExecution('ERROR', 'Error in customRecalc', e.toString());
        } finally {
            IS_PROCESSING = false;
        }
    }
}

// Custom Field Change function
function customFieldChange(type, name) {
    if (nlapiGetContext().getExecutionContext() !== 'webstore') {
        return true;
    }
   
    if (name === 'shippingcost') {
        if (IS_PROCESSING) {
            return true;
        }

        IS_PROCESSING = true;
        nlapiSetFieldValue('shippingcost', 0);  // Set shipping cost to 0 under certain conditions
        IS_PROCESSING = false;
    }
}

// Custom Post Sourcing function
function customPostSourcing(type, name) {
    if (nlapiGetContext().getExecutionContext() !== 'webstore') {
        return true;
    }
   
    if (name === 'shippingtaxcode') {
        if (IS_PROCESSING) {
            return true;
        }

        IS_PROCESSING = true;
        nlapiSetFieldValue('shippingtax1rate', 0);  // Set shipping tax rate to 0 under certain conditions
        IS_PROCESSING = false;
    }
}
s
Small thing but where you have these:
Copy code
// From this
if (IS_PROCESSING) {
    return true;
}

// To this
if (IS_PROCESSING) {
    return;
}
Could you change them to just
return
please? Returning
true
here could be messing with it when I think you need to be returning
undefined
n
I don't actually see what you changed.
var IS_PROCESSING = false;
is outside your functions and I do not know if this code as a whole is being run repeatedly and if it is, that variable is potentially being reset to false each time before it's evaluated in the functions. Sorry I'm not familiar with the script and architecture to know, I just hoped that talking about that might give you a 💡 moment. However, if anyone can figure it for you @Steve Goldberg is your man 🙂
❤️ 2
l
Ohhh, Yes, I think I missed that.....Let me try it..
s
Another idea is to add in a lot more logs at various steps in your custom recalc function, logging the type and action each time (including whether IS_PROCESSING is true or not)
l
Sure, will do that as well, I am trying suggested code, However seems not working, it is still printing that logs 6 times.
Copy code
if (IS_PROCESSING) {
    return;
}
Steve, can we use some body field or something like that? instead Global variable?
Copy code
// Global variable to prevent recursion
var IS_PROCESSING = false;

// Custom Recalculation function
function customRecalc(type, action) {
    if (nlapiGetContext().getExecutionContext() !== 'webstore') {
        return true;
    }
    

    if (type === 'item' && (action === 'commit' || action === 'remove')) {
        if (IS_PROCESSING) {
            return;
        }
        try {
            IS_PROCESSING = true;
            nlapiLogExecution('DEBUG', 'IS_PROCESSING initial', IS_PROCESSING);
            nlapiLogExecution('DEBUG', 'action', action);
        } catch (e) {
            nlapiLogExecution('ERROR', 'Error in customRecalc', e.toString());
        } finally {
            IS_PROCESSING = false;
        }
    }
}

// Custom Field Change function
function customFieldChange(type, name) {
    if (nlapiGetContext().getExecutionContext() !== 'webstore') {
        return true;
    }
   
    if (name === 'shippingcost') {
        if (IS_PROCESSING) {
            return;
        }

        IS_PROCESSING = true;
        nlapiSetFieldValue('shippingcost', 0);  // Set shipping cost to 0 under certain conditions
        IS_PROCESSING = false;
    }
}

// Custom Post Sourcing function
function customPostSourcing(type, name) {
    if (nlapiGetContext().getExecutionContext() !== 'webstore') {
        return true;
    }
   
    if (name === 'shippingtaxcode') {
        if (IS_PROCESSING) {
            return;
        }

        IS_PROCESSING = true;
        nlapiSetFieldValue('shippingtax1rate', 0);  // Set shipping tax rate to 0 under certain conditions
        IS_PROCESSING = false;
    }
}
s
I mean, yeah you can set transaction body fields during this process. I'm also trying to remember if you can set session variables in scriptable cart
👍 1
I can't remember if you've already answered this, but you don't have any other scripts running do you - user scripts for example?
l
NO NOT AT ALL, I don't have any like any script running in my account, only this script I have in my account
I am specifically using this account to fix the recursion issue, because of we have many customers asking with same issue on their SCA webstore.
and we figure out performance is impacting due to scriptable cart and this Recursion issue.