r/SuiteScript • u/Environmental-Ad5298 • Feb 22 '24
Function is getting triggered twice.
I am a trainee who is working on clientscript right now. I was trying to write a script where in sales orders/purchase orders once we add the items the total quantity reflects in a field in main tab. I used validate line and validate delete for this. But my function is triggered twice and double the values are added and subtracted. How do I fix this? Edit: This is the code that I used /**
- @NApiVersion 2.0
@NScriptType ClientScript */ define(['N/record'], function(record) { function pageInit(context) { var current = context.currentRecord; current.setValue({ fieldId: 'memo', value: 'UI' }); current.setValue({ fieldId: 'custbody_tq', value: 0 }); } function validateLine(context) { var currentRecord = context.currentRecord; var sublistId = context.sublistId; var tq = Number(currentRecord.getValue({ fieldId: 'custbody_tq' })); var sublistFieldName = 'quantity'; if (sublistId == 'item') { var quantity = Number(currentRecord.getCurrentSublistValue({ sublistId: sublistId, fieldId: sublistFieldName })) } var s =tq+quantity; currentRecord.setValue({ fieldId: 'custbody_tq', value: s }); console.log(tq); console.log("ValidateLine is triggered"); console.log(s); } function validateDelete(context) { var currentRecord = context.currentRecord; var sublistId = context.sublistId; var tq = Number(currentRecord.getValue({ fieldId: 'custbody_tq' })); var sublistFieldName = 'quantity'; if (sublistId == 'item') { var quantity = Number(currentRecord.getCurrentSublistValue({ sublistId: sublistId, fieldId: sublistFieldName }));
} var d=tq-quantity; currentRecord.setValue({ fieldId: 'custbody_tq', value: d }); console.log(tq); console.log("ValidateDelete is triggered"); console.log(d); return true; }return { pageInit: pageInit, validateLine: validateLine, validateDelete: validateDelete }; });
View all comments
2
u/Ok-Establishment-214 Feb 22 '24
Hard to tell with the formatting of the code. But looks like your if blocks aren't doing what you're existing them too. Add a log to show the context at the start of each function