Skip to content


SuiteScript Tip #1. nlapiGetCurrentLineItemValue() vs nlapiGetLineItemValue()

Stumbled upon Scripting tip-25:Difference between nlapiGetCurrentLineItemValue vs nlapiGetLineItemValue earlier today— Apparently I am not alone when it comes to understanding the oddities of SuiteScript. I have a couple small insights I came across and decided it was worth posting my own version of the subject.

When dealing with sublists for a NetSuite record, there are two ways of accessing line items: [1] nlapiGetCurrentLineItem___(type, field) and [2] nlapiGetLineItem___(type, field, lineNum). These two functions perform the same in most contexts (when available). The crucial difference between these two APIs is in Client Scripts. Because of the order of function calls in how NetSuite handles Client events, e.g. fieldChanged, Recalc, lineInsert, etc., the two sets of APIs can refer to different sets of values.

For fieldChanged, or Recalc scripts, the nlapiGetCurrentLineItem___() functions will refer to the last values before the event occurred, while the nlapiGetLineItem___() will refer to the new current clean model of the data.

For example, if you are working on a SalesOrder and change the quantity 123 to 456, you will have the following in the fieldChanged() function:

> nlapiGetCurrentLineItemValue('item', 'quantity' ) = 123

> nlapiGetLineItemValue( 'item', 'quantity', line ) = 456

This oddity in behavior will be applied both in fieldChanged and Recalc functions. While this seems a bit confusing, there is a parallel that can be seen while using the UI. When using the UI, a fieldChanged event will be called after any field is changed, including columns in a line item, e.g. changing the quantity for a line item. The line is not committed until the Add/Edit button is clicked. This event corresponds to the validateLine event.

Similarly, when you remove an item, the nlapiGetCurrentLineItem___() functions will point to the previous (and now removed) line item. Assume we start with 3 line items, and then proceed to remove the first item. In the Recalc function call when the item is removed, the lines will report the correct new number:

> var totalLines = nlapiGetLineItemCount('item') = 2;

while calls to nlapiGetCurrentLineItem___() will return the old removed lined:
> nlapiGetCurrentLineItemValue('item', 'item' ) = Item#1
> nlapiGetCurrentLineItemValue('item', 'quantity' ) = 123

and calls to nlapiGetLineItem___() will return the values for line #2.

> nlapiGetLineItemValue('item', 'item', 1 ) = Item#2
> nlapiGetLineItemValue('item', 'quantity', 1 ) = 456

Odd but true..

In the code I have been working on, a particular line item can have a dependent line item following it. So when the item is removed, the Recalc handler has the following code:

1
2
3
4
5
6
7
8
function recalc(type, name ) {
  if (type == 'item' && name == 'remove') {
    var lineNum = parseInt (nlapiGetCurrentLineItemIndex('item')); //same line as current
    if (nlapiGetCurrentLineItemValue('item', 'item') == '1000') { //check value of removed item
      nlapiRemoveLineItem('item', lineNum ); //removes line following line of what was just removed
    }
  }
}

It makes sense when you understand the NetSuite model, but it still is confusing at times without the comments.

Posted in NetSuite.

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.