1
votes

I am receiving an error in NetSuite and trying to resolve. Goal is to calculate the total sales order weight and post as a stored value. I am calculating each line item weight but struggling with the script to sum all lines. I have tried this script as a UserEventScript and ClientScript - Please Help!

 *@NApiVersion 2.0
 *@NScriptType UserEventScript
 */
define([],
    function() {
        function afterSubmit(context) {
            var objRecord = context.currentRecord;
            var lines = objRecord.getLineCount({sublistId: 'item'});
            var totalWeight = 0 ;
            for (var i = 1; i < lines+1; i++) {
                var weight = objRecord.getSublistValue({sublistId: 'item', fieldId: 'custcol_individual_weight', line: i});
                var quantity = objRecord.getSublistValue({sublistId: 'item', fieldId: 'quantity', line: i});
                var weightTimesQuantity = weight * quantity;
                totalWeight = totalWeight + weightTimesQuantity ;
            }
            objRecord.setValue({fieldId: 'custbody_items_total_weight',value: totalWeight});
        }
        return {
            afterSubmit: afterSubmit
        };
    });````
1
I could be wrong, and often am, but I think that the getLineCount is zero index based and doesn't start at 1 in SuiteScript 2.0.Simon Delicata

1 Answers

0
votes

Since you are trying to update the record you'll want to make that a beforeSubmit event script.

Also unless you have ensured that all current and future items do have a weight you should have a fallback for the case where the item doesn't have a weight entered.