When authoring the arcade expression in the Arcade Editor, the "test" $feature may not exist. If it does exist its $Geometry may be null. The "test" feature is the first feature in your dataset. If you don't have any data collected yet then this is a case where you may get an error or 0 because the Arcade editor didn't find a feature in which to test with; but once you are in field maps you'll have a feature and its geometry to work with... its geometry will change as you add points to the line and you calculation will update as a result.
Also, a point feature won't have a length.
In Arcade, you will want to protect yourself from null geometries before trying to using it as a argument in a function, such as with the Length functions. Otherwise, you may get errors until the geometries exist.
Here is an example using a couple of test geometries unrelated to a feature:
// CREATE a POLYLINE TO TEST WITH
var polylineJSON =
{
"paths": [
[
[-97.06138,32.837],[-97.06133,32.836],
[-97.06124,32.834],[-97.06127,32.832]
],
[
[-97.06326,32.759],[-97.06298,32.755]
]
],
'curvePaths': [[]],
"spatialReference": { "wkid": 3857 }
};
// TEST 1 -> Non Empty Geom
var geom = Geometry(polylineJSON);
// Test 2 -> Empty Geom
// var geom = Geometry({})
console(geom);
if (!IsEmpty(geom)){return Round(lengthGeodetic(geom, 'feet'), 4)};
// ^^^ Returns 0.0294 feet -> Testing with the "Non Empty Geom"
// ^^^ Returns Null -> Testing with the "Empty geom"
The same thing, but using the feature's geometry:
if (!IsEmpty(Geometry($feature))) { /* -> Handle No Geometry*/
return round(lengthGeodetic(Geometry($feature), 'feet'), 4)
}; // ELSE -> Return Nothing
@MichaelLohr makes some great points below about the shape_length or area fields. I would not use them for the purpose you need them for. Instead access the geometry itself to determine the length on-the-fly with Arcade.
Keep in mind, that this too will stop being a source of truth if the lines are manipulated outside of field maps or the map viewer that uses the field maps form as the calculation won't update if not editing with the form. You would then need to calculate manually again for it to be correct.
- Justin Reynolds, PE