Hello - I would like to write an expression that will look at a field and return the values in a list. If that field is empty, look at another field and return those values in the field. Very green with arcade but I think this would be an if/else statement?
In my example, color will always have a value, overridden color will be empty 90% of the time. Would it be best to return color field first and if overridden has value return that value? Or overridden first then color?
Solved! Go to Solution.
The simplest would be to use DefaultValue. Written
DefaultValue(fieldA, fieldB)
this will return the value of A if one exists, and fall back to B if not.
You certainly could do this with if/else as well, which would look like:
if (!IsEmpty(fieldA)) {
return fieldA
} else {
return fieldB
}
However! When you only have a single condition and two outcomes, Iif is great for that, too.
Iif(!IsEmpty(fieldA), fieldA, fieldB)
But since all you're checking is whether a value exists and returning it if it does, that's exactly what DefaultValue is for, so it's a bit more concise.
The simplest would be to use DefaultValue. Written
DefaultValue(fieldA, fieldB)
this will return the value of A if one exists, and fall back to B if not.
You certainly could do this with if/else as well, which would look like:
if (!IsEmpty(fieldA)) {
return fieldA
} else {
return fieldB
}
However! When you only have a single condition and two outcomes, Iif is great for that, too.
Iif(!IsEmpty(fieldA), fieldA, fieldB)
But since all you're checking is whether a value exists and returning it if it does, that's exactly what DefaultValue is for, so it's a bit more concise.
@jcarlson as always, thank you so much for your suggestion. That is exactly what I was looking for. After adding:
Line item:
You'll need to assign the output of the DefaultValue statement to a variable, then use that in the appropriate location.
var rendered_color = DefaultValue($datapoint.OverridenColor, $datapoint.color)
return {
...,
attributes: {
...,
rendered_color
}
}
Then call {expression/rendered_color} in your line item template wherever you want the color / override to be applied.