I have a data collection layer with a field for Trail ID that I would like to have auto-populate from the Trail ID field of the trails layer that I have in the same field map. These are separate layers within the same web map. I found this post and modified the calculated expression for populating this Trail ID field to the Arcade expression below (with a change from $datastore to $map to select the trails layer). This passed the error test in AGOL but when I try to do a test data collection in the Field Maps app, I get a calculation error. Does anyone have any ideas how I can get this to work?
var trailLayer = FeatureSetByName($map,"R1 EDW Trail NFS Publish", ["TRAIL_NO"]);
var searchDistance = 50;
var trailIntersect = Intersects(trailLayer, BufferGeodetic($feature, searchDistance, "feet"));
var cnt = Count(trailIntersect);
var minDistance = Infinity;
var name = Null
if (cnt > 0) {
for (trail in trailIntersect) {
var dist = DistanceGeodetic(trail, $feature, "feet");
if (dist < minDistance) {
name = trail.TRAIL_NO;
minDistance = dist
}
}
} else {
// pass no features found within search distance, id remains null
}
return name;