Help

Re: Scripting button & if statement error

Solved
Jump to Solution
2264 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Sarah_Viljoen
6 - Interface Innovator
6 - Interface Innovator

My code is really long so I'm not posting the whole thing, but here's the gist:

 

 

let choice = await input.buttonsAsync('Is the stock received from a pending order, transfer or unsure?', ['Pending Order Received', 'Transfer', 'Unsure']);

if (choice == 'Pending Order Received')
{
//code here with no errors
}

else if (choice == 'Transfer')
{
//code here with no errors
}

else if (choice == 'Unsure')
{
//code here
}

 

 

I'm getting this error with the last if statement for 'Unsure' and I don't know how to fix it:

Screenshot (11).png

1 Solution

Accepted Solutions
Grunty
7 - App Architect
7 - App Architect

I think you missed a { between each 'else' and 'if', and the corresponding }} to close them at the end.

Did I hit it?

See Solution in Thread

6 Replies 6

Weird, there's no error when I paste your code into a script extension

Screenshot 2023-02-13 at 6.37.22 PM.png

Grunty
7 - App Architect
7 - App Architect

I think you missed a { between each 'else' and 'if', and the corresponding }} to close them at the end.

Did I hit it?

Hey @Sarah_Viljoen

Huh... no idea what's causing intellisense to flag that.
Similar to @TheTimeSavingCo, I pasted in your script and intellisense didn't flag it for me. Nevertheless, just because intellisense flags a potential issue doesn't mean that your script will actually fail to run or throw an exception.
You can test it by simply tossing in a console.log() into your code block for the else...if condition in question and confirm that it actually runs as intended.

There are many things that you'll commonly do when scripting in Airtable that intellisense isn't a fan of.
In any other IDE, we'd be able to just flag that it should be ignored. Unfortunately, Airtable's built-in code editor doesn't allow us to do that.

Yes !! I have a lot of embedded if statements in this code and went over it 3 times before posting but I guess the fourth check was needed. Thank you 😊!

I feel like leaving a tiny tip for coders that, like you, at times get trapped in a spiderweb of if-else 🙂

Look back at your original code. Not visually bad, but logically imprecise, as the editor pointed out.

Now look at this beauty:

if (choice == 'Pending Order Received') {

//code here

} else {

if (choice == 'Transfer') {

//code here

} else {

if (choice == 'Unsure') {

//code here

}}}

The key to all this is the little cookie:  } else {

Most importantly, it helps you preventing the omition of a bracket. The error message when you do will be clear.

Added benefit is visual: It allows to realign all ‘if’s according to their logical value. There should not be any indentations among the three choices. All them have the same logical hierarchy, none subordinates to other.

So you may want to keep at hand this little cookie: } else {
and use it at every opportunity, to make your code neater and more compact.

@Grunty  Thanks for this  😊