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
}
}
]
}
]
}
}
Solved! Go to Solution.
I was able to fix it! Here is the completed code in case anyone coming across this thread would like to reference it:
// filter the parent class for only related features
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 tabledata = FeatureSetByName($datastore, "FFS_GIS.ArchSitePl_Insp_Tb");
var ordered_dates = OrderBy(tabledata, "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.GUID,
"edit": [
{
"className" : "FFS_GIS.ArchSite_Pl",
"updates": [
{"globalID" : parent_id,
"attributes":
{
"Last_Monitored" : relatedinfo
}
}
]
}
]
}
}
You're supplying a Feature to the OrderBy function, which needs a FeatureSet. Are you trying to order the FeatureSet that the feature is a part of or the parent layer?
I am trying to order the FeatureSet that the feature is a part of (the inspection table), what command should I use instead of $feature?
I believe you have to get it from the $datastore, like you got the parent table.
I was able to fix it! Here is the completed code in case anyone coming across this thread would like to reference it:
// filter the parent class for only related features
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 tabledata = FeatureSetByName($datastore, "FFS_GIS.ArchSitePl_Insp_Tb");
var ordered_dates = OrderBy(tabledata, "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.GUID,
"edit": [
{
"className" : "FFS_GIS.ArchSite_Pl",
"updates": [
{"globalID" : parent_id,
"attributes":
{
"Last_Monitored" : relatedinfo
}
}
]
}
]
}
}