https://netsuiteprofessionals.com logo
#suitescript
Title
# suitescript
m

mg2017

04/03/2022, 11:58 PM
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

battk

04/04/2022, 3:21 AM
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

mg2017

04/04/2022, 4:24 AM
The insertLine requires a line. Should I not use this?
b

battk

04/04/2022, 6:58 AM
you dont need to insert line the new line
it will happen automatically
m

mg2017

04/04/2022, 8:43 AM
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

battk

04/04/2022, 8:44 AM
setCurrentSublistValue is for dynamic records only
m

mg2017

04/04/2022, 9:37 AM
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.
3 Views