<@UDEBTKW4A> What is your use case with that?
# suitescript
a
@Tyler What is your use case with that?
t
I need to do a search of all previous sales orders and get information about the line items (i.e. item, quantity, etc). When I run a search currently, it returns a separate result for each line item, but it would be much easier if I could return 1 search result for each Sales Order and have the line items for the Sales Order grouped in the result. I'm coming to the conclusion that this isn't possible, but I'm still holding out just a tiny bit of hope.
I've tried grouping by transaction number, but I get an unexpected error from NetSuite
j
I believe you will get what you want if you use a N/record. This should give you everything in the SO.
e
What data do you need from each line?
i.e. how would it be summarized in a single result?
a
@Tyler Transaction searches are driven by Main Line, if you criteria is Main Line = either, you would be able to show results from the main line(header/body) and the lines, which mean you will get for example as result row by row: Row1: Document Number(Sales Order Number) + Line Item1 + Line Quantity1. Row2: Document Number(Sales Order Number) + Line Item2 + Line Quantity2.
That just the way it works…
j
i think you could group by document number and use listagg sql function
in a formula
a
If you are doing this using script you can still do group by the document number at the script level grouping your results…
e
Might be best to wait to get a description of what we need to accomplish first
a
@jkabot Agree LISTAGG should work but that extremely inefficient and most likely not needed… plus I bet you it will reach some limit with orders with many lines.
t
So this is a rough sketch of what I would ideally like to accomplish: var results = [ { tranid: "SO123456", trandate: "6/17/19", items: [ { item: "Random Item", quantity: 15 }, { item: "Another random item", quantity: 20 } ] }, { tranid: "SO987654", trandate: "6/16/19", items: [ { item: "Random Item", quantity: 15 }, { item: "Another random item", quantity: 20 } ] } ]
j
Iterate through the search results and build that object
t
@jkabot that's what I'm currently doing, but due to the nature of things, this makes things very difficult
e
You'll have to build that structure yourself, grouping the data by tranid. Pretty straightforward with a library like
lodash
that already includes a
_.groupBy
method. Or you can do it yourself with maybe a
reduce
over the results
Depending on the result size and how critical performance is, I'd begin with something like
Copy code
var results = s.create(...).run().getRange(...);

results.map(toObject)
  .reduce(groupByTransaction)
  .forEach(processTransaction);
👍 1
t
Thanks for the help everyone
@erictgrubaugh I use Lodash but am still new to it - the groupBy option seems to be my best bet. Appreciate that
e
np
a
@Tyler I was a heavy user of lodash and use it all over the place, at the end sooner or later you may want to do or learn how to do those simple grouping by yourself with javascript.
t
@alien4u it's not that I don't know how to do it in vanilla javascript, it's just much cleaner using something like Lodash
a
@Tyler Loading a whole module/library to only do a search/array grouping is over kill in my opinion.
t
@alien4u A minified version of Lodash weighs in at 72KB and I use it throughout an entire project for many different things.
e
Yeah, I would absolutely load a whole library for a single method (e.g.
moment
)
That's soooo not the obstacle to performance in NetSuite