Oh man ... there are a TON of searches I'd need to...
# suitescript
d
Oh man ... there are a TON of searches I'd need to rewrite ... I take it by that answer that
formula
fields in Map/Reduces are a big no no?
s
if you use a
label
on the columns, do they come through unscathed into
map()
?
s
labels do not go through at all in searches
s
I was curious if map/reduce was an exception for formula fields
I don't think I've done a MR with multiple formula fields, but since map just gets a serialized version of a search result, how would one handle 2 formula fields unless they are somehow distinguishable?
a label would be a reasonable way to distinguish
s
If I remember correctly, it only give you the last one unless you do the custom naming as battk suggested
s
what custom naming is there outside of custom labels?
s
Copy code
columns: [
        search.createColumn({
            name: 'formulatext_0',
            formula: '{entity}',
        }),
        search.createColumn({
            name: 'formulatext_1',
            formula: '{subsidiary}'
        })
    ]
If you make them both
name: 'formulatext'
, when it gets to map
result.values.formulatext
will only be the second one if I remember correctly
s
that [only the last formula survives] makes sense... and I guess in the case of formula the
name
property acts as a label?
s
Whenever you run any search in code, the label property is ignored afaik
s
nay, I use it all the time to assign JS-friendly names
for NFT
nsSearchResult2obj()
also, it appears the
asMappedResults()
native method does the same IIRC?
s
asMappedResults is not a thing for search I dont think
s
i.e. I think
asMappedResults()
will return labels as the object keys if set.
s
When you do search.run(), the result output has no properties related to labels at all that I know of? Edit: The
.columns
property is there (which has the label), but you cannot use
.getValue()
in conjunction with labels. And
toJSON()
on the result line sees the labels go away completely.
asMappedResults
uses the
alias
property in query, and I believe that is translated properly between getInput and map
s
nsSearchResult2obj
cleanly handles normal
search.Result
objects, using labels if present, if not falling back to the column name.
I presume many people have their own version of [nsSearchResult2Obj](https://exploreconsulting.github.io/netsuite-fasttrack-toolkit-ss2/modules/search.html#nssearchresult2obj)
s
yeah but the labels are not passed down between
getInput
and
map
if you just return a created search in an MR
s
aye, that is indeed a problem. Another way in which scheduled scripts are simpler/easier.
and 👎 on markdown syntax for links not seeming to work in a thread.
I'm moving towards SuiteQL for MR scripts. At least there this issue of 'label or no label' shouldn't be a concern, assuming you can express the needed formulas as plain SQL?
s
I've never tried passing suiteQL from getinput down into map, I'm pretty confident
query.create()
syntax does respect the
alias
param on the columns when passing down the map
a
No need to reference hardcoded formulatext field column name you can do:
Copy code
var aColumns = [];
var oColumnsDefinition = {};
for (var sKey in COLUMNS_MAP) {
   oColumnsDefinition[sKey] = search.createColumn(COLUMNS_MAP[sKey]);
   aColumns.push(oColumnsDefinition[sKey]);
}
If you don't pass the naked/raw result object to the map stage and you properly do that in getInput data you will have zero problem with multiple formulas.
In 2.1 you don't even need to create a separate object with the column definition, it would work out the box like this:
Copy code
const columns = [...Object.keys(oColumnsMap).map(sColumn => search.createColumn(oColumnsMap[sColumn]))];