nlapiSetFieldValue() and its underrated 3rd parameter

For this post I am going to discuss about a NetSuite function called nlapiSetFieldValue(). So first of all, what is this function and how do I use it?

The nlapiSetFieldValue() function as it speaks for itself, takes a user-defined value and passes it as a value to a defined internal id of a field in a NetSuite record. You can use it on any script types in NetSuite. (I will not enumerate them all here, but maybe on my next post I will *wink*)

However this function has a 3rd and 4th parameter which in my experience is I seldom see on where it should be. Client Scripts.

PS: I will focus my discussion on the 3rd parameter only.

The function’s 3rd parameter “firefieldchanged” takes a boolean value and does the following:

  • If set to true, the function will trigger the fieldchange function for the specified internal id. This is the default value of the parameter if not set.
  • If set to false, the function will just set the field value to what is defined by user and stops.

So, why am I telling this and where is my point coming from?

Suppose you have this code:

function fieldChanged(name)
{
switch(name)
{
case “fielda”:
somefunc();
break;
case “fieldb”:
nlapiSetFieldValue(“fielda”, “what is the value”);
break;
default: break;
}
}

function somefunc()
{
// code logic..
nlapiSetFieldValue(“fieldb”, “so the value is this”);
}

There is nothing wrong here syntactically, right? But if you try to execute it:

nlapisetfieldvalue1

And it breaks the process.

Why? because we overlooked the 3rd parameter in our nlapiSetFieldValue() on the someFunc() function which in return calls the fieldchanged function of the called field (fieldb) that is setting the value of the someFunc() caller (fielda) field which in return calls again the someFunc() function which in return… (STACK OVERFLOW)

To finish this:

function somefunc()
{
// code logic..
nlapiSetFieldValue(“fieldb”, “so the value is this”, false);
}

We added a third parameter on the function, which tells it to not call the fieldChanged function of the said field, thus leading you to a stop process. (No more infinite loops!)

So the lesson here is, be aware of your code logic and syntax, the nlapiSetFieldValue might be a trivial function however when not used right may lead you to a path of long bug-finding since the error that will be given to you would be generic and no logs of this event will ever be traced by the NetSuite Support Team.

Hope this helps! Happy coding~

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s