I'm trying to get the sum of all the line amounts ...
# suitescript
m
I'm trying to get the sum of all the line amounts that meet a condition. This is OK. However, when I attempt to insert a new line I get an unexpected error (Error\n at RecordInvoker.insertLine). I want to keep the insert out of the loop as I only want to add one line:
Copy code
const item_count = current_record.getLineCount({
     sublistId: 'item'
});
const tax_item = '28709';
var sugar_tax = 0.025;
var item_quantity_sum = 0;
var item_amount_sum = 0;

for (var i = 0; i < item_count; i++) {
    const item_id = current_record.getSublistValue({
        sublistId: 'item',
        fieldId: 'item',
        line: i
    })

    const add_tax = current_record.getSublistValue({
        sublistId: 'item',
        fieldId: 'custcol_add_sugar_tax',
        line: i
    })

    if (add_tax == true) {

        var item_quantity = current_record.getSublistValue({
            sublistId: "item",
            fieldId: "quantity",
            line: i
        });

        var item_amount = current_record.getSublistValue({
            sublistId: "item",
            fieldId: "amount",
            line: i
        });

        item_quantity_sum += parseFloat(item_quantity);
        item_amount_sum += parseFloat(item_amount);

    } else {
        log.debug("Sugar Tax NOT Checked");
    }
} //End Loop

log.debug("Insert Line");
log.debug({title: "Quantity Sum", details: item_quantity_sum});
log.debug({title: "Amount Sum", details: item_amount_sum});

current_record.insertLine({
    sublistId: "item",
    line: item_count + 1
});

current_record.setSublistValue({
    sublistId: "item",
    fieldId: "item",
    line: item_count + 1,
    value: tax_item,
});

current_record.setSublistValue({
    sublistId: "item",
    fieldId: "quantity",
    line: item_count + 1,
    value: 1,
});

const total_sugar_tax = item_amount_sum * sugar_tax
log.debug({title: "Total Sugar Tax", details: total_sugar_tax});
current_record.setSublistValue({
    sublistId: "item",
    fieldId: "amount",
    line: item_count + 1,
    value: total_sugar_tax
});
b
the last index of a line with 5 lines is 4
your code is trying to insert at index 6, (5 + 1) which is skipping a row
beyond that issue, you dont need to insert a row at the new line index, netsuite will automatically do that when you start setting values on the new line's index
m
The insertLine requires a line. Should I not use this?
b
you dont need to insert line the new line
it will happen automatically
m
I see, thanks. So I removed the InsertLine and changed the setSublistValue to setCurrentSublistValue, but that complaint is not a function error. Does the record need loading first before saving?
b
setCurrentSublistValue is for dynamic records only
m
OK, cool - think it's working now. I'm using setSublistValue and my line count appears to work. Much thanks.
Just testing the various scenarios and I will need to be able to reset the sugar tax item (recalculate) in the event that the user adds another item that meets the tax condition. Is there a way to do this? I am thinking of deleting the tax line and re adding it.