Having syntax issues with 'When' function for recalculating ages

119
1
Jump to solution
08-14-2022 12:06 PM
Labels (2)
slach
by
New Contributor II

Hi there,

I have what I thought to be a simple arcade expression that I'm trying to use for a dashboard indicator. Currently I'm working with a table where ages are a string variable and some  records have 'NN" or 'BB' to refer to their stage in infancy. I'm looking to recalculate those records to be 0 and then to convert all the ages to integers so I can find the average.

I've been playing with this script all day and I think I'm running into syntax issues with recalling my feature service. Either that or I'm just not understanding the When function. I would really appreciate any suggestions or help!

 

var port = "https://--------------.arcgis.com"

var itemID = "---------------------------------"

var layerID = 3

var fields = ['AGE']

var geo = False

// Set up the FeatureSet with which to call:
var FS = FeatureSetByPortalItem(Portal(port), itemID, layerID, fields, geo)

// return the set of features to use within indicator:

When($FS.AGE ='BB', '0', $FS.AGE = 'NN', '0', defaultValue);

var newagesdictionary = {
    'fields': [
        { 'name': 'age_recalc', 'type': 'esriFieldTypeInteger'}
    ],
    'geometryType':'',
    'features': 
    [{'attributes':
    {'newage': Number($FS.AGE)}
    }]}

var recalcagedict = FeatureSet(Text(newagesdictionary));

return recalcagedict

 

I keep getting 'Invalid Parameter as an error message but I'm not sure where it's really referring to.

 

Thank you so much!

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Regular Contributor

You're misunderstanding the When function.

When() doesn't work on feature sets. It takes multiple booleans and objects and returns the object following the first true boolean.

var x = 1
// this will return 25
return When(x == 0, "a", x == 1, 25, x == 2, "z", "default")

 

Other things:

  • the dollar sign denotes global variables like $feature, $map, or $layer. You can't just put it in front of custom variables.
  • In most programming languages, "=" is assignment ("var x = 3"). To test for equal, use "==" ("x == 3" -> true)
  • FS is a FeatureSet, a collection of Seatures. You can't call a field on a FeatureSet, only on the separate Features.
  • in your dictionary, you have a different name for the field in the fields declaration and the feature attributes.

 

To solve your problem, create an empty dictionary, then loop through the FeatureSet and Push the corrected values into the feature array:

var port = "https://--------------.arcgis.com"

var itemID = "---------------------------------"

var layerID = 3

var fields = ['AGE']

var geo = False

// Set up the FeatureSet with which to call:
var FS = FeatureSetByPortalItem(Portal(port), itemID, layerID, fields, geo)

// return the set of features to use within indicator:

var newagesdictionary = {
    'fields': [
        { 'name': 'age_recalc', 'type': 'esriFieldTypeInteger'}
    ],
    'geometryType':'',
    'features':[]
}

for(var f in FS) {
    var new_age = When(f.AGE == 'BB', '0', f.AGE == 'NN', '0', f.AGE)
    var new_feature = {'attributes': {'age_recalc': Number(new_age)}}
    Push(newagesdictionary.features, new_feature)
}

var recalcagedict = FeatureSet(Text(newagesdictionary));

return recalcagedict

 


Have a great day!
Johannes

View solution in original post

0 Kudos
1 Reply
JohannesLindner
MVP Regular Contributor

You're misunderstanding the When function.

When() doesn't work on feature sets. It takes multiple booleans and objects and returns the object following the first true boolean.

var x = 1
// this will return 25
return When(x == 0, "a", x == 1, 25, x == 2, "z", "default")

 

Other things:

  • the dollar sign denotes global variables like $feature, $map, or $layer. You can't just put it in front of custom variables.
  • In most programming languages, "=" is assignment ("var x = 3"). To test for equal, use "==" ("x == 3" -> true)
  • FS is a FeatureSet, a collection of Seatures. You can't call a field on a FeatureSet, only on the separate Features.
  • in your dictionary, you have a different name for the field in the fields declaration and the feature attributes.

 

To solve your problem, create an empty dictionary, then loop through the FeatureSet and Push the corrected values into the feature array:

var port = "https://--------------.arcgis.com"

var itemID = "---------------------------------"

var layerID = 3

var fields = ['AGE']

var geo = False

// Set up the FeatureSet with which to call:
var FS = FeatureSetByPortalItem(Portal(port), itemID, layerID, fields, geo)

// return the set of features to use within indicator:

var newagesdictionary = {
    'fields': [
        { 'name': 'age_recalc', 'type': 'esriFieldTypeInteger'}
    ],
    'geometryType':'',
    'features':[]
}

for(var f in FS) {
    var new_age = When(f.AGE == 'BB', '0', f.AGE == 'NN', '0', f.AGE)
    var new_feature = {'attributes': {'age_recalc': Number(new_age)}}
    Push(newagesdictionary.features, new_feature)
}

var recalcagedict = FeatureSet(Text(newagesdictionary));

return recalcagedict

 


Have a great day!
Johannes
0 Kudos