I'm just starting to use Arcade - like it a lot so far but there is one scenario that I can't figure out. I want to retrieve the set of related records associated with a feature for my ArcGIS Online web map popup. I can do this with this statement
var fs = FeatureSetByRelationshipName($feature, "Program", ["ProgramName"], false);
It works great except when the feature has no related records. I still get a FeatureSet returned, and I would expect it to contain zero features. But any method I use to access the FeatureSet returns "Execution Error:Cannot read property 'toString' of null". I've tried all of the following:
What is the trick to allow me to detect in my code that the feature set is empty?
Solved! Go to Solution.
OK - I finally figured out what I need to do to get this to work. Since the feature class that I'm in is the destination of the 1 to many relationship, there can only be either zero or one related records. So I check the foreign key field for null before calling the FeatureSetByRelationshipName function like this and that avoids the error that I was getting:
if ($feature.ProgramGUID != null) {
return First(FeatureSetByRelationshipName($feature, "Program", []));
}
else {
return null
}
Thanks for helping me work through this Xander!
Hi don.morrison ,
You are right to asume that a FeatureSet is returned which will have 0 features if there are no records in the related table. It is strange that you get this unexpected behavior.
I just did a test and it works for me:
var fsrel = FeatureSetByRelationshipName($feature,"RelData");
Console(Count(fsrel));
Console(fsrel);
Console(TypeOf(fsrel));
In case of not having a related record in the table it returns as expected:
object, FeatureSet
0
FeatureSet
Did you use the GUI to define the FeatureSetByRelationshipName row in your expression? It is either caused by that or perhaps caused by your data.
If you would be able to share the data or web map with me I could take a closer look.
Hi Xander. Yes - I used the GUI to create the Arcade expression. I have 3 feature classes that have a relationship with the "Program" table and they all seem to have the same problem. If you want to take a look first hand I can send you credentials to our test organization - let me know where to send that.
I thought maybe it had something to do with my data being served up from an ArcGIS server REST endpoint - but you can see here that it works for one relationship but not the other.
var fs1 = FeatureSetByRelationshipName($feature,"Site_ConservationMeasures")
var fs2 = FeatureSetByRelationshipName($feature,"Program")
Console(Count(fs1))
Console(Count(fs2))
Use Console Function to output messages.
0
Execution Error:Cannot read property 'toString' of null
Here is what the relationship looks like:
Hi don.morrison ,
You can have multiple relationships to a table called "Program", but each relationshipname will be different. I think that is what is causing the error that you see. Otherwise you should get a featureset (which could have 0 features).
Do you mean that the "Relationship Class" names must be different? In any case, they are unique, but some of the path labels are not. ROW_Habitat.SDE.Program is the table that seems to be the one that causes the problem - it participates in 4 relationships. It is the origin in all of them.
Relationship Class name | Backward path label | Forward path label |
---|---|---|
ROW_Habitat.SDE.Program_to_MgtAreas | Program | ManagementArea |
ROW_Habitat.SDE.Program_to_MgtMeasures | Program | MgmtArea_ConservationMeasures |
ROW_Habitat.SDE.Program_to_Sites | Program | Site |
ROW_Habitat.SDE.Program_to_PollinatorScorecards | Program | PollinatorScorecard |
Hi Don Morrison ,
When you want to access data using a relationship name, the best way to do this in AGOL is to go through the interface to make sure you use the correct name for the relationship in Arcade.
So in the Arcade expression GUI, expand the $feature (click on ">" right from $feature):
Next at the bottom you will find something called "Related records" (or similar, sorry for my Spanish screenshot). (click on ">" right from "Related records"):
Then you will see the relationships listed. Click on the link "FeatureSetByRelationshipName(...)" to get the correct line in the Arcade expression editor window.
Hi Xander - that is exactly how I did it. And the expression does work for those features where there exists one or more related records. The only problem is when there are no related records. I'd like to use the Count function to determine whether any records exist in the returned FeatureSet, but the function throws an error (is there a way to catch the error?) when it should return a count of 0.
OK - I finally figured out what I need to do to get this to work. Since the feature class that I'm in is the destination of the 1 to many relationship, there can only be either zero or one related records. So I check the foreign key field for null before calling the FeatureSetByRelationshipName function like this and that avoids the error that I was getting:
if ($feature.ProgramGUID != null) {
return First(FeatureSetByRelationshipName($feature, "Program", []));
}
else {
return null
}
Thanks for helping me work through this Xander!
Thanks for sharing don.morrison , glad you could resolve it!
I wanted to thank you for posting this. I ran into a similar error that cropped up in FieldMaps.
~My custom scripts were working fine okay* in AGOL and the ExB App, but when viewed from the FieldMaps App would throw 'unknown data type' errors.
My initial statement wound up being:
Features that would have a related record also had a CATEGORY field value containing the text 'Start' |
I really ought to revamp the whole thing, I've realized. I don't think a foreign GUID was used.
*At some point, the AGOL map was producing "Execution Error:d is null" errors for the same scripts, but it stopped for no reason I can determine.