user3681692
user3681692

Reputation: 11

NetSuite script to update one line level field from another after submit

First, I'm very new to scripting in NetSuite, so please forgive the scrappy code.

I'm trying to create a script (yes, I know this can be done in a workflow) that loops back through the lines of a sales order and copies the line 'amount' over to the 'altsalesamt' field after the order is saved. The script runs okay but doesn't actually do anything.

function afterSubmit_setASA(){
    var soItemCount = nlapiGetLineItemCount('item');

    for (var i = 1; i <= soItemCount; ++i) {
        var lineamt = nlapiGetLineItemValue('item', 'amount', i);

        if (lineamt != null && lineamt != '') {
            nlapiSetLineItemValue('item', 'altsalesamt', i, lineamt);
        }
    }
}

Can anyone point me in the right direction of what I may need to change or do? Any help is greatly appreciated!!

Upvotes: 1

Views: 3789

Answers (2)

eliseobeltran
eliseobeltran

Reputation: 577

You forgot to submit: nlapiSubmitRecord(...);

You can also try this code:

var record = nlapiLoadRecord(nlapiGetRecordType(), nlapiGetRecordId());
var soItemCount = record.getLineItemCount('item');

for (var i = 1; i <= soItemCount; ++i) {
    var lineamt = record.getLineItemValue('item', 'amount', i);

    if (lineamt != null && lineamt != '') {
        record.setLineItemValue('item', 'altsalesamt', i, lineamt);
    }
}

var id = nlapiSubmitRecord(record, true);

Upvotes: 0

erictgrubaugh
erictgrubaugh

Reputation: 8847

You are doing this in the After Submit event. At this point, the record has already been submitted to the database. You have two options:

  1. As @eliseobeltran said, you can load and submit the record first.
  2. Move your exact existing code to the Before Submit event instead.

I would recommend option 2, as this will use less governance, be more performant, and result in less database queries.

Upvotes: 2

Related Questions