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!
Solved! Go to Solution.
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:
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
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:
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