Select to view content in your preferred language

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

462
23
Jump to solution
2 weeks ago
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
23 Replies
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;

 

RichardCreek
New Contributor III

Produces this output: 

RichardCreek_0-1718911742947.png

Number of deaths should not be zero

0 Kudos
KenBuja
MVP Esteemed Contributor

I'm definitely not reading things closely enough! Just the injury field is Yes/No, so replace line 23 with

attrs['fatal'] = f.number_dead;
RichardCreek
New Contributor III

Ken, you have been an amazing help, I really appreciate it.

0 Kudos