I have a search with multiple (complicated) formul...
# suitescript
a
I have a search with multiple (complicated) formulas When I export or load the search and do a getValue on the results I am getting results from one of the other formulas on the same search. How do I specify which one I want to get the results from?
s
When looping over your search results, use the result.columns property to differentiate, e.g.
Copy code
searchObj.run().each(function (result) {
    result.getValue(result.columns[0]) //formula 1
    result.getValue(result.columns[1]) //formula 2
    return true;
}
result.columns is an array of all the columns in your search
b
Copy code
result.getValue(
  search.createColumn({
    name: "formulatext",
    summary: "MAX",
    formula: "formula 1 code here"
  })
);
should work too
a
@Sandii So I am supposed to just get the values in the order they were created? That's pretty dangerous, isn't it? @battk I should createColumn in the mapping of the results?
b
ideally its the same column you used to make the search
s
You can put the exact formula in there as battk suggested, you could also use the toJSON method on th results and it will append 1,2 etc to duplicates (i don’t see that is less dangerous?)
a
@battk suggestion worked, although it seems weird to me that I am creatingColumns after the search has already run. @Sandii That doesn't seem less dangerous to me.
@Sandii @battk Thanks for you help guys!
b
its not really any different than when you pass in parameters to getValue
the object you pass in is basically the parameters you use to create a search column
a
@battk I think that was the first time you answered me without giving me a link to docs... 😉 I was passing along those params all along, but just as object without the createColumn
Thats how I always do it in a search, but I guess formulas need more specificity
b
using a column as a parameter is in the docs, but the docs dont specify that you have to use a column object for formulas, so no docs
🤣 1
s
I personally would just make an array/object before creating the search, add an alias property to each object, and loop that object to create the search and read the results.
Then you know add them in the exact same order you read them
a
@Sandii That sounds much safer.
e
Straight from my Advanced Search cookbook:
Untitled
Basically if you use the
Result
instance's
toJSON()
method, each formula column of a particular type after the first will get a
_N
appended, where
N
starts at
1
Just in case you're like me and often keep the Search definition and the Result processing in completely separate scopes
a
@erictgrubaugh But isn't @battk's method safer? Here if someone adds a new formula to the columns (in a larger search) without accounting for the exact order in the resultToObject, the results will be skewed.
b
if you fear people changing the order of columns, then specifying the formula is safer
e
It might be safer only if you keep the references to your initial
Column
instances around or process the results in the same scope in which the
Search
is defined. This is almost never the case for me, in which case I'd have to duplicate the code which creates the
Column
instance, which seems just as unsafe.
b
if you fear people changing the formula, or the summary type, then the toJSON is probably safer
pick what you fear your users will change
honestly do the search in code if you fear them changing your search
e
Yeah if the search isn't entirely in code, you're basically SOL on safety anyway
I'm not trying to diminish or invalidate your concerns, but you're sort of in a pick-your-poison situation
a
I already do the search entirely in code. I'm talking about when pulling out the values in the map/each afterwards. I just always feel safer with keys in an object then indexes in an arrays (or in this case naming the column rather then getting the index of the formula) . But I don't know, maybe thats just me.
b
you have no choice in a map/reduce
you wont have a getValue method
and whatever netsuite uses to stringify search results is not smart enough to handle multiple formula fields
you have to run the search yourself
returning the search results as the return value of getInputData is basically the same as calling .toJSON on each of them, so you have to refer to each formula by things like formulatext_1
you can also transform(array.map works nicely here) each search result into your own object