TypeError: Cannot read property 'getValue' of unde...
# suitescript
r
TypeError: Cannot read property 'getValue' of undefined i know why is the error, hi know where is exists but still is there a validation to avoid this error?
c
Well, you have
something.getValue()
and that
something
is undefined, hence producing the error. What do you think you should be checking?
r
i have a search, which is empty i know that.. but i just want to avoid this error.. by some way is it possible????
c
Share your code
r
see what i am saying is
i know that search is null and it has no value ... but if it has no value ..i need to avoid this error
Untitled.txt
c
if ( /* whatever */)
r
i have two searches .. i have to get value from whatever search and set it
anyway it wont work
because it is still getting value from the search ... in the array
i have given null validation but the getValue will still show error because the search is null
c
so use intermediate variables instead of shoving it all on one horrible line
r
i was try to avoid that extra work
but okay
c
basic error checking and validation is not "extra work", it should be the minimum
r
no , but creating a whole new object is. when existing code is already up and running. i just thought there might me a way. its okay. there isn't.
s
You might want to consider refactoring that search to SuiteQL. I'd write that code very differently - using functions to better reflect the meaning of the code and I'd avoid the column indexing for sure.
I also generally do NOT create a bunch of intermediate objects or variables unless really needed to make code more clear. In general, every time you introduce extra objects or variables it makes code harder to read because each one is an additional mental connection the reader has to make when trying to understand the code.
👍 1
ironically, sometimes the seeming need to create intermediate variables is a sign of a confusing design. Whenever I find myself thinking I need to create intermediate variables I use it as a sign to step back and consider if I can instead simplify the whole thing.
in short, one thing you can do (if you're not going to refactor) is use the optional chaining operator. e.g.
searchResultTransactn[0]?.getValue(...)
c
Kinda hard to give decent advice when not all of the code was even shared
s
true that.
r
thanks @stalbert