<@U9CS7LVCN> To get the most recent date instead o...
# suitescript
b
@scottvonduhn To get the most recent date instead of all the dates
s
If you are looking for the latest Purchase Order date for ALL inventory items, then you could just use the MAX summary function in your createColumn call for trandate. If you wanetd the latest Purchase Order date for EACH inventory item, then just add a GROUP function on internalid and itemid. The best way to do this is to build the search in the UI and use the Search Export extension in Chrome to give you the code for the search.
b
Thanks Scott, @scottvonduhn that was my first attempt but I found it would write every PO rate to the field I created, this was my code before. When I look in the system notes on the items, each time the script would run there would be multiple writes to the custom field.
and I am using the extension you mention, that is how I got the code for both searches.
s
right. you have no summary functions in your columns, so you are not getting aggregated results. Aggregating in the filter is just filtering out any inventory item that does not have a purchase order after 4/5/2019. It's not giving you only the latest trandate in the results. that requires putting the summary function on the column instead
You should be able to see that in the UI when you save & run the search. you need to get the results there to match the data you want in the script.
b
in my second example I have summary functions, unless I'm doing it wrong
s
yes, that one looks better. i was referring to the first one.
b
That was my first attempt but it would write multiple times to the field
s
Though, there should probably only be one MAX function. I think you want a GROUP function on internal id. Maybe not, though.
When you run the saved search in the UI, are you getting more than one result?
b
I tried both, I stuck with MAX because in the UI the internal id would not have a link on it with MAX to drill into results. I'm not getting more than one result in ui search
s
So, the UI gives you exactly one result, but you are getting more than one in the script, for the same search?
Just curious, if you save the search, then load and run it in the script, instead of creating it in code, does it also return multiple results?
b
Haven't tried that yet, that is a good thought
s
Also, based on the criteria, I'd expect more than one result, unless you only have one inventory item.
b
I am getting multiple results in the search, I wonder if I have to sort by name instead of date
s
Sorting shouldn't matter in the code, unless order of updates matters
b
it didn't matter
seems like this should be easier
s
to be clear on the results your script is seeing, try adding a logging statement right before the call to `record.submitFields`:
log.debug({ title: 'Item Internal Id: ' + item, details: 'Last PO Rate: ' + lastPORate });
then look through the execution logs to make sure they match the search results in the UI.
be sure that each Item internal id is only getting logged once
b
The internal id is there more than once. For now I've just sorted by item and then by date so the most recent date gets written last. I don't like it but it works for now
s
ah, i think i see the problem
because you include the Item Rate (transaction.rate) in a GROUP function, you are getting multiple results per inventory item.
b
Ok, I can't max it because then I'll get the highest one, not the most recent, right?
s
right, that won't work as expected
i am not sure how to structure what you want directly as a saved search. With SQL, I'd probably use ROW_NUMBER to order the search results, partitioned by internalid and ordered by date descending, and only return the results where ROW_NUMBER equals 1. By order the search results the way you did, you are essentially doing the same, though with many more updates to the item.
b
thank you for your help
s
You might be able to translate this into a Map/Reduce script that combined all the results together by internal id in the Map phase, and then in the Reduce phase select only the latest rate.
Ah, it is available now! It must have been released with 2019.2
Our upgrade was delayed until just last week because of issues NetSuite had during the upgrade
b
what is available?
s
The REST Web Services (Beta) feature
it was not there a few weeks ago in any of the 2019.1 accounts I had access to.
oops, sorry, I was trying to respond in a different window.
it was right next to this one
e
In terms of getting minimal/maximal values from summarized search results, you probably need to be using When Ordered By in your search. Here's the chapter straight from my Transaction Search cookbook
b
Thanks Eric, our Admin came up with a formula that seems to be working but your example is probably cleaner. MAX({transaction.rate}) KEEP(dense_rank last order by {transaction.trandate}, {transaction.datecreated})