Hi guys, hope you're well! Question, how can i acc...
# suitescript
b
Hi guys, hope you're well! Question, how can i access a field inside a saved search that contains a formula?
e
Given a Search defined like:
Copy code
const lateEntriesSearch = s.create({
  type: s.Type.TIME_BILL,
  filters: [
    [`formulanumeric: ${daysElapsedFormula}`, s.Operator.GREATERTHAN, 7]
  ],
  columns: [
    'employee',
    'date',
    'datecreated',
    {
      name: 'formulanumeric',
      formula: daysElapsedFormula
    }
  ]
})
You could retrieve the formula column like so:
Copy code
result.getValue({ name: 'formulanumeric' })
If you have multiple
formulanumeric
columns, you have to be a bit more creative
b
Hi! Thank you for your response. I have this:
Copy code
search.createColumn({
   name: "formulanumeric",
   summary: "MAX",
   formula: "{netamount} ",
   label: "Net Amount"
})
When i access the saved search i get this:
Copy code
values: {
      "MAX(formulanumeric)": "1198.5"
   }
e
You have a
Summary
on your Column, so you need to retrieve it using
getValue
with the
Summary
specified as well. In general, you retrieve a Column value by calling
getValue
with the same parameters as you called
createColumn
. In this case,
Copy code
result.getValue({ name: 'formulanumeric', summary: s.Summary.MAX })
b
Oh I see. So in this case, the s in the s.Summary.MAX refers to which variable?
Search?
e
Oh sorry, that's the
Search
module
b
I used search and it did not work.
e
It contains an enumeration for all the
summary
options
b
Let me try again 🙂
b
What would you do when you have more than one column with a "formulanumeric"?
e
There are probably multiple ways. I use the `Result`'s
toJSON()
method like so:
Copy code
const results = lateEntriesSearch.run().getRange({ start: 0, end: 1000 })
const lateEntries = results.map(resultToObject)

function resultToObject (result) {
  const res = result.toJSON()
  console.log(res)
  return {
    employeeName: result.getText({ name: 'employee' }),
    daysElapsed: res.values.formulanumeric,
    daysElapsedNoRound: res.values.formulanumeric_1
  }
}
Replace the
console.log
with the
log
module if this is server-side
(or delete it)
b
Ahhh I see, that's great!
Thank you so much!
❤️ 1
e
I desperately wish the
search
module had a method similar to the
query
module's
asMappedResults()
b
I do not think i have used the query's module. That one is similar to the search, i am assuming? I will look at it in a bit 🙂