Select to view content in your preferred language

Use arcade to read from 2 fields. Look at one field and return that value, if null/empty return value from other field

1874
3
Jump to solution
08-28-2023 05:29 AM
dwold
by
Frequent Contributor

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?

dwold_1-1693225651459.pngdwold_2-1693225678110.pngdwold_3-1693225738759.png

 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

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.

- Josh Carlson
Kendall County GIS

View solution in original post

3 Replies
jcarlson
MVP Esteemed Contributor

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.

- Josh Carlson
Kendall County GIS
dwold
by
Frequent Contributor

@jcarlson as always, thank you so much for your suggestion. That is exactly what I was looking for. After adding:

// If a OverrideColor has no value
// then Color is returned
DefaultValue($datapoint.OverridenColor, $datapoint.color)
 
in the advanced formatting section, I assume I need to call that in the return section and line item template (both attached)?
dwold_0-1693245499062.pngdwold_1-1693245510159.png

Line item:

<table style="width:100%">
<tbody>
<tr>
<td>
<p><span style="color:#00c5ff"><strong>{AssesmentName}</strong></span></p>
 
<p>Created on&nbsp;{expression/create}</p>
 
<p>Created by<span style="color:#ffff00">&nbsp;{Creator}</span></p>
 
<p>{expression/over}</p>
</td>
<td>
<p>{expression/icon}</p>
</td>
</tr>
</tbody>
</table>

 

0 Kudos
jcarlson
MVP Esteemed Contributor

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.

- Josh Carlson
Kendall County GIS