VicDid VicDid - 1 year ago 76
Javascript Question

Retrieving the Line Item of an Old Record

I have a User Event Script that is deployed to Sales Orders. It uses the field values of line items to determine the quantity left. However, if I remove a line item it doesn't update the quantities of the removed item. I need the the item to be updated afterSubmit.

Is it possible to edit the quantities of the removed line item using

or something of that sort?

Here's what the code looks like:

function afterSubmit(){
var curRec = nlapiGetRecordId();
var item = nlapiLoadRecord('item', curRec);
var sold = item.getFieldValue('cust_sold');
var quantity = item.getFieldValue('cust_quantity');
var leftToSell = quantity - sold;
item.setFieldValue('cust_lefttosell', leftToSell);

var finalValue = item.getFieldValue('cust_lefttosell');
var old = nlapiGetOldRecord(); // only retrieves salesorder record


EDIT: So it turns out I can target the line items with a simple
old.getLineItemValue('item', 'item', linenum)
As Adolfo pointed out below, I can target the line item of the old record. For some reason I thought the only way to target it was by using
nlapiGetLineItemField(type, fldnm, linenum)
. The
version of the function was exactly what I was looking for.
This is what the code would look like:

var old = nlapiGetOldRecord();
var id = old.getLineItemValue('item', 'item', linenum);
var rec = nlapiLoadRecord('type', id);

Answer Source

Why not make cust_lefttosell a formula field so the value is always calculated on the fly from cust_quantity - cust_sold?

Anyways, if you want to use an afterSubmit then you have to iterate through all the line items in the old record, find out which ones were removed and then update the quantity on those.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download