Dealing with potentially null arrays

Topic Labels: Automations
2476 5
Showing results for 
Search instead for 
Did you mean: 
6 - Interface Innovator
6 - Interface Innovator

What is the recommended way to deal with potentially null arrays when running a script as part of an automation?

I’m setting up an automation flow that runs a script and as part of the script I’m calling record.getCellValue() which oftentimes will be null. When using the scripting block, I’ve handled this by using the nullish operator, like this:

let array = record.getCellValue() ?? [];

That way I always have an array, even if empty, and not null.

When trying to re-create that script in an automation though, I get this error:
Screen Shot 2021-08-30 at 3.57.41 PM

I would’ve expected both scripting environments to have the same support. Am I missing something here? And if ?? isn’t supported, what’s the best way around it? I’m working with a lot of arrays so would like something succinct.

Thanks for any help

5 Replies 5

Automation scripts do not support everything that Scripting app supports, including the nullish operator (??) and option chaining (?.)

You can use a ternary operator.

let array = record.getCellValue() ? record.getCellValue() : [];
7 - App Architect
7 - App Architect

Just for the record, if you are sure your cell value will always be of type Array, using the logical OR operator (||) is equivalent to using the nullish coalescing operator (??😞

let array = record.getCellValue() || []

The pitfall here is JavaScript’s idea of falsiness. If your cell value can be a string, using || will result in a type conversion for empty strings (they become empty arrays, which, depending on your use case, may or may not be what you want), and numeric cell values will produce unexpected results. Which is exactly why the nullish coalescing operator was introduced in the first place.

Thanks @kuovonne. It’s pretty frustrating how the two environments are so different but thanks for the suggestion!

Thanks @Martin_Kopischke! I didn’t realise that was possible using the OR operator. I come from a background in Swift, so the wild world of javascript sometimes does my head in hah. Thanks for the help, will give that a go and see if it’s supported in the automation script

Ah, yes, unwrapping optionals is part and parcel in Swift. It’s an afterthought in JS, really.

The logical operators are part of the core JS functionality (see the browser version support table at the end of this article for an idea how long they have been supported); they are available :winking_face: .