Hello!
I'm quite new to using data expressions with dashboards. My data contains water sample results and i wanted to assign a value for each period/date ranges. The data was filtered by different date ranges and I assigned them a value and wrote them in a new table.
It worked just fine. However, when I tried to return a FeatureSet from the Dictionary, it's not returning any values even if the dictionary has features. Not sure what I'm missing...
Here's the code I wrote for reference:
var periods = [p1, p2, p3, p4, p5];
var features = [];
var feat;var cnt = 0;
for (var ped in periods) {
var period = periods[cnt];
for (var p in period) {
feat = {
attributes: {
'objectid': p['objectid'],
'assetid': p['assetid'],
'wzonename': p['wzonename'],
'reservoir_id': p['reservoir_id'],
'sp_type': p['sp_type'],
'sample_date': p['sample_date'],
'sample_year': p['sample_year'],
'test_description': p['test_description'],
'result': p['result'],
'izparea': p['izparea'],
'period': 'p'+(cnt+1)
}
}
Push(features,feat)
}
cnt++;
}
//return features;
var samples = {
'fields':
[{'name': 'objectid', 'type': 'esriFieldTypeInteger'},
{'name': 'assetid', 'type': 'esriFieldTypeString'},
{'name': 'wzonename', 'type': 'esriFieldTypeString'},
{'name': 'reservoir_id', 'type': 'esriFieldTypeString'},
{'name': 'sp_type', 'type': 'esriFieldTypeString'},
{'name': 'sample_date', 'type': 'esriFieldTypeDate'},
{'name': 'sample_year', 'type': 'esriFieldTypeString'},
{'name': 'test_description', 'type': 'esriFieldTypeString'},
{'name': 'result', 'type': 'esriFieldTypeDouble'},
{'name': 'izparea', 'type': 'esriFieldTypeString'},
{'name': 'period', 'type': 'esriFieldTypeString'}
],
'geometryType': 'esriGeometryPoint',
'features': features
};
//return samples;
return FeatureSet(Text(samples));
Solved! Go to Solution.
To post formatted code:
You have a date field in your feature set. For whatever stupid design reason, you can't input Date() values into date fields, you have to convert them to Number() first. I took the opportunity to make your for loop shorter.
var periods = [p1, p2, p3, p4, p5];
var features = [];
for (var cnt in periods) {
var period = periods[cnt];
for (var p in period) {
// get the original attributes
var copy_attributes = Dictionary(Text(p))['attributes']
// cast the date to number
copy_attributes['sample_date'] = Number(copy_attributes['sample_date'])
// calculate the period
copy_attributes['period'] = 'p' + (cnt + 1)
// push into array
Push(features, {'attributes': copy_attributes})
}
}
//return features;
var samples = {
'fields': [
{'name': 'objectid', 'type': 'esriFieldTypeInteger'},
{'name': 'assetid', 'type': 'esriFieldTypeString'},
{'name': 'wzonename', 'type': 'esriFieldTypeString'},
{'name': 'reservoir_id', 'type': 'esriFieldTypeString'},
{'name': 'sp_type', 'type': 'esriFieldTypeString'},
{'name': 'sample_date', 'type': 'esriFieldTypeDate'},
{'name': 'sample_year', 'type': 'esriFieldTypeString'},
{'name': 'test_description', 'type': 'esriFieldTypeString'},
{'name': 'result', 'type': 'esriFieldTypeDouble'},
{'name': 'izparea', 'type': 'esriFieldTypeString'},
{'name': 'period', 'type': 'esriFieldTypeString'}
],
'geometryType': 'esriGeometryPoint',
'features': features
};
//return samples;
return FeatureSet(Text(samples));
Hi Johannes,
That works like a charm - You're heaven sent!
Yeah, I've seen some other thread with the other users having issues writing Dates too, but didn't expect that was the same issue as mine.
Thanks heaps!! 🙂
Have a great day!
To post formatted code:
You have a date field in your feature set. For whatever stupid design reason, you can't input Date() values into date fields, you have to convert them to Number() first. I took the opportunity to make your for loop shorter.
var periods = [p1, p2, p3, p4, p5];
var features = [];
for (var cnt in periods) {
var period = periods[cnt];
for (var p in period) {
// get the original attributes
var copy_attributes = Dictionary(Text(p))['attributes']
// cast the date to number
copy_attributes['sample_date'] = Number(copy_attributes['sample_date'])
// calculate the period
copy_attributes['period'] = 'p' + (cnt + 1)
// push into array
Push(features, {'attributes': copy_attributes})
}
}
//return features;
var samples = {
'fields': [
{'name': 'objectid', 'type': 'esriFieldTypeInteger'},
{'name': 'assetid', 'type': 'esriFieldTypeString'},
{'name': 'wzonename', 'type': 'esriFieldTypeString'},
{'name': 'reservoir_id', 'type': 'esriFieldTypeString'},
{'name': 'sp_type', 'type': 'esriFieldTypeString'},
{'name': 'sample_date', 'type': 'esriFieldTypeDate'},
{'name': 'sample_year', 'type': 'esriFieldTypeString'},
{'name': 'test_description', 'type': 'esriFieldTypeString'},
{'name': 'result', 'type': 'esriFieldTypeDouble'},
{'name': 'izparea', 'type': 'esriFieldTypeString'},
{'name': 'period', 'type': 'esriFieldTypeString'}
],
'geometryType': 'esriGeometryPoint',
'features': features
};
//return samples;
return FeatureSet(Text(samples));
Hi Johannes,
That works like a charm - You're heaven sent!
Yeah, I've seen some other thread with the other users having issues writing Dates too, but didn't expect that was the same issue as mine.
Thanks heaps!! 🙂
Have a great day!
Glad to help.
Please accept the answer as solution, so that this question gets shown as resolved.
Just posted an Idea about this, feel free to add your support:
Arcade: Allow Date() values in date fields (esriFi... - Esri Community
With this week's update to Arcade and ArcGIS Dashboards, date fields in feature set constructors now just work. You no longer have to wrap dates with Number() if you pass the dictionary into the FeatureSet() function (which as of this release accepts a dictionary as opposed to only text based JSON).
Instead of:
return FeatureSet(Text(dict))
Do this:
return FeatureSet(dict)
Learn more in this blog post.
NOTE: For Enterprise users, this update is targeted for 11.2