Select to view content in your preferred language

Arcade Labels in Pro: Return list of all fields, concatenate field names

1467
7
Jump to solution
08-31-2022 02:58 AM
sofoo
by
Frequent Contributor

Trying to do some looping in Arcade label expression in Pro. 

First, the following script works fine on a hosted feature layer, but on a feature class in a file geodatabase, it only returns the first field (ObjectID).  


function countAnalyte(){
     var allFields = []
     for (var j in $feature){
          Push(allFields, j)
     }
return allFields
}

Second, I want to iterate through fields and build field names dynamically.  I have a feature class with columns: AName1, AResult1, AName2, AResult2.  The feature class may have unspecified numbers of these repeats.  I want to build it so I can iterate on the number, and use each set of "AName" and "AResult" fields and their values to build the label expression.  Something like this:

var acount = [1,2,3]

for (var i in acount){

        $feature.AName+i //use value for exp

        $feature.AResult+i //use value for exp

}

@XanderBakker any help is greatly appreciated!

 

Tags (3)
0 Kudos
2 Solutions

Accepted Solutions
MikeMillerGIS
Esri Frequent Contributor

That stinks!  What if you use the Expects($feature,'*') and then loop over the feature, does that return all the fields?  I would caution against this if it works as the labeling engine will returning a full row.

View solution in original post

JohannesLindner
MVP Frequent Contributor

Hmmm, interesting. I'm still on 2.8, so I can't test it.

@sofoo Assuming you use Pro 2.9 or later, Does this label expression work for feature classes?

Expects($feature, "*")
var allFields = []
for (var j in $feature){
    Push(allFields, j)
}
return allFields

 


Have a great day!
Johannes

View solution in original post

0 Kudos
7 Replies
JohannesLindner
MVP Frequent Contributor

First, the following script works fine on a hosted feature layer, but on a feature class in a file geodatabase, it only returns the first field (ObjectID).  

Confirmed. The expression works for Feature Services, but not for features from a FGDB or EGDB (Microsoft Server). You can access the fields by using $feature.Field, but only OBJECTID abd GlobalID are available in the attributes dictionary:

JohannesLindner_0-1661941601337.png

This seems like a bug.

 


Have a great day!
Johannes
0 Kudos
MikeMillerGIS
Esri Frequent Contributor

What about using the Schema function?

 

var fields = [];
var values = [];
var all_fields = Schema($feature)['fields'];
for (var i in all_fields){
    var field_dict = all_fields[i]
    var x = field_dict['name']
    Push(fields,x)
    push(values, $feature[x])
}
return values
JohannesLindner
MVP Frequent Contributor

Schema() isn't available in the Labeling profile.


Have a great day!
Johannes
0 Kudos
MikeMillerGIS
Esri Frequent Contributor

That stinks!  What if you use the Expects($feature,'*') and then loop over the feature, does that return all the fields?  I would caution against this if it works as the labeling engine will returning a full row.

JohannesLindner
MVP Frequent Contributor

Hmmm, interesting. I'm still on 2.8, so I can't test it.

@sofoo Assuming you use Pro 2.9 or later, Does this label expression work for feature classes?

Expects($feature, "*")
var allFields = []
for (var j in $feature){
    Push(allFields, j)
}
return allFields

 


Have a great day!
Johannes
0 Kudos
sofoo
by
Frequent Contributor

@MikeMillerGIS  @JohannesLindner  That's exactly what i needed!  Thank you!  They keep saying all functions in Pro are available in all other parts of the Enterprise but I keep running into functions that don't work with label expressions...

Also I am in Pro 2.9.3.

0 Kudos
JohannesLindner
MVP Frequent Contributor

Well, "they" are kinda wrong.

There are multiple contexts in which you can use Arcade, so called "profiles".  For example Labeling, Attribute Rules, Popups.

Different profiles have different globals and functions they can access. This is often done for performance reasons. Eg its's OK to load a feature layer and intersect it with a feature if you are working with a popup. But you can't do that in the Labeling profile, because it would be too slow to do the same operation for all features in the map extent.

 

You can find a list of the different profiles here: Profiles | ArcGIS Arcade | ArcGIS Developers

If a function is only available to some profiles, it is explicitly stated in the Function Reference: Function Reference | ArcGIS Arcade | ArcGIS Developers


Have a great day!
Johannes
0 Kudos