Arcade Expression to Change Multiple Values in a Field for Each Record

2195
6
Jump to solution
07-28-2020 01:48 PM
StaceyPlumley
New Contributor II

Hello - I'm trying to change multiple values in a field for each record (e.g., SK, CO, BT) to different values (e.g. Sockeye, Coho, Bull Trout) to display in a web map pop-up using Arcade. I've tried this Arcade expression but it returns the error: Execution Error:Field not Found:

var spcode = Dictionary('SK', 'Sockeye', 'CH', 'Chum', 'PK', 'Pink', 'CO', 'Coho', 'CK', 'Chinook', 'SH', 'Steelhead', 'SRCT', 'Sea run cutthroat', 'RT', 'Resident trout', 'BT', 'Bull Trout');

var values = Split($feature.Species, ',');

var numcode = Count(values)

var spindex = 0;

var splist = [];

if (numcode > 0) {

    for (var i in values) {

        splist[spindex] = spcode[values];

        ++spindex;

    }

return splist;

If the array is a variable, this expression works in the Arcade Playground:

var spcode = Dictionary('SK', 'Sockeye', 'CH', 'Chum', 'PK', 'Pink', 'CO', 'Coho', 'CK', 'Chinook', 'SH', 'Steelhead', 'SRCT', 'Sea run cutthroat', 'RT', 'Resident trout', 'BT', 'Bull Trout');
var values = ['CH', 'BT', 'SH'];
var spindex = 0;
var splist = [];
for (var i in values) {
splist[spindex] = spcode[values];
++spindex;
}
return splist;

I've also tried a function to create an array of the field values, similar to the second expression, but get the same error as the first expression:

function fieldarr(inField) {

    var values = Split(inField, ',');

    var num = Count(values)

    var cdindex = 0;

    var cdlist = [];

    if (num > 0) {

        for (var i in values) {

            cdlist[cdindex] = values;

            ++cdindex;

        }

    } 

return cdlist;

}

var spcode = Dictionary('SK', 'Sockeye', 'CH', 'Chum', 'PK', 'Pink', 'CO', 'Coho', 'CK', 'Chinook', 'SH', 'Steelhead', 'SRCT', 'Sea run cutthroat', 'RT', 'Resident trout', 'BT', 'Bull Trout');

var fieldlist = fieldarr($feature.Species)

var numcode = Count(fieldlist)

var spindex = 0;

var splist = [];

if (numcode > 0) {

    for (var i in fieldlist) {

        splist[spindex] = spcode[fieldlist];

        ++spindex;

    }

return splist;

I'm stumped - any help would be greatly appreciated!

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

This code in the playground worked. I used the Popup profile and changed the $feature.FEATURECODE values to "SK, PK, CK".

Notice on line 2 that Split uses ', ' since you have to get rid of that space after the comma. Otherwise, you'll have an array ['SK',' PK',' CK']. The code was failing at the array value with that leading space.

var spcode = Dictionary('SK', 'Sockeye', 'CH', 'Chum', 'PK', 'Pink', 'CO', 'Coho', 'CK', 'Chinook', 'SH', 'Steelhead', 'SRCT', 'Sea run cutthroat', 'RT', 'Resident trout', 'BT', 'Bull Trout');
var values = Split($feature.FEATURECODE, ', ');
var numcode = Count(values)
var spindex = 0;
var splist = [];
if (numcode > 0) {
    for (var i in values) {
        //console(spcode[values]);
        splist[spindex] = spcode[values[i]];
        spindex++;
    }
} 

return splist;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

6 Replies
Egge-Jan_Pollé
MVP Regular Contributor

Hi Stacey Plumley,

I am not completely sure whether I do understand exactly what you want to accomplish, but I think you can do it in one line of code...

Situation: in your feature class there is an attribute column called Species containing values like 'CK', 'SH' and 'SRCT' which should be replaced with the values 'Chinook', 'Steelhead' and 'Sea run cutthroat' respectively.

Did you have a look at the Decode() function on this page Logical Functions | ArcGIS for Developers ?

I think this should do the trick:

Decode($feature.Species, 'SK', 'Sockeye', 'CH', 'Chum', 'PK', 'Pink', 'CO',
'Coho', 'CK', 'Chinook', 'SH', 'Steelhead', 'SRCT', 'Sea run cutthroat',
'RT', 'Resident trout', 'BT', 'Bull Trout', 'Other');‍‍‍‍‍

This will replace all the abbreviations with the full words. I have added 'Other' at the end (as the default), just in case there are any abbreviations in the column not listed here.

Please be gentle and test it on a newly added attribute expression before applying it to the source column 🙂

What do you think?

BR,

Egge-Jan

0 Kudos
StaceyPlumley
New Contributor II

Hi Egge-Jan,

I tried Decode and it only returns the default value. Here's the full expression:

And the resulting pop-up:

I also tried this expression and it also only returned the default value:

I've also tried Replace but only the first value got replaced.

Thanks for the suggestion!

Stacey

0 Kudos
KenBuja
MVP Esteemed Contributor

This code in the playground worked. I used the Popup profile and changed the $feature.FEATURECODE values to "SK, PK, CK".

Notice on line 2 that Split uses ', ' since you have to get rid of that space after the comma. Otherwise, you'll have an array ['SK',' PK',' CK']. The code was failing at the array value with that leading space.

var spcode = Dictionary('SK', 'Sockeye', 'CH', 'Chum', 'PK', 'Pink', 'CO', 'Coho', 'CK', 'Chinook', 'SH', 'Steelhead', 'SRCT', 'Sea run cutthroat', 'RT', 'Resident trout', 'BT', 'Bull Trout');
var values = Split($feature.FEATURECODE, ', ');
var numcode = Count(values)
var spindex = 0;
var splist = [];
if (numcode > 0) {
    for (var i in values) {
        //console(spcode[values]);
        splist[spindex] = spcode[values[i]];
        spindex++;
    }
} 

return splist;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
StaceyPlumley
New Contributor II

That did the trick - thanks Ken!

0 Kudos
KenBuja
MVP Esteemed Contributor

Glad to help. Don't forget to click the "Mark Correct" button.

Also, if you want the list to be a little easier to read, you can use the Concatenate function to add a space

return Concatenate(splist,', ');

StaceyPlumley
New Contributor II

Very nice! Thanks again for your help!

0 Kudos