Arcade - return field from related layer

831
5
Jump to solution
10-13-2021 08:35 AM
Labels (1)
LylianAvero
New Contributor II

Hi!

I need help with Arcade expression. I have polygon layer and related tables that are related based on these fields: (Global_ID=ReferenceID)

In the attribute table for the related table, I'd like to return a field value from related polygon layer

Thank you in advance!!

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Regular Contributor
// load your polygon featureset
// https://developers.arcgis.com/arcade/function-reference/data_functions/#featuresetbyname
var polygon_fs = FeatureSetByName(...)

// filter your polygons
var ref_id = $feature.ReferenceID
var polygon_filter = Filter(polygon_fs, "Global_ID = @ref_id")

// return the attribute
if(Count(polygon_filter) > 0) {
  return First(polygon_filter).Attribute
}
return null

Have a great day!
Johannes

View solution in original post

5 Replies
JohannesLindner
MVP Regular Contributor
// load your polygon featureset
// https://developers.arcgis.com/arcade/function-reference/data_functions/#featuresetbyname
var polygon_fs = FeatureSetByName(...)

// filter your polygons
var ref_id = $feature.ReferenceID
var polygon_filter = Filter(polygon_fs, "Global_ID = @ref_id")

// return the attribute
if(Count(polygon_filter) > 0) {
  return First(polygon_filter).Attribute
}
return null

Have a great day!
Johannes
LylianAvero
New Contributor II

Thank you so much, it worked! I was missing "Count" part

0 Kudos
KelseySmuczynski
New Contributor II

This worked for me initially, but when I returned the next day to continue populating for other fields I received an error: "Execution Error: Cannot read properties of null (reading 'toString')".  I think the issue is stemming from the filter variable -- somehow the GlobalID and GUID are not matching? Or something is null somewhere? Do you have any ideas on how best to resolve the issue? Any help would be appreciated. Below is the code I am running.

var wells_fs=FeatureSetByName($map,"Wellsites2")
var ref_guid= $feature.GUID
var wells_filter = Filter(wells_fs, "GlobalID = @ref_guid")

if(Count(wells_filter) > 0) {
return First(wells_filter).name
}
return null

 

0 Kudos
JohannesLindner
MVP Regular Contributor

The filter query is OK. But you are right, the problem is related to the filter query and yes, something is null somewhere.

This is probably caused by null values in $feature.GUID.

When you execute this expression for a feature whose GUID is not in RelatedTable.GlobalID (but not null), Filter() returns an empty feature set, which is the reason for the empty check before accessing the First() element (better way in the code below).

When you execute the expression for a feature whose GUID is null, Filter() returns null and the expression fails on anything using that result, e.g. Count() or First().

So you just need another check at the top of the expression:

var ref_guid= $feature.GUID

if(ref_guid == null) {
    // there can be no related features
    return null
}

var wells_fs=FeatureSetByName($map,"Wellsites2")
var wells_filter = Filter(wells_fs, "GlobalID = @ref_guid")
var well = First(wells_filter)

if(well == null) {
    // wells_filter is empty, no related features found
    return null
}
return well.name

Have a great day!
Johannes
0 Kudos
jcarlson
MVP Notable Contributor

Is the relate 1:1 or 1:M? The expression may be more or less complex depending on that. Also, what is the context in which you're using this? Certain profiles (symbology, etc) have limitations on what functions are available to you.

Take a look at the expression FeatureSetByRelationshipName. This will return all related features, and individual features and attributes can be accessed directly through the resulting FeatureSet.

In the Expression Builder, you can see related records at the bottom of the $feature globals:

jcarlson_0-1634140281663.png

From there, you can select the particular relation (if there are multiple) that you'd like to bring into your expression.

jcarlson_1-1634140559095.png

 

- Josh Carlson
Kendall County GIS
0 Kudos