# Calculated Expression Error: Failed to Calculate

529
4
08-04-2022 07:11 AM
Occasional Contributor

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
}

1 Solution

Accepted Solutions
Occasional Contributor III

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.

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 Reynolds, PE
4 Replies
by
MVP Esteemed Contributor

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;
}``````

Occasional Contributor

Thanks for the reply. I'm assuming that the Sort function is correct. I got it from #7 from this ESRI blog article

Occasional Contributor III

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.