Select to view content in your preferred language

Arcade Data Expression runs in playground but not in "live" dashboard

546
2
Jump to solution
06-21-2023 09:55 AM
JamiDennis
New Contributor III

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!

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

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)
- Josh Carlson
Kendall County GIS

View solution in original post

2 Replies
jcarlson
MVP Esteemed Contributor

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)
- Josh Carlson
Kendall County GIS
JamiDennis
New Contributor III

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
}
0 Kudos