Hello -
I'm trying to list averages from featureset variables and display them in a list. My script runs in the ArcCade or "Data Expression" playground but when I try to use it for an element on my dashboard it shows a message "unable to execute script". What am I doing wrong?
var p = 'https://xxxx.maps.arcgis.com/';
var itemId = '<layer id goes here>';
var fs = FeatureSetByPortalItem(Portal(p), itemId, 0, ['judgename','totalscore_stu1','totalscore_stu2','totalscore_stu3','totalscore_stu4','totalscore_stu5','totalscore_stu6'], false);
var scoreStu1 = Average(fs, 'totalscore_stu1');
var scoreStu2 = Average(fs, 'totalscore_stu2');
var scoreStu3 = Average(fs, 'totalscore_stu3');
var scoreStu4 = Average(fs, 'totalscore_stu4');
var scoreStu5 = Average(fs, 'totalscore_stu5');
var scoreStu6 = Average(fs, 'totalscore_stu6');
var result = [
{ "Name": "scoreStu1", "Score": scoreStu1 },
{ "Name": "scoreStu2", "Score": scoreStu2 },
{ "Name": "scoreStu3", "Score": scoreStu3 },
{ "Name": "scoreStu4", "Score": scoreStu4 },
{ "Name": "scoreStu5", "Score": scoreStu5 },
{ "Name": "scoreStu6", "Score": scoreStu6 }
];
return result;
thanks in advance for any guidance!
Solved! Go to Solution.
The playground will return anything provided it's a valid object. A data expression is specifically looking for a FeatureSet, which your result is not. You have an array of objects, but you need to built them into a FeatureSet as follows:
var p = 'https://xxxx.maps.arcgis.com/';
var itemId = '<layer id goes here>';
var fs = FeatureSetByPortalItem(Portal(p), itemId, 0, ['judgename','totalscore_stu1','totalscore_stu2','totalscore_stu3','totalscore_stu4','totalscore_stu5','totalscore_stu6'], false);
var scoreStu1 = Average(fs, 'totalscore_stu1');
var scoreStu2 = Average(fs, 'totalscore_stu2');
var scoreStu3 = Average(fs, 'totalscore_stu3');
var scoreStu4 = Average(fs, 'totalscore_stu4');
var scoreStu5 = Average(fs, 'totalscore_stu5');
var scoreStu6 = Average(fs, 'totalscore_stu6');
var result = [
{ attributes: { "Name": "scoreStu1", "Score": scoreStu1 }},
{ attributes: { "Name": "scoreStu2", "Score": scoreStu2 }},
{ attributes: { "Name": "scoreStu3", "Score": scoreStu3 }},
{ attributes: { "Name": "scoreStu4", "Score": scoreStu4 }},
{ attributes: { "Name": "scoreStu5", "Score": scoreStu5 }},
{ attributes: { "Name": "scoreStu6", "Score": scoreStu6 }}
];
var out_dict = {
fields: [
{ name: 'scoreStu1', type: 'esriFieldTypeDouble' },
{ name: 'scoreStu2', type: 'esriFieldTypeDouble' },
{ name: 'scoreStu3', type: 'esriFieldTypeDouble' },
{ name: 'scoreStu4', type: 'esriFieldTypeDouble' },
{ name: 'scoreStu5', type: 'esriFieldTypeDouble' },
{ name: 'scoreStu6', type: 'esriFieldTypeDouble' }
],
geometryType: '',
features: result
}
return FeatureSet(out_dict)
The playground will return anything provided it's a valid object. A data expression is specifically looking for a FeatureSet, which your result is not. You have an array of objects, but you need to built them into a FeatureSet as follows:
var p = 'https://xxxx.maps.arcgis.com/';
var itemId = '<layer id goes here>';
var fs = FeatureSetByPortalItem(Portal(p), itemId, 0, ['judgename','totalscore_stu1','totalscore_stu2','totalscore_stu3','totalscore_stu4','totalscore_stu5','totalscore_stu6'], false);
var scoreStu1 = Average(fs, 'totalscore_stu1');
var scoreStu2 = Average(fs, 'totalscore_stu2');
var scoreStu3 = Average(fs, 'totalscore_stu3');
var scoreStu4 = Average(fs, 'totalscore_stu4');
var scoreStu5 = Average(fs, 'totalscore_stu5');
var scoreStu6 = Average(fs, 'totalscore_stu6');
var result = [
{ attributes: { "Name": "scoreStu1", "Score": scoreStu1 }},
{ attributes: { "Name": "scoreStu2", "Score": scoreStu2 }},
{ attributes: { "Name": "scoreStu3", "Score": scoreStu3 }},
{ attributes: { "Name": "scoreStu4", "Score": scoreStu4 }},
{ attributes: { "Name": "scoreStu5", "Score": scoreStu5 }},
{ attributes: { "Name": "scoreStu6", "Score": scoreStu6 }}
];
var out_dict = {
fields: [
{ name: 'scoreStu1', type: 'esriFieldTypeDouble' },
{ name: 'scoreStu2', type: 'esriFieldTypeDouble' },
{ name: 'scoreStu3', type: 'esriFieldTypeDouble' },
{ name: 'scoreStu4', type: 'esriFieldTypeDouble' },
{ name: 'scoreStu5', type: 'esriFieldTypeDouble' },
{ name: 'scoreStu6', type: 'esriFieldTypeDouble' }
],
geometryType: '',
features: result
}
return FeatureSet(out_dict)
Thank you!! This got me almost exactly what I needed. Just had to rewrite the out_dict to this:
var out_dict = {
fields: [
{ name: 'Name', type: 'esriFieldTypeString' },
{ name: 'Score', type: 'esriFieldTypeDouble' }
],
geometryType: '',
features: result
}