netsuitesuitescript2.0

How to update custom column field on just created transaction


I have a SS2 user event script which runs on after submit when an Item Fulfillment record is created.

There is a custom column field, custcol_sp_itf_cost with data type Currency, on the Item Fulfillment record. The field has Store Value checked.

The following code produces logging to show that it runs through the lines but doesn't set the value of the field. Does someone know why?

(I've removed the logging code here for brevity.)

function afterSubmit(context)
{
    var lineCount = context.newRecord.getLineCount({ sublistId: 'item' });

    for (var i = 0; i < lineCount; i++) {

        context.newRecord.setSublistValue({
            sublistId: 'item',
            fieldId: 'custcol_sp_itf_cost',
            line: i,
            value: 1234
        });
    }
}

Solution

  • When running code in an afterSubmit event, the record has already been submitted to the database, so any attempt to update newRecord directly will not work. You've got two options:

    1. Move your code to the beforeSubmit event, in which case, setSublistValue will work just like you're trying to do. This would be the recommended approach.
    2. Load the newly created record with record.load() then call setSublistValue(), then call record.save(). You can get the record id from context.newRecord in the afterSubmit event.

    The second option would NOT be the recommended approach since re-loading the record and saving it again is much slower than just updating your value in beforeSubmit and letting NetSuite save the record for you one time.