Select to view content in your preferred language

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

459
23
Jump to solution
2 weeks ago
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
23 Replies
KenBuja
MVP Esteemed Contributor

Since I don't have access to your data, I've been using another dataset to do my testing. This version returns the expected FeatureSet. Can you modify it to use your data?

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['date'])
    Push(
      temp_dict['features'],
      {attributes: attrs}
    )
  }

  return FeatureSet(Text(temp_dict))
}// Fetches features from a public portal item
var fs = FeatureSetByPortalItem(
  Portal("https://www.arcgis.com"),
  // portal item id
  "25e36eb0d93d49859f73a4c98b1d3484",
  0, // layer id
  ["*"], // fields to include
  false // include or exclude geometry
);

var newFS = Memorize(fs)
var summaryFS = GroupBy(newFS, 'collision_year',
  [
    {name: 'Maximum magnitude', expression: 'magnitude', statistic: 'Max' }
  ]); 

return summaryFS;

 

0 Kudos
RichardCreek
New Contributor III

Ken if you could provide some additional assistance I'd really appreciate it, still stuck. 

 

This is what I have now, but it's not correct: 

 

// 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);


function Memorize(fs) {
  var fieldList = Schema(fs)['collision_date'];
  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['date'])
    Push(
      temp_dict['features'],
      {attributes: attrs}
    )
  }

return FeatureSet(theDict);

var newFS = Memorize(fs)
var summaryFS = GroupBy(newFS, 'collision_year',
  [
    {name: 'Maximum magnitude', expression: 'magnitude', statistic: 'Max' }
  ]);

return summaryFS;
0 Kudos
KenBuja
MVP Esteemed Contributor

When posting code, use the "Insert/Edit code sample" button. It makes your code easier to read and provide line numbers in your code.

Remember that I was using a different data set to show you how it would work, since I couldn't test it with your data and you were getting an execution error.

Do you still get that error when using this?

var summaryFS = GroupBy(newFS, 'collision_year',
  [
    {name: 'number_dead', expression: 'TotalDeaths', statistic: 'SUM' }, 
    {name: 'gis_incapacitating', expression: 'TotalInjuries', statistic: 'SUM' }
  ]); 
0 Kudos
RichardCreek
New Contributor III

 

// 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 summaryFS = GroupBy(newFS, 'collision_date',
  [
    {name: 'number_dead', expression: 'TotalDeaths', statistic: 'SUM' }, 
    {name: 'gis_incapacitating', expression: 'TotalInjuries', statistic: 'SUM' }
  ]); 

 

 

Like this? it does not execute

0 Kudos
KenBuja
MVP Esteemed Contributor

Try this

**Updated GroupBy expression

 

// 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);


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['date'])
    Push(
      temp_dict['features'],
      {attributes: attrs}
    )
  }

return FeatureSet(theDict);

var newFS = Memorize(fs)
var summaryFS = GroupBy(newFS, 'collision_year',
  [
    {name: 'Total Deaths', expression: 'number_dead', statistic: 'SUM' }, 
    {name: 'Total Injuries', expression: 'gis_incapacitating', statistic: 'SUM' }  ]);

return summaryFS;

 

0 Kudos
RichardCreek
New Contributor III

Thank you so much for your continued assistance Ken, I really do appreciate it. Unfortunately, received this error: Test execution error: '}' expected.. Verify test data. It doesn't like somethin with line 40

0 Kudos
KenBuja
MVP Esteemed Contributor

A few fixes I missed

// 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);


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['date'])
    Push(
      temp_dict['features'],
      {attributes: attrs}
    )
  }

return FeatureSet(temp_dict);
}

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

return summaryFS;
0 Kudos
RichardCreek
New Contributor III

Thanks Ken for taking another look, received this error: Test execution error: Execution error - Key not found - date. Verify test data.

I don't see in the expression where collision_date (the date field) is referenced, maybe I'm missing something? 

0 Kudos
KenBuja
MVP Esteemed Contributor

Two more changes. Line 8 should be

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

and line 25 should

attrs['collision_year'] = Year(f['collision_date'])
0 Kudos
RichardCreek
New Contributor III

Really close Ken, output is not counting the injuries though, looks like this. 

RichardCreek_0-1718898744142.png

gis_incapacitating is a string field (Yes or No) and needs to be counted for 'Yes' records and then summed 

Current expression iteration for reference:

// 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);


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(temp_dict);
}

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

return summaryFS;
0 Kudos