Hello, I'm trying to write a script in Field Maps that will extract and Sum the values from a field in one layer and then insert that value into a field in another layer based on matching fields. I found some example of code that I think is what I need, or at least close to what I need but I can't figure out how to tailor it to my needs. I have a basic understanding of Arcade and have been stuck on this for days.
I have several Project Areas with Cables running throughout the various areas. My goal is to have a script that will Sum the total Length of cables in each project area, and then insert that total into a field in the Project Area, and it does this based on the Area ID in the Cable layer Matching the Area ID in the Project Areas layer.
I have a script that can do this based on Cables that intersect the Project Area, but that won't work because numerous Cables intersect multiple Project Areas, but they are only assigned a single Area ID and thus I want that footage to be attributed to the total for that single Area.
I've included what I have so far below. When I run the script in the code creation box, the output message gives me the Length total for the Entire Cable layer, and then when I go back to the map and click on a Design Area polygon the field is just blank, it doesn't give any footage. Appreciate any help you could offer!
var Cables = FeatureSetByName($map, "Cable", ['Area_ID', 'Length'], true)
var Areas = FeatureSetByName($map, "Project Areas", ['Area_ID'], true)
var totalsum = 0
for (var $feature in Areas) {
var matchvalue = $feature["Area_ID"]
var relatedfeatures = Filter(Cables, "Area_ID = @matchvalue")
for (var $feature in relatedfeatures) {
totalsum += $feature["Length"]
}
}
return totalSum