I am trying to create an attribute rule on a related inspection table that will update a field in the parent table when new inspection records are created (or existing ones are updated). I would like to calculate the most recent inspection date (to fill in the parent table's 'last_monitored' field).
The code that I currently have says that it's valid in the arcade expression builder but comes up with an error when I try to create an inspection record in a Pro web map. The error is in line 18 (var ordered_dates) and states that there is an "expected feature set". Any ideas on what to edit to make this code work as intended? Thank you in advance!
// filter the parent class for only related features
var features = $feature
var parent_id = $feature.GUID;
var parent_class = FeatureSetByName($datastore, "FFS_GIS.ArchSite_Pl", ["GlobalID", "Last_Monitored"], false);
var parent_match = Filter(parent_class, "GlobalID = @parent_id");
// count records and return null if no entries have been made
var cnt = Count(parent_match);
if (cnt == 0) {
return null
};
// order inspection records by most recent to oldest entries and retain most recent record
var ordered_dates = OrderBy($feature, "Monitor_Date DESC");
var relatedinfo = "";
if (cnt > 0) {
var info = First(ordered_dates);
relatedinfo = info.Monitor_Date;
// update parent table field with most recent record data
return {
"result" : $feature.field,
"edit": [
{
"className" : "FFS_GIS.ArchSite_Pl",
"adds": [
{
"attributes":
{
"Last_Monitored" : relatedinfo
}
}
]
}
]
}
}