Minor issue with arcade script that I typically don't run into.

363
8
Jump to solution
03-17-2025 06:11 AM
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Hi,

I typically don't have issues with arcade expressions but for some reason this one is returning the error message: "Unable to execute arcade script for Feature ObjectedID 0"

function GetPctColor(P){
    var Color = When(
        P >= 0 && P < 5,'#d01111',
        P >= 5 && P < 10,'#cc2211',
        P >= 10 && P < 15,'#c83311',
        P >= 15 && P < 20,'#c34211',
        P >= 20 && P < 25,'#bf5112',
        P >= 25 && P < 30,'#bb5f12',
        P >= 30 && P < 35,'#b76c12',
        P >= 35 && P < 40,'#b37912',
        P >= 40 && P < 45,'#af8512',
        P >= 45 && P < 50,'#ab9012',
        P >= 50 && P < 55,'#a79a12',
        P >= 55 && P < 60,'#a2a312',
        P >= 60 && P < 65,'#919f12',
        P >= 65 && P < 70,'#819b12',
        P >= 70 && P < 75,'#719712',
        P >= 75 && P < 80,'#639312',
        P >= 80 && P < 85,'#558f11',
        P >= 85 && P < 90,'#478b11',
        P >= 90 && P < 95,'#3b8711',
        '#2f8311'
        )
    return Color
    }
var fields = []
var Values = {}
for( var i=5; i<= 95; i=i+5 ){
    if( i%5 == 0 ){
        var f = {'name':'n'+Text(i), 'type':'esriFieldTypeSmallInteger'}
        Push( fields , f )
        Values[ 'n'+Text(i) ] = i
        f = {'name':'c'+Text(i), 'type':'esriFieldTypeString', Length:7}
        Push( fields , f )
        Values[ 'c'+Text(i) ] = GetPctColor(i)
        }
    }
Console( Values )
var Ramp = {
    'fields': fields,
    'geometryType':'',
    'features' : [{ attributes : Values }]
    }
Ramp = FeatureSet(Text(Ramp))
return Ramp

 

0 Kudos
1 Solution

Accepted Solutions
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

So the error that I came across is due to the featureset not likeing alphnumeric field names for integers or strings.

To bypass this; I created a list of alphabetical letters and then indexed those for the field names which seemed to bypass that issue.

 

function GetPctColor(P){
    var Color = When(
        P >= 0 && P < 5,'#d01111',
        P >= 5 && P < 10,'#cc2211',
        P >= 10 && P < 15,'#c83311',
        P >= 15 && P < 20,'#c34211',
        P >= 20 && P < 25,'#bf5112',
        P >= 25 && P < 30,'#bb5f12',
        P >= 30 && P < 35,'#b76c12',
        P >= 35 && P < 40,'#b37912',
        P >= 40 && P < 45,'#af8512',
        P >= 45 && P < 50,'#ab9012',
        P >= 50 && P < 55,'#a79a12',
        P >= 55 && P < 60,'#a2a312',
        P >= 60 && P < 65,'#919f12',
        P >= 65 && P < 70,'#819b12',
        P >= 70 && P < 75,'#719712',
        P >= 75 && P < 80,'#639312',
        P >= 80 && P < 85,'#558f11',
        P >= 85 && P < 90,'#478b11',
        P >= 90 && P < 95,'#3b8711',
        '#2f8311'
        )
    return Color
    }
var Alph = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V']
var fields = []
var V = {}
var counter = 0
for( var i=5; i<= 95; i=i+5 ){
    if( i%5 == 0 ){
        var l = Alph[ counter ]
        var f = {'name':l, 'type':'esriFieldTypeSmallInteger'}
        Push( fields , f )
        V[ l ] = i
        var n = {'name':'c'+Text(i), 'type':'esriFieldTypeString', Length:7}
        Push( fields , n )
        V[ 'c'+Text(i) ] = GetPctColor(i)
        counter++
        }
    }

var Ramp = {
    'fields': fields,
    'geometryType':'',
    'features' : [{ attributes : V }]
    }
Ramp = FeatureSet(Text(Ramp))
return Ramp

View solution in original post

8 Replies
KenBuja
MVP Esteemed Contributor

How are you using this script? Running it as is returns a FeatureSet

Snag_44c7d9.png

0 Kudos
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

So the error that I came across is due to the featureset not likeing alphnumeric field names for integers or strings.

To bypass this; I created a list of alphabetical letters and then indexed those for the field names which seemed to bypass that issue.

 

