Select to view content in your preferred language

Arcade Expression in Dashboard runs in testing, then Unable to execute Arcade script appears

727
23
Jump to solution
06-12-2024 11:29 AM
Labels (1)
RichardCreek
New Contributor III

Hello Community, 

I am trying to determine why this arcade expression runs during testing, but then when I click "done" and go back to the "Select a layer" page to select the expression to use in a bar chart the data expression text is grayed out with an alert saying "Unable to execute Arcade script". 

 

I've included my script below for reference, I would appreciate any assistance or explanation why this isn;t working as intended. 

 

// sets portal url
var cobportal = Portal('https://bloomingtonin.maps.arcgis.com/');

// assign Crash Dataset variables
var crashDatasetID = '50b2634c8c9443f58cd004f1138ef7ea';var crashLayerID = 0;

// assign FeatureSet variables
var crashFs = FeatureSetByPortalItem(cobportal, crashDatasetID, crashLayerID, ['number_dead', 'gis_incapacitating'], true);

// Initialize counters
var totalDeaths = 0;var totalInjuries = 0;

// Iterate through the FeatureSet and calculate totals
for (var crash in crashFs) {
  if (IsEmpty(crash.number_dead) == false && crash.number_dead > 0) {
    totalDeaths += crash.number_dead;
    }
    if (IsEmpty(crash.gis_incapacitating) == false && crash.gis_incapacitating == 'Yes') {
      totalInjuries += 1;
      }
      }

// Return the results as an array
return [totalDeaths, totalInjuries];
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

I couldn't figure out a way to do that in the GroupBy statement alone, so give this a try where it creates new count fields for the two fields. I simplified it a bit so it wasn't writing all the attributes to the new FeatureSet, so see how it works.

// sets portal url
var cobportal = Portal('https://bloomingtonin.maps.arcgis.com/');

// assign Crash Dataset variables
var crashDatasetID = '50b2634c8c9443f58cd004f1138ef7ea';var crashLayerID = 0;

// assign FeatureSet variables
var crashFs = FeatureSetByPortalItem(cobportal, crashDatasetID, crashLayerID, ['number_dead', 'gis_incapacitating', 'collision_date'], true);

var fieldList = [];
Push(fieldList, {"alias":"Collision Year","editable":true,"length":4,"name":"collision_year","nullable":true,"type":"esriFieldTypeInteger"})
Push(fieldList, {"alias":"Fatal Total","editable":true,"length":4,"name":"fatal","nullable":true,"type":"esriFieldTypeInteger"})
Push(fieldList, {"alias":"Injury Total","editable":true,"length":4,"name":"injured","nullable":true,"type":"esriFieldTypeInteger"})
var temp_dict = {
  fields: fieldList,
  geometryType: '',
  features: []
}

for (var f in crashFs) {
  var attrs = {}
  attrs['fatal'] = iif(f.number_dead == 'Yes', 1, 0);
  attrs['injured'] = iif(f.gis_incapacitating == 'Yes', 1, 0);
  attrs['collision_year'] = Year(f['collision_date'])
  
  Push(
    temp_dict['features'],
    {attributes: attrs}
  )
}

var newFS = FeatureSet(temp_dict);

var summaryFS = GroupBy(newFS, 'collision_year',
  [
    {name: 'Total Deaths', expression: 'fatal', statistic: 'SUM' }, 
    {name: 'Total Injuries', expression: 'injured', statistic: 'SUM' }  ]);

return summaryFS;

 

View solution in original post

23 Replies
marksm_macomb
Occasional Contributor

Dashboard data expressions require a featureset to be returned, but you are returning an array. You may want to create a featureset that has a category and a count field, and populate those fields with your totals. So I imagine you'll want to end up with a featureset "table" that looks like this in order to make a bar chart from it.

CategoryCount
Total Deaths###
Total Injuries###


Create a data expression—ArcGIS Dashboards | Documentation

0 Kudos
KenBuja
MVP Esteemed Contributor

A data expression must return a FeatureSet.

