Hey all,
I’m writing a calculated expression to populate a field in a Field Maps form. The function is to find the nearest creek feature and extract a value to populate the field in my new point. All new points will be very close to a creek. As a matter of fact, the collection tech may be standing directly in the water so finding a close feature isn’t an issue. When I run the script within the form’s calculated expression the result is null, not an error, which leads me to believe it’s executing properly. Since I haven't actually digitized a point yet there’s no location from which to calculate. However, when I test collecting a point in field maps the calculation fails. Any pointers would be greatly appreciated. Thanks in advance.
// Get the geometry of the new digitized point
var pointGeometry = Geometry($feature);
var nearestCreek = null;
var nearestDistance = null;
// Access the Creek Reaches 2025 feature set
var creeks = FeatureSetByName($map, "Inspection 2025/Creek Reaches 2025");
// Loop through each creek to find the nearest one
for (var creek in creeks) {
var creekGeometry = Geometry(creek);
var distance = Distance(pointGeometry, creekGeometry);
// Check if this is the closest creek found so far
if (nearestDistance == null || distance < nearestDistance) {
nearestDistance = distance;
nearestCreek = creek;
}
}
// Extract the desired field value (e.g., REACH) from the nearest creek feature
var reachName = null;
if (nearestCreek != null) {
reachName = nearestCreek.REACH; // Ensure the field name matches the one in your dataset
}
// Return the reach name to populate the Reach_Name field
return reachName;