This article is relevant if you are programming NetSuite Item Groups and you are experiencing strange and erratic behavior.
Background
One of our senior consultants came to me after pushing through a challenging set of client-side programming tasks with NetSuite item groups.
We do a fair amount of work with our clients with item groups as considerations for their NetSuite bundling requirements. Consider a previous article,
Understand NetSuite Item Groups vs. Kits to Produce Superior Reporting, to learn about the benefits of using Item groups.
Programming a NetSuite Price when Item is in a Specific Location
For our client, we had created a price lookup function based on the location of the item. We had created a cross-reference lookup table between the item and a NetSuite’s location table which would allow us to find and set a line item price. If no price was set between the item and the location, then this meant that the item is not available in that location.
Since the implementation a couple of years ago, the client had been happy with this functionality. They then needed to set up item groups in their business. The basic thinking was to copy the location that was set on the item group so that that it would apply to the constituent line elements as it expanded on the screen. This is where we started to have challenges.
As you add an item group item to a NetSuite line, you start with the item in the conventional sense, NetSuite then expands the lines automatically with the related items as defined in the item group component list, and finally, you get an “end-of-group” item (itemtype EndGroup).
To make this discussion easier, let’s call the line that was added (the group item) the GROUP HEADER, then actual items the GROUP ITEMS, and then finally the end-of-group line we’ll call the GROUP FOOTER.
NetSuite Item Group Client-Side Event Firing Limitations
The original script to lookup the price fired whenever an item was put on the line and when a location record has changed. These events fire using the conventional fieldchanged and postsourcing events. The thought process was that these events would fire when NetSuite expanded the lines. But they did not.
We discovered that NetSuite does fire the “recalc” event (in 2.0 it’s the sublistChanged event) a couple of times during the auto-expansion process. The problem is that it’s not a “unique” event. The first time it fires it is as the GROUP HEADER is added (but the GROUP ITEMS don’t exist yet). Then it fires it again after the GROUP ITEMS have been added (but before they even show up in the UI).
Our consultant was challenged as he didn’t want to look up the pricing multiple times – in other words, once the lookup logic for a line succeeded, he didn’t want to do it again, because the user might have overridden the price manually in the meantime. Since the “recalc” event fires repeatedly as lines are changed, and this event doesn’t tell you a specific line, he had no way of knowing whether it was firing because a group was being expanded, or for any other reason.
Ultimately, he had to add a custom column (hidden) which he would set in the recalc function as he was looking up item prices for the group – and then he knew that he didn’t need to do it again, even though the event fired again.
So the summary of this tip is to leverage NetSuite’s recalc event with your own custom dynamic hidden column field so you could keep track of what you are doing.
Strange Item Group Data Visibility
In the next challenge, the consultant assumed that he needed to copy the location from the GROUP HEADER down to each line, because when NetSuite expanded the group, the location set automatically on each line was NOT the location from the GROUP HEADER. Well, as it turns out, the location as seen in the UI was wrong, but the location in the actual data was right. In other words, NetSuite did the right thing (used the location from the GROUP HEADER on each line of the GROUP ITEMS), but on the screen it shows you a different location. You don’t see the “truth” until you save the record, and it refreshes the screen.
In this case, what you see on the screen may not be what you get. That can be a time waster. Argh.
Setting Values on NetSuite Item Group Lines Pecularities
The final challenge came in actually setting the values on the lines. Once the consultant knew where to add the logic (in the recalc event), he wrote a simple loop to process the GROUP ITEMS that were added. He would find the GROUP HEADER, and the GROUP FOOTER, and then had a “for” loop on the intervening rows. The weird thing was that on the first line of the GROUP ITEMS, NetSuite reported that the location was blank. Every other line of the group had a location, but not the first. And no matter what he did, he couldn’t set it (in other words, even if he set it, it remained blank despite the what you see is not what you get challenge). In fact, he couldn’t set any value on that line – even his hidden column which told him whether he had processed that line before.
So this line never received the right custom lookup price.
Now, if he subsequently edited the line manually, or if he added another item, the recalc event ran again as expected (it runs just about any change is made to the sublist), and this time it worked. It was only on the initial pass that the first line failed every time.
In desperation, he decided to try something different – he changed his “for” loop to run through the item group BACKWARDS (from the bottom to the top). And that suddenly worked! Every line now had a location and could be worked in a reliable fashion.
So the lesson here is if you are going to manipulate NetSuite item group lines in a set, do so by walking the list backward.
Join a Creative Group of NetSuite Innovators
Because this challenge was so troublesome, our consultant eagerly wanted to share with our other Prolecto technical team members his findings. My hope too is that this his findings will save you from a world of trouble. I am in gratitude for his tenacity, his instinct for willingness to share, and ultimately his competency to produce our client’s objectives.
One of the things I love about NetSuite is how the platform gives us the ability to innovate to solve our clients’ real-world business challenges. My clients say they value that we listen to their underlying concerns and we come up with inventive ways leveraging our background in accounting, operations and software development to produce creative solutions.
If you are a NetSuite professional who holds high standards for care, wishes to be appreciated for the way you think about challenges, seeks to not be alone yet still wants autonomy and flexible work situations, then let’s have a conversation.