Nested If or When Statement in Arcade Needs Boolean Test Condition

1534
3
Jump to solution
03-04-2022 03:32 PM
SummerKing
New Contributor

Testing this in the Arcade Playground, the error that occurs happens whether I use an IF or a WHEN statement.

The goal is to round up if the number is >= x.75, round down is the number is < x.75 and not to round at all if the number is x.00.  

Code:

//Some variables instead of referencing geodatabase fields which Playground doesn't understand.

var Life80=5.5
var LSY=2020


//Determine if date is before or after FY start of September 1
Var RoundDown = Floor(Life80,0);

//Additions to simplify formula
Var StraightAdd = Round(LSY+Life80,0);
Var AddRound = LSY+RoundDown;

//Constrain to current year minimum and ridiculously later year maximum
Iif((Life80=RoundDown), StraightAdd, (Iif((Life80-0.75)<RoundDown, Constrain(AddRound,2022,9999), Constrain(StraightAdd, 2022,9999))));

//Alternate expression: When(Life80=RoundDown, StraightAdd, (Life80-0.75)<RoundDown, Constrain(AddRound,2022,9999), (Life80-0.75)>RoundDown, Constrain(StraightAdd, 2022,9999),9999);

 

Error: 

Execution Error:IF Function must have a boolean test condition

 

Question: Is there something off about my syntax?  Have been looking through the ESRI community and have seen multiple examples of IF and WHEN statements and the syntax is just as above as far as I can tell.  I feel as if I'm missing something really obvious, but I don't see it.  Any insight would be appreciated!

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

The Iif function certainly has its place, but nesting more than one of them makes for hard-to-read code. Consider just breaking it out into separate if/else if/else blocks.

PS - Don't forget about the "insert/edit code sample" button here!

jcarlson_0-1646511965896.png

Anyway, testing your same expression in the Playground. The first thing I notice is this:

Life80=RoundDown

In Arcade, this would set the Life80 variable to the value in the RoundDown variable. To check equivalence, you need to use "==". In fact, making that single change gets your expression to evaluate!

jcarlson_1-1646512142199.png

But here's that long nested iif, broken up:

if (Life80 == RoundDown){
    return StraightAdd
} else if (Life80-0.75 < RoundDown){
    return Constrain(AddRound,2022,9999)
} else {
    return Constrain(StraightAdd, 2022,9999)
}

As you allude to in your post, though, you could certainly do this with When. In your particular situation, it makes sense to use that, as it will be more concise. A full if/else if/else block is useful when the different conditions merely cause your expression to branch off, but since we're just determining the final returned value, it's a bit much. Here's the when version:

return When(
    Life80 == RoundDown, StraightAdd,
    Life80 - 0.75 < RoundDown, Constrain(AddRound, 2022, 9999),
    Constrain(StraightAdd, 2022, 9999)
)
- Josh Carlson
Kendall County GIS

View solution in original post

3 Replies
JayantaPoddar
MVP Esteemed Contributor

Try a similar expression.

var ar=$feature.Area_Feet
IIF(ar-Floor(ar,0)>=0.75,Ceil(ar,0),Floor(ar,0))

 

JayantaPoddar_0-1646448599763.png

 



Think Location
0 Kudos
jcarlson
MVP Esteemed Contributor

The Iif function certainly has its place, but nesting more than one of them makes for hard-to-read code. Consider just breaking it out into separate if/else if/else blocks.

PS - Don't forget about the "insert/edit code sample" button here!

jcarlson_0-1646511965896.png

Anyway, testing your same expression in the Playground. The first thing I notice is this:

Life80=RoundDown

In Arcade, this would set the Life80 variable to the value in the RoundDown variable. To check equivalence, you need to use "==". In fact, making that single change gets your expression to evaluate!

jcarlson_1-1646512142199.png

But here's that long nested iif, broken up:

if (Life80 == RoundDown){
    return StraightAdd
} else if (Life80-0.75 < RoundDown){
    return Constrain(AddRound,2022,9999)
} else {
    return Constrain(StraightAdd, 2022,9999)
}

As you allude to in your post, though, you could certainly do this with When. In your particular situation, it makes sense to use that, as it will be more concise. A full if/else if/else block is useful when the different conditions merely cause your expression to branch off, but since we're just determining the final returned value, it's a bit much. Here's the when version:

return When(
    Life80 == RoundDown, StraightAdd,
    Life80 - 0.75 < RoundDown, Constrain(AddRound, 2022, 9999),
    Constrain(StraightAdd, 2022, 9999)
)
- Josh Carlson
Kendall County GIS
SummerKing
New Contributor

Aha!  It's the "==" vs "=" that was tripping me up!  Thank you for the great explanation and the streamlining of the code.  It works perfectly now!

0 Kudos