I'm currently trying to create a pop-up which pulls data from related tables.
I've been successful in puling data from the table immediately related to the feature layer. But there is a table related to that related table that I also want to pull data from. The feature layer is a polygon, the related table has species that occur within that polygon and the secondary related table is supplemental information about that species.
Feature Layer <> Related Table <> Related Table
This is my current expression for my feature layer:
var relatedrecords = FeatureSetByRelationshipName($feature,"Provincially_Tracked_Species_Detail");
var popupString = ""
for (var f in relatedrecords){
popupString += Text(f.Com_Name) + TextFormatting.NewLine +
"Scientific Name: " +
DefaultValue(f.Sci_Name, 'no data') + TextFormatting.NewLine +
"Taxon: " +
DefaultValue(f.Lower_Taxon, 'no data') + TextFormatting.NewLine +
"SARO Status: " +
DefaultValue(f.SARO_Status, 'no data') + TextFormatting.NewLine +
TextFormatting.NewLine
}
return popupString
These are my results:
I have also made an expression for my first related table pulling data from the second related table but because it is an expression not a real field it won't let me use it like any of the other fields (as I did with other fields in the above example).
Hopefully, this makes sense. If you have any suggestions on a solution, please let me know.
Solved! Go to Solution.
Pulling additional details would be just like your current expression, with the for loop being nested and repeated.
var relatedrecords = FeatureSetByRelationshipName($feature, "Provincially_Tracked_Species_Detail")
for (var f in relatedrecords){
... // your existing popup string stuff here
var secondary_related = FeatureSetByRelationshipName(f, "whatever_the_other_relationship_name_is")
for (var s in secondary_related){
// Add more to your output string
popupString += s.some_attribute
}
}
Also, you may be interested to know that you can easily create a multiline string using a backtick string, and pipe in all the attributes at the same time.
These lines of code:
popupString += Text(f.Com_Name) + TextFormatting.NewLine +
"Scientific Name: " +
DefaultValue(f.Sci_Name, 'no data') + TextFormatting.NewLine +
"Taxon: " +
DefaultValue(f.Lower_Taxon, 'no data') + TextFormatting.NewLine +
"SARO Status: " +
DefaultValue(f.SARO_Status, 'no data') + TextFormatting.NewLine +
TextFormatting.NewLine
Are equivalent to these:
popupString += `${Text(f.Com_Name)}
Scientific Name: ${DefaultValue(f.Sci_Name, 'no data')}
Taxon: ${DefaultValue(f.Lower_Taxon, 'no data')}
SARO Status: ${DefaultValue(f.SARO_Status, 'no data')}
`
Pulling additional details would be just like your current expression, with the for loop being nested and repeated.
var relatedrecords = FeatureSetByRelationshipName($feature, "Provincially_Tracked_Species_Detail")
for (var f in relatedrecords){
... // your existing popup string stuff here
var secondary_related = FeatureSetByRelationshipName(f, "whatever_the_other_relationship_name_is")
for (var s in secondary_related){
// Add more to your output string
popupString += s.some_attribute
}
}
Also, you may be interested to know that you can easily create a multiline string using a backtick string, and pipe in all the attributes at the same time.
These lines of code:
popupString += Text(f.Com_Name) + TextFormatting.NewLine +
"Scientific Name: " +
DefaultValue(f.Sci_Name, 'no data') + TextFormatting.NewLine +
"Taxon: " +
DefaultValue(f.Lower_Taxon, 'no data') + TextFormatting.NewLine +
"SARO Status: " +
DefaultValue(f.SARO_Status, 'no data') + TextFormatting.NewLine +
TextFormatting.NewLine
Are equivalent to these:
popupString += `${Text(f.Com_Name)}
Scientific Name: ${DefaultValue(f.Sci_Name, 'no data')}
Taxon: ${DefaultValue(f.Lower_Taxon, 'no data')}
SARO Status: ${DefaultValue(f.SARO_Status, 'no data')}
`
It worked! Thanks so much for your help and additionally your advice to simplify my code. Also, generally speaking thanks for your contribution and help on this forum. It's greatly appreciated!
For some reason the code works in the test but nothing shows in the pop-up. When I comment out the new section to that secondary related table, the information shows up fine. Do you know why this would be happening?
Curious. I've seen this sort of thing happen before. I wonder if it has to do with a null value somewhere. Can you find out what UTM Zone the test is running against and check the popup of that feature?
Also, can you try to replace the "for (var s in secondary_related)" section with something else? Maybe just add Count(secondary_related) to the output string to make sure that it's finding something? Sometimes a for loop errors out when there's nothing in the featureset.
Thanks again! It was due to my id field (Sci_Name) having null values. I had to keep those null values so I ended up putting in an if statement and that solved the problem.