What's the best way to validate results from a sel...
# suitescript
k
What's the best way to validate results from a select field in
search.lookupFields
? I need a way to only take action on soFields['shipmethod][0].value if soFields['shipmethod'][0] exists
u
Copy code
var shipmethod = soFields['shipmethod'][0] && soFields['shipmethod'][0].value
👍 1
💯 1
k
I'll try that, thanks\
Well, it didn't throw an error at least
What does that
&&
syntax do in this context?
I was using ternary syntax before this
u
that syntax is similar to ternary or an if statement
it's basically saying
Copy code
if(soFields['shipmethod'][0] && soFields['shipmethod'][0].value){
   var shipmethod = soFields['shipmethod'][0].value
}
k
ok so it's similar for
||
as an assignment syntax
u
yep
exactly
k
Cool, I learned something! 😄
It never ceases to amaze me that even after a couple decades of JavaScript, there's always more to learn
u
haha true
k
I improved it slightly because I need to do calculations on it later so it has to have a default value
Copy code
soShipMethod = soFields['shipmethod'][0] && soFields['shipmethod'][0].value || null;
      soShipMethodText = soFields['shipmethod'][0] && soFields['shipmethod'][0].text || ' ';
You're my hero today! Ok back to work.
s
soFields['shipmethod']?[0]?.value
u
has optional chaining been released with 2.1?
s
it's released with TypeScript for some time now
k
@stalbert Ooooh that's sweet!