I'm trying to use a calculated expression to auto populate several fields in a layer that I'm collecting. I have 3 expressions that are doing basically the same thing, but only 2 of the 3 expressions work. I get an error that says Failed to Calculate and because of this error I can't submit the collected point. Below is the Arcade expression that I'm using. The 3 calculated expressions are using this same code, but pulling data from different layers from the map.
if (IsEmpty(Geometry($feature))){
return null
}
var parcels = FeatureSetByName($map,"Parcels")
var bufferedLocation = Buffer($feature, 100, "feet")
var candidateParcels = Intersects(parcels, bufferedLocation)
var featuresWithDistances = []
for (var f in candidateParcels){
Push(featuresWithDistances,
{
"distance": Distance($feature, f, "feet"),
"feature": f
}
)
}
function sortByDistance(a,b){
return a["distance"] - b["distance"]
}
var sorted = Sort(featuresWithDistances, sortByDistance)
var closestFeatureWithDistance = First(sorted)
if (IsEmpty(closestFeatureWithDistance)){
return null
}
return `${closestFeatureWithDistance["feature"]["ADDRESS"]}`
Solved! Go to Solution.
Checking the Field Map's logs will probably give you insight to why it failed to calc.
You also make sure that the layer you are using (featureSetByName) loads successfully in Field Maps. Since you are working with Geometry you might also make sure it is drawing at the map scale you
Many of the geometry functions are subject to map scale. Including Buffer, Distance and Intersect. You may be running into a scenario here that isn't trapped by one of your "return null" checks.
https://developers.arcgis.com/arcade/function-reference/geometry_functions/#distance
Be aware that using $feature as input to this function will yield results only as precise as the view's scale resolution. Therefore values returned from expressions using this function may change after zooming between scales. Read more here.
Are you the correct Sort function? This is what is used to sort distance in other examples
function compareDistance(a,b){
if(a['Distance']<b['Distance'])
return -1;
if(a['Distance']>b['Distance'])
return 1;
return 0;
}
Thanks for the reply. I'm assuming that the Sort function is correct. I got it from #7 from this ESRI blog article
Checking the Field Map's logs will probably give you insight to why it failed to calc.
You also make sure that the layer you are using (featureSetByName) loads successfully in Field Maps. Since you are working with Geometry you might also make sure it is drawing at the map scale you
Many of the geometry functions are subject to map scale. Including Buffer, Distance and Intersect. You may be running into a scenario here that isn't trapped by one of your "return null" checks.
https://developers.arcgis.com/arcade/function-reference/geometry_functions/#distance
Be aware that using $feature as input to this function will yield results only as precise as the view's scale resolution. Therefore values returned from expressions using this function may change after zooming between scales. Read more here.
Justin,
I took a look at the logs and I saw that the error was "The target server failed to respond" so I changed the layer to a more reliable one and now it's working as expected. Thanks for your help.
Michael