Arcade When expression returning no results

909
3
Jump to solution
07-15-2019 06:39 PM
DerekPhyn
Occasional Contributor

Hi All, I'm relatively new to Arcade but can't figure out this When function. I have a Right expression that returns the last three characters of a field $feature.info_headline when applied as a separate arcade expression. However when I apply the same expression as a variable in front of a When expression I get no errors when tested but also no results are returned? Can someone point me where I am going wrong with this expression? Is there a rule about variables I am missing?

var right3 = Right($feature.info_headline, 3);
var warning_level = When(right3=='Red', 'Red', right3=='nge', 'Orange', 'Watch');

I'm trying to get the expression to populate a new Color Style such that if the last three characters in the field $feature.info_headline are 'Red' the value returned is 'Red', or if he last three characters are 'nge' the value returned is 'Orange', else return 'Watch'.


I know there might be some options using nested IIf functions but thought the When function would be simple enough to do what I am trying.


Cheers

Derek

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi Derek Phyn ,

That is correct you should always return the result of the expression. 

For readability especially when you have a lot of values to check you could spread the "when parts" over multiple lines like this:

var right3 = Right($feature.info_headline, 3);
return When(right3=='Red', 'Warning - Red', 
            right3=='nge', 'Warning - Orange', 
            'Watch');

I also often use if - else if - else statements:

var right3 = Right($feature.info_headline, 3);
if (right3 == 'Red') {
    return 'Warning - Red';
} else if (right3 == 'nge') {
    return 'Warning - Orange';
} else {
    return 'Watch';
}

You can also use a dictionary with the options:

var right3 = Right($feature.info_headline, 3);
var dct = {'Red': 'Warning - Red',
           'nge': 'Warning - Orange'};
if (HasKey(dct, right3)) {
    return dct[right3];
} else {
    return 'Watch';
}

View solution in original post

3 Replies
DerekPhyn
Occasional Contributor

Stand down. Solutions found include:

When(Right($feature.info_headline, 3)=='Red', 'Warning - Red', Right($feature.info_headline, 3)=='nge', 'Warning - Orange','Watch')

or

var right3 = Right($feature.info_headline, 3);

return When(right3=='Red', 'Warning - Red', right3=='nge', 'Warning - Orange', 'Watch');

XanderBakker
Esri Esteemed Contributor

Hi Derek Phyn ,

That is correct you should always return the result of the expression. 

For readability especially when you have a lot of values to check you could spread the "when parts" over multiple lines like this:

var right3 = Right($feature.info_headline, 3);
return When(right3=='Red', 'Warning - Red', 
            right3=='nge', 'Warning - Orange', 
            'Watch');

I also often use if - else if - else statements:

var right3 = Right($feature.info_headline, 3);
if (right3 == 'Red') {
    return 'Warning - Red';
} else if (right3 == 'nge') {
    return 'Warning - Orange';
} else {
    return 'Watch';
}

You can also use a dictionary with the options:

var right3 = Right($feature.info_headline, 3);
var dct = {'Red': 'Warning - Red',
           'nge': 'Warning - Orange'};
if (HasKey(dct, right3)) {
    return dct[right3];
} else {
    return 'Watch';
}
XanderBakker
Esri Esteemed Contributor

Hi derek.phyn_WRC , since you answered your own question, can you mark your answer as the correct answer? Thanks, this will help other find the answer to similar questions more easily.

0 Kudos