it goes into an infinite loop because it doesn't c...
# ask-stanley-ai
n
it goes into an infinite loop because it doesn't commit the line /** *@NApiVersion 2.x *@NScriptType ClientScript */ define(['N/log', 'N/ui/dialog','N/record', 'N/search','N/currentRecord'], function(log, dialog, record, search,currentRecord) { function getItemPromo(itemId) { var promoSearch = search.create({ type: search.Type.ITEM, filters: [ ['internalid', 'is', itemId] ], columns: ['custitem1', 'custitem2'] }); var searchResult = promoSearch.run().getRange({ start: 0, end: 1 }); if (searchResult.length > 0) { return { promoQty: searchResult[0].getValue('custitem1'), giftedQty: searchResult[0].getValue('custitem2') } } else { return null; } } function validateLine(context) { var currRec = currentRecord.get(); var sublistName = context.sublistId; if (sublistName === 'item') { var itemId = currRec.getCurrentSublistValue({ sublistId: 'item', fieldId: 'item' }); log.debug({ title: "itemId", details: itemId }); var itemTax = currRec.getCurrentSublistValue({ sublistId: 'item', fieldId: 'taxcode' }); log.debug({ title: "itemTax", details: itemTax }); var itemPromo = getItemPromo(itemId).promoQty; log.debug({ title: "itemPromo", details: itemPromo }); if(itemPromo) { var itemLineQuantity = currRec.getCurrentSublistValue({ sublistId: 'item', fieldId: 'quantity' }); log.debug({ title: "itemLineQuantity", details: itemLineQuantity }); var itemGiftedQty = getItemPromo(itemId).giftedQty; var calcPromo = (Number(itemLineQuantity) / Number(itemPromo)) * itemGiftedQty ; log.debug({ title: "calcPromo", details: calcPromo }); if(calcPromo && (Number(calcPromo) > 0)) { var lineCount = currRec.getLineCount({ sublistId: 'item' }); log.debug({ title: "lineCount", details: lineCount }); var currLineIndex = currRec.getCurrentSublistIndex({ sublistId: 'item' }); log.debug({ title: "currLineIndex", details: currLineIndex }); log.debug({ title: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2" }); for (var i = lineCount; i >= 0; i--) { currRec.selectLine({ sublistId: 'item', line: i }); currRec.commitLine({ sublistId: 'item' }); } currRec.selectLine({ sublistId: 'item', line: 0 , ignoreRecalc: true}); currRec.selectNewLine({ sublistId: 'item' }); currRec.setCurrentSublistValue({ sublistId: 'item', fieldId: 'item', value: itemId }); currRec.setCurrentSublistValue({ sublistId: 'item', fieldId: 'quantity', value: calcPromo }); currRec.setCurrentSublistValue({ sublistId: 'item', fieldId: 'amount', value: 0 }); currRec.setCurrentSublistValue({ sublistId: 'item', fieldId: 'taxcode', value: 11 }); currRec.commitLine({ sublistId: 'item' }); } } } return true; } // function commitLine(context) { // if (validateLine(context)) { // context.currentRecord.commitLine({sublistId: 'item'}); // var currRec = context.currentRecord; // var itemId = currRec.getCurrentSublistValue({ // sublistId: 'item', // fieldId: 'item' // }); // log.debug({ // title: "itemId", // details: itemId // }); // var itemPromo = getItemPromo(itemId).promoQty; // log.debug({ // title: "itemPromo", // details: itemPromo // }); // var itemLineQuantity = currRec.getCurrentSublistValue({ // sublistId: 'item', // fieldId: 'quantity' // }); // log.debug({ // title: "itemLineQuantity", // details: itemLineQuantity // }); // var itemGiftedQty = getItemPromo(itemId).giftedQty; // var calcPromo = (Number(itemLineQuantity) / Number(itemPromo)) * itemGiftedQty ; // log.debug({ // title: "calcPromo", // details: calcPromo // }); // currRec.selectNewLine({sublistId: 'item'}); // currRec.setCurrentSublistValue({ sublistId: 'item', fieldId: 'item', value: itemId }); // currRec.setCurrentSublistValue({ sublistId: 'item', fieldId: 'quantity', value: calcPromo }); // currRec.setCurrentSublistValue({ sublistId: 'item', fieldId: 'amount', value: 0 }); // currRec.setCurrentSublistValue({ sublistId: 'item', fieldId: 'taxcode', value: 11 }); // currRec.commitLine({sublistId: 'item'}); // } // else // { // context.currentRecord.commitLine({sublistId: 'item'}); // } // } return { validateLine: validateLine }; });
c
AI generated answer (using internal Slack threads):
The provided code is a NetSuite SuiteScript 2.x Client Script. The script is designed to validate line items on a record. The function
validateLine(context)
is triggered every time a line item is added or edited. The script seems to be going into an infinite loop because it's trying to commit a line within a loop that iterates over the lines of the same sublist. This is happening in the following section of the code:
Copy code
javascript
for (var i = lineCount; i >= 0; i--) {
    currRec.selectLine({ sublistId: 'item', line: i });
    currRec.commitLine({ sublistId: 'item' });
}
The
commitLine
function is used to save changes to the current line in a sublist. However, in this case, it's being used within a loop that's iterating over the lines of the same sublist. This could be causing the script to go into an infinite loop as it's constantly trying to save changes to the lines while iterating over them. To fix this issue, you might need to restructure your code to avoid committing a line within the loop that iterates over the lines of the same sublist. You could possibly move the
commitLine
function outside of the loop, so it's only called once after all necessary changes to the sublist lines have been made. Remember, it's important to thoroughly test any changes to ensure they work as expected and don't introduce new issues. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.875090897
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.87387681
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.871067643
Please provide your feedback on this answer.