function GetPctColor(P){
    var Color = When(
        P >= 0 && P < 5,'#d01111',
        P >= 5 && P < 10,'#cc2211',
        P >= 10 && P < 15,'#c83311',
        P >= 15 && P < 20,'#c34211',
        P >= 20 && P < 25,'#bf5112',
        P >= 25 && P < 30,'#bb5f12',
        P >= 30 && P < 35,'#b76c12',
        P >= 35 && P < 40,'#b37912',
        P >= 40 && P < 45,'#af8512',
        P >= 45 && P < 50,'#ab9012',
        P >= 50 && P < 55,'#a79a12',
        P >= 55 && P < 60,'#a2a312',
        P >= 60 && P < 65,'#919f12',
        P >= 65 && P < 70,'#819b12',
        P >= 70 && P < 75,'#719712',
        P >= 75 && P < 80,'#639312',
        P >= 80 && P < 85,'#558f11',
        P >= 85 && P < 90,'#478b11',
        P >= 90 && P < 95,'#3b8711',
        '#2f8311'
        )
    return Color
    }
var Alph = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V']
var fields = []
var V = {}
var counter = 0
for( var i=5; i<= 95; i=i+5 ){
    if( i%5 == 0 ){
        var l = Alph[ counter ]
        var f = {'name':l, 'type':'esriFieldTypeSmallInteger'}
        Push( fields , f )
        V[ l ] = i
        var n = {'name':'c'+Text(i), 'type':'esriFieldTypeString', Length:7}
        Push( fields , n )
        V[ 'c'+Text(i) ] = GetPctColor(i)
        counter++
        }
    }

var Ramp = {
    'fields': fields,
    'geometryType':'',
    'features' : [{ attributes : V }]
    }
Ramp = FeatureSet(Text(Ramp))
return Ramp
KenBuja
MVP Esteemed Contributor

There are a couple ways to simplify this script.

  • You don't need to check if the values are greater than the previous values in the if statement (line 4-21)
  • You don't need the Text function anywhere, since concatenating a string and a number will result in a string (lines 33 and 35)
  • You also don't need the Text function to create a FeatureSet from a Dictionary (line 44). This was fixed as while ago, possibly in version 1.19
  • And you don't need the Alph array, since you can cast the variable i to a string also (lines 30 and 32).
function GetPctColor(P) {
  var Color = When(
    P >= 0 && P < 5, "#d01111",
    P < 10, "#cc2211",
    P < 15, "#c83311",
    P < 20, "#c34211",
    P < 25, "#bf5112",
    P < 30, "#bb5f12",
    P < 35, "#b76c12",
    P < 40, "#b37912",
    P < 45, "#af8512",
    P < 50, "#ab9012",
    P < 55, "#a79a12",
    P < 60, "#a2a312",
    P < 65, "#919f12",
    P < 70, "#819b12",
    P < 75, "#719712",
    P < 80, "#639312",
    P < 85, "#558f11",
    P < 90, "#478b11",
    P < 95, "#3b8711",
    "#2f8311"
  );
  return Color;
}
var fields = [];
var V = {};
for (var i = 5; i <= 95; i = i + 5) {
  if (i % 5 == 0) {
    var f = { name: `${i}`, type: "esriFieldTypeSmallInteger" };
    Push(fields, f);
    V[`${i}`] = i;
    f = { name: "c" + i, type: "esriFieldTypeString", Length: 7 };
    Push(fields, f);
    V["c" + i] = GetPctColor(i);
  }
}

var Ramp = {
  fields: fields,
  geometryType: "",
  features: [{ attributes: V }]
};
return FeatureSet(Ramp);

 

RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Thanks @KenBuja for showcasing a better way to write when statements. I am still working on improving the use of the functions and the concatenating text resulting in a text value I completely forgot about.

We are using a portal that is one version and several point releases behind the latest portal version, so somethings work and others don't. But next time we upgrade, I will be sure to update my code bases to better work using simpler code.

0 Kudos
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

It is ultimately, when combined with a modified html, to look like this.

RPGIS_0-1742221217822.png

 

VictorGutzler
Emerging Contributor
One inconsistency appears to be the type casting of the name in the 2nd line below. Shouldn't it also be a string type instead of an integer type?

if( i%5 == 0 ){

var f = {'name':'n'+Text(i), 'type':'esriFieldTypeSmallInteger'}

Push( fields , f )

Values[ 'n'+Text(i) ] = i

f = {'name':'c'+Text(i), 'type':'esriFieldTypeString', Length:7}

Push( fields , f )

Values[ 'c'+Text(i) ] = GetPctColor(i)

}

0 Kudos
KenBuja
MVP Esteemed Contributor

It can be a integer since the value that's being inserted into the field is an integer (line 4)

0 Kudos
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Hi @VictorGutzler ,

The second line populates an array of field names using the field name dictionary and the value populates a dictionary which is later added to an array as values.

0 Kudos