Yep that was the root cause... is working now... e...
# suitescript
a
Yep that was the root cause... is working now... end up with:
e
Meant to start a thread not just comment on the snippet
My question is about the ternary; when will it ever return
null
?
You effectively have
Copy code
if (A && B) {
  data[key] = A ? oneThing : null;
}
In order to even execute your ternary statement,
A
has to be `true`; it will never be anything else, so the ternary will always return
oneThing
@alien4u ^^
a
data[key] = results[map[key]] ? results[map[key]][0].value : null;
Look like I dont understand this entirely I would check to understand it better...
e
I'm completely guessing, but maybe you wanted
results[map[key]][0] ?
(add the
[0]
)
a
Evene if I try like this:
data[key] = results[map[key]].length ? results[map[key]][0].value : results[map[key]];
Or this:
data[key] = results[map[key]][0] ? results[map[key]][0].value : results[map[key]];
I don't get the Booleans
e
And you do the other way?
a
I think I got it.. wait...
e
A boolean value stored in
results[map[key]]
will never go through that ternary statement at all because it doesn't have a
length
property, so your
if
will be false and it will go through the
else
a
Yep now is working...
How do you insert code tags into a thread?
e
just surround with three backticks
a
Copy code
for (var key in map) {
			if (results[map[key]]) {
				data[key] = results[map[key]].length ? results[map[key]][0].value : data[key] = results[map[key]];
			}
		}
e
Basically you're iterating over this
map
to pull out the elements themselves or for any elements that are Arrays, grab the
value
property from the first element
a
Yep is working ok now I think this snippet could be use to iterate over any lookupFields result set...
e
How about
Copy code
for (var key in map) {
  if (util.isArray(results[map[key]])) {
    data[key] = results[map[key]][0].value;
  } else {
    data[key] = results[map[key]];
  }
}
Or even more concise
Copy code
for (var key in map) {
  var result = results[map[key]];
  data[key] = util.isArray(result) ? result[0].value : result;
}
a
Good also but ternary does not look nicer? without calling any external anything?
e
The
N/util
module is always loaded anyway
a
ohh you can use util the same way you use log? there is more modules like those?
e
No just
util
and
log
(afaik)
a
Thanks...
e
well test it out first and make sure it works before you thank me 🙂
a
is working... not using util I think I will stay with the ternary and length version but saving your snippet too..
By the way, do you use anything specific to save snippets like in a central place or something?
e
Nope
Might be a good idea, but I haven't taken the time to set anything like that up
All my code is always in git, so I typically just search my repos