Select to view content in your preferred language

Arcade Script Error

270
8
Jump to solution
3 weeks ago
RomanFox
New Contributor III

Hello,

 

I am trying to set an Attribute Rule in ArcPro.  I am receiving "Invalid expression. Error on line 1. Field not found Cleanliness_Rating.

RomanFox_0-1719860917242.png

 

I have a field (Cleanliness_Rating) with a text value (from a Domain) of 1-xyzxyzx... , and I want it to slice the first value (1) and return it as a number.

RomanFox_1-1719860978618.png

I have test data in the fields.  Am I missing something that will allow the field to be found?

 

Thanks,

 

 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

Try using bracket notation, $feature['Cleanliness_Rating']. Dot notation works most of the time, but bracket notation works all the time.

Also, Arcade isn't Python, so it's really not going to work that way. What you want is the text function Mid for getting text out from the string, or Left to get the first character. Try this instead:

Left($feature['Cleanliness_Rating'], 1)

 

- Josh Carlson
Kendall County GIS

View solution in original post

8 Replies
jcarlson
MVP Esteemed Contributor

Try using bracket notation, $feature['Cleanliness_Rating']. Dot notation works most of the time, but bracket notation works all the time.

Also, Arcade isn't Python, so it's really not going to work that way. What you want is the text function Mid for getting text out from the string, or Left to get the first character. Try this instead:

Left($feature['Cleanliness_Rating'], 1)

 

- Josh Carlson
Kendall County GIS
RomanFox
New Contributor III

This makes sense.  I have tried two different configurations of the above idea:

var firstChar = Left($feature['Cleanliness_rating'],1)
var result= Number(firstChar);
result

This results in "Invalid expression. Error on line1. Object not found cleanliness_rating

 

var firstChar = Left($feature['Cleanliness_rating'],1)
var result= Number(firstChar);
result

This results in "Invalid Expression. No features found."

 

I also tried the below idea with this:

var firstChar = Cleanliness_Rating($feature,[Cleanliness_rating],1)
var result= Number(firstChar);
result

The results in "Invalid expression. Error on line 1. Unknown function"

0 Kudos
jcarlson
MVP Esteemed Contributor

Yeah, that third option won't work unless you define the function first.

Those errors on the first two seem strange. Are you positive you're typing the field name correctly?

Let's do some debugging. Can you add some Console statements to the top of your expression and see what it spits out?

Console($feature)

for (var attribute in $feature) {
  Console(attribute, $feature[attribute])
}

Console($feature['Cleanliness_rating'])

 

- Josh Carlson
Kendall County GIS
0 Kudos
RomanFox
New Contributor III

Alright, my test data was corrupted.  I reloaded the data and we are good to go.  The below expression is valid and functioning:

 

var firstChar = Left($feature['Cleanliness_rating'],1)
var result= Number(firstChar);
result

 

Thank you everyone for the help.  I learn something new everyday.

0 Kudos
doyle2jm
New Contributor

Two quick thoughts:

  • Arcade might not like a space between Text(), if there is one. 
  • you could try using Left() instead of slice to see if that would give you the expected result. I am not familiar with 'slice' but also Split() may give you the answer you are looking for too. People tend to stay away from left() because it's not as precise as Split() though. Example w/ left():
    var firstchar = $feature.cleanliness_rating;
    var extractsubstring = Left(firstchar,1);
    var result = Number(extractsubstring);
    return result;

 

RomanFox
New Contributor III

There wasn't a space after "Text"

The above expression results in the message "Invalid Expression. No features found"

0 Kudos
simoxu
by MVP Regular Contributor
MVP Regular Contributor

You mentioned you are using domain for that field: 

"I have a field (Cleanliness_Rating) with a text value (from a Domain) of 1-xyzxyzx..."

To get the descriptive value, you should use  DomainName($feature, 'Cleanliness_Rating'), please give it a try.

https://developers.arcgis.com/arcade/function-reference/feature_functions/#domainname

 

RomanFox
New Contributor III

I gave that a try:

Cleanliness_Rating($feature. 'cleanliness_rating')

This results in " Invalid expression. Error on line 1. Identifier expected"

0 Kudos