// sets portal url
var cobportal = Portal('https://bloomingtonin.maps.arcgis.com/');

// assign Crash Dataset variables
var crashDatasetID = '50b2634c8c9443f58cd004f1138ef7ea';var crashLayerID = 0;

// assign FeatureSet variables
var crashFs = FeatureSetByPortalItem(cobportal, crashDatasetID, crashLayerID, ['number_dead', 'gis_incapacitating'], true);

// Initialize counters
var totalDeaths = 0;var totalInjuries = 0;

// Iterate through the FeatureSet and calculate totals
for (var crash in crashFs) {
  if (IsEmpty(crash.number_dead) == false && crash.number_dead > 0) {
    totalDeaths += crash.number_dead;
    }
    if (IsEmpty(crash.gis_incapacitating) == false && crash.gis_incapacitating == 'Yes') {
      totalInjuries += 1;
      }
      }

// Return the results as a FeatureSet
var theDict = {
  'fields': [
    {'name':'TotalDeaths', 'type':'esriFieldTypeInteger'},
    {'name':'TotalInjuries', 'type':'esriFieldTypeInteger'}
  ],
  'geometryType': '',
  'features': [{
    'attributes': {
      'TotalDeaths': totalDeaths,
      'TotalInjuries': totalInjuries
     }
  }]
};
return FeatureSet(theDict);

 

 

RichardCreek
New Contributor III

I am pretty new to Arcade, how would you group the above output by year? I have a collision_date field where the year could be extracted. 

0 Kudos
KenBuja
MVP Esteemed Contributor

You can do that with the GroupBy function. Esri has a GitHub site with data expression samples, with one on GroupBy.

You can add the year field to your FeatureSet using a modification of @jcarlson's Memorize function, then use GroupBy on it. This should work...but it's untested

**Edit...fixing field names. and more fixes

 

function Memorize(fs) {
  var fieldList = Schema(fs)['fields'];
  Push(fieldList, {"alias":"Collision Year","editable":true,"length":4,"name":"collision_year","nullable":true,"type":"esriFieldTypeInteger"})
  var temp_dict = {
    fields: fieldList,
    geometryType: '',
    features: []
  }

  for (var f in fs) {
    var attrs = {}
    for (var attr in f) {
      attrs[attr] = f[attr]
    }
    attrs['collision_year'] = Year(f['collision_date'])
    Push(
      temp_dict['features'],
      {attributes: attrs}
    )
  }

  return FeatureSet(Text(temp_dict))
}
var cobportal = Portal('https://bloomingtonin.maps.arcgis.com/');

// assign Crash Dataset variables
var crashDatasetID = '50b2634c8c9443f58cd004f1138ef7ea';var crashLayerID = 0;

// assign FeatureSet variables

var crashFs = FeatureSetByPortalItem(cobportal, crashDatasetID, crashLayerID, ['number_dead', 'gis_incapacitating', 'collision_date'], true);

var newFS = Memorize(crashFs);
var summaryFS = GroupBy(newFS, 'collision_year',
  [
    {name: 'number_dead', expression: 'TotalDeaths', statistic: 'SUM' }, 
    {name: 'gis_incapacitating', expression: 'TotalInjuries', statistic: 'SUM' }
  ]); 

return summaryFS;

 

I was hoping to use an SQL expression to define the year from the collision-date field to avoid using the Memorize function, but I can never seem to get more complicated SQL expressions to work.

RichardCreek
New Contributor III

I'm a little confused about the order of this expression, is this appended to the original expression or run by itself? It did not run in Dashboards on its own. 

0 Kudos
KenBuja
MVP Esteemed Contributor

This is the whole expression. What do you get when you run in the code editor?

0 Kudos
RichardCreek
New Contributor III

Test execution error: Unexpected token ')'.. Verify test data.

0 Kudos
KenBuja
MVP Esteemed Contributor

I had to fix an error in line 13. There was an extra parenthesis at the end.

0 Kudos
RichardCreek
New Contributor III

Test execution error: Aggregation field not found. Verify test data.

0 Kudos