This has vexed me for quite some time. How do you...
# suitescript
m
This has vexed me for quite some time. How do you handle the results of a search.lookupFields for a 'select' field where there is no result? So to get the value when there is one present, I would do something like this:
let woLookup = search.lookupFields({
type: search.Type.WORK_ORDER,
id: wo_internalId,
columns: [
'bom.internalid',
'item',
'entity',
'custbody_ship_by_date_wo',
'createdfrom',
]
});
let salesOrd = woLookup.createdfrom[0].value;
When there is no 'createdfrom', I get an undefined error. Specifically: "Cannot read property 'value' of undefined". Can't seem to get around that no matter what I try. Any thoughts?
m
You just need to test the value before pulling. I am returning null but you can determine the default value you want. This should work.
Copy code
let salesOrd = woLookup.createdfrom.length > 0 ? woLookup.createdfrom[0].value : null;
m
Oh ok cool. I'll give that a try. Thanks!
n
Copy code
woLookup.hasOwnProperty("createdfrom")...
or maybe:
Copy code
woLookup.createdfrom[0].value||null;
d
Assuming you're using SS2.1 and ES6 you could also use optional chaining:
Copy code
let salesOrd = woLookup.createdfrom[0]?.value;
In case the createdfrom field is empty
salesOrd
will be undefined
💯 4
s
^ optional chaining would be my preferred