I'm trying to write an expression to calculate the elevation field of stormwater devices from the intersecting polygon from another layer in the same REST service. I have been able to calculate fields from intersecting features before, but for some reason it keeps giving me an error. If I use $datastore, I get table not found. If I use $map or $layer, I get object not found, even though the layer is loaded in the current map. The error is coming on the first assignment statement. What am I getting wrong?
// Retrieve the DEM polygons feature set
var demFeatures = FeatureSetByName($datastore, "Elev_Polygons_2023", ["gridcode"], true);
// Find the polygons that intersect with the current device
var intersectingPolygons = Filter(demFeatures, 'Intersects($feature)');
// Get the first intersecting polygon
var intersectingPolygon = First(intersectingPolygons);
// Extract the gridcode value if an intersecting polygon exists
var elevation = intersectingPolygon["gridcode"];
// Return the elevation value
return elevation;