Arcade Get Field Value Using Variable

3204
3
Jump to solution
11-18-2022 11:23 AM
VinceE
by
Frequent Contributor

Need some help understanding Arcade's syntax. The top two lines throw an error, as shown. The bottom one works fine. I'd prefer to not hard-code field names, so using the bracket syntax with a variable would be preferable to the dot notation ($feature.COLOR). What am I missing here?

VinceE_0-1668790229249.png

VinceE_1-1668790291933.png

 

 

 

0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Notable Contributor

 

 

Expects($feature, 'COLOR')
var fieldName = 'COLOR'

$feature[fieldName]

 

 

As seen here.

R_

View solution in original post

3 Replies
RhettZufelt
MVP Notable Contributor

 

 

Expects($feature, 'COLOR')
var fieldName = 'COLOR'

$feature[fieldName]

 

 

As seen here.

R_

VinceE
by
Frequent Contributor

Very helpful, thanks. Seems like I was missing the part where it is necessary to declare the attribute variables from the table that are to be used in the script...?

 

Expects($feature, 'COLOR')
//$feature.COLOR;

var fieldName = 'COLOR'
return $feature[fieldName]

 

The documentation, found at the link you provided:

You should explicitly list all field attributes that will be used at the top of the expression. This will ensure the profile will request the appropriate data from required fields so the expression evaluates properly.

$feature.COLLEGE;$feature.POPULATION;
Round(($feature.COLLEGE / $feature.POPULATION) * 100, 2);
Alternatively, you may use the Expects function for this purpose.
Expects($feature, "COLLEGE", "POPULATION");
Round(($feature.COLLEGE / $feature.POPULATION) * 100, 2);
0 Kudos
RhettZufelt
MVP Notable Contributor

Sorry, did not post correctly, but I updated it.

Sounds like you got the important part about listing the fields that will be used and got it working.

R_