How would one filter or go around 0 results, for a...
# suitescript
s
How would one filter or go around 0 results, for accesing values via lookup? Getting record.field[0].value
TypeError: Cannot read property "0" from undefined
When results is {} I tried checking it against below
val == "" || val == null || val == undefined || val.length === 0
But it still goes through the if condition
c
What do you mean by "via lookup"?
a
share more code
record.field[0].value could really be anything... if we can't see how you're making that object we can't understand where the undefined piece is coming from so we can't help you work around it
the issue seems to be with field though... not value
also you're using val and value... is that just a typo?
s
Oh, I wanted to get the item type from a lookup in my UE script. Then use that as a condition before doing some logic
Copy code
var itemTypeVal = search.lookupFields({
                            type: 'item',
                            id: itemLine,
                            columns: ['type']
                        })
                        log.debug('itemLog', itemTypeVal);

                        if (IsNotEmpty(itemTypeVal)) {
                            var itemType = itemTypeVal.type[0].value
                             log.debug('itemType', itemType);
                        }

                        if (itemType == 'InvPart') {

                          // update some fields

                        }
On good results it’s working
{
"type": [
{
"value": "InvtPart",
"text": "Inventory Item"
}
]
}
On bat results, i’m getting just
{}
So my
if (IsNotEmpty(itemTypeVal))
is still being executed
c
All the tests you're performing will return false against an empty object.
You can either add an object-specific test, or to be more specific, you can test for the existence of the expected key in the object
a
yeah so you're checking
type
... so just check for a
type
key?
if (itemTypeVal.type)
{}
will not pass that condition
c
What Anthony says will work in this case, or a more general approach of
if ("type" in itemTypeVal)
. I personally prefer the second when looking for a key in case a property exists on an object but is falsey.
s
dang it, that worked facepalm i even tried using
itemTypeVal.type[0].value
as condition thanks a lot!! what’s weird is that why other items are throwing blank object even if they have values on their record. the ones having the blank are the non-inventory ones, discount etc
c
@Schwifty that's odd. I don't see the same behavior in my environment. You're positive the internal IDs are correct?
I get a blank object when I search using an internal ID that doesn't exist as an item key.
s
yeah… the code before my lookup are just getting sublist values, specifically im having the issue with internalid -2 which is Subtotal
c
I don't think the system records, which are usually indicated with a negative internal ID, are going to behave the same as instances added to the environment.
s
Yeah.. that makes sense. Anyhow, the condition filters it properly now and logic works. Appreciate the time!
🙌 1
a
at a higher level... you probably just want to source the item type on to its own custcol field on your transactions... the lookups are way less performant and if this is on something like a sales order... you will suffer with performance over time the more of these things just keep getting added on
🙌 1
1
n
Copy code
itemTypeVal.hasOwnProperty('type')
Is another option. (until people shoot me down and say why that's a bad idea... 🤭)
🙌 2
n
If you are using SS 2.1, then you can use record.field[0]?.value Edit: search.lookupFields always return the field in result even if it is empty. Your error suggest that you are not getting any results. In that case, you can use record.field? record.field[0]?.value : null;
1
🙌 1