Select to view content in your preferred language

Arcade expression help: updating parent features from related inspection table

784
4
Jump to solution
09-09-2024 07:56 AM
KarynaB
Occasional Contributor

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
}
}
]
}
]
}
}

 

 

0 Kudos
1 Solution

Accepted Solutions
KarynaB
Occasional Contributor

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
}
}
]
}
]
}
}

View solution in original post

4 Replies
KenBuja
MVP Esteemed Contributor

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?

0 Kudos
KarynaB
Occasional Contributor

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?

0 Kudos
KenBuja
MVP Esteemed Contributor

I believe you have to get it from the $datastore, like you got the parent table.

0 Kudos
KarynaB
Occasional Contributor

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
}
}
]
}
]
}
}