Can anybody explain why the code in the reply sect...
# suitescript
l
Can anybody explain why the code in the reply section keeps logging the first variable? It gets logged a hundred times and gets this error: RangeError Maximum call stack size exceeded; ID:.
/**
* @NApiVersion 2.0
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/currentRecord'], function (currentRecord) {
function fieldChanged(context) {
var currentRecord = context.currentRecord;
if (context.fieldId === 'cseg1') {
var headerFinancialLoc = currentRecord.getValue({
fieldId: 'cseg1'
});
log.debug('headerFinancialLoc: ', headerFinancialLoc);
var lineCount = currentRecord.getLineCount({
sublistId: 'item'
});
log.debug('Line Count: ',lineCount)
for (var i = 0; i < lineCount; i++) {
var lineFinancialLoc = currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'cseg1',
line: i
});
log.debug('Line Financial Location: ', lineFinancialLoc)
if (!lineFinancialLoc) {
currentRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'cseg1',
line: i,
value: headerFinancialLoc
});
}
}
}
}
function lineInit(context) {
var currentRecord = context.currentRecord;
var headerFinancialLoc = currentRecord.getValue({
fieldId: 'cseg1'
});
currentRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'cseg1',
value: headerFinancialLoc
});
}
return {
fieldChanged: fieldChanged,
lineInit: lineInit
};
});
b
client script entry points can trigger other entry points
in this case, your lineInit is triggering your fieldChanged
and more troubling, your fieldChanged is triggering your fieldChanged
l
Hi @battk, thank you for explaining. Is there a way to stop the “loop”?
r
you can add a if lineFinancialLoc !== value of cseg1
b
your fieldChanged is triggering for 2 different fields, when you only want 1
your if statement at the top is not specific enough to distinguish them since they have the same field id
l
Thank you!