if in calculated attribute

Deborah 3 years ago

Hello,

5.6 I saw that the old IF syntax in calculated attributes is no longer supported.

For example I had this attribute:

If(io87)
  {
  }
else
  {
     return io87
  }

How can this be done now? From the JEXL syntax it still seems to be allowed (https://commons.apache.org/proper/commons-jexl/reference/syntax.html)

I also have some nested ifs that I wouldn't know how to convert

Thanks!

Anton Tananaev 3 years ago

You can use the ternary operator:

io87 ? first : second
Deborah 3 years ago

Thank you, it works. How come this expression truncates my decimals?

io83 ?? io83/10.0F

Instead by leaving only io83/10.0F the final result correctly has one decimal place.
The output is always of type number

Anton Tananaev 3 years ago

Are you sure the result is truncated? Or the UI just displays one decimal place? Check the API or the database.

Deborah 3 years ago

These are the two tests done directly from the attributes page

https://ibb.co/HgSV0yf
https://ibb.co/8Dfc8VZ

Anton Tananaev 3 years ago

Oh I see. Your expression doesn't really make sense. It always uses the original value.

Deborah 3 years ago

I would basically need the attribute to be calculated only if io83 exists. Are there any other solutions?

Anton Tananaev 3 years ago

In that case your expression is completely incorrect. The right solution would be something like this:

io83 ? io83/10.0F : null
Deborah 3 years ago

thanks it work