I have an ArcGIS geodatabase with a parent feature class related to a child stand alone table containing the various inspections made on each parent feature. Primary key is GlobalID and foreign key is GUID.
In ArcGIS Pro, I'm trying to edit an attribute rule to automatically populate a LAST_DATE_INSPECTION field in the parent feature class with the most recent inspection (DATE_INSPECTION field in the child table).
The code I did is only updating the parent LAST_DATE_INSPECTION field with the first inserted inspection date of the child table, not the most recent date. Looks like my filter isn't working.
// calculation attribute rule on child table, field DATE_INSPECTION
// triggers: Insert, Update, Delete
//Foreign key in child table
var fk = $feature.GUID
//Load all inspections of the feature
var related_inspection = FeatureSetByName($datastore, "CHILD_TABLE_NAME",['GUID'],false)
//Filter the most recent inspection date of the feature
var last_inspection = First(Filter(related_inspection, 'GUID = @fk'))
//Return nothing if no date found
if(IsEmpty(last_inspection)){
return ;
}
//Updating DATE_INSPECTION field in parent feature class
return {
"result": fk,
"edit": [{
"className" : "PARENT_FC_NAME",
"updates": [{
//selecting the parent feature to update
"GlobalID" : last_inspection.GUID,
//Update the LAST_DATE_INSPECTION field in parent feature class
"attributes" : {'LAST_DATE_INSPECTION': last_inspection.DATE_INSPECTION
}
}]
}]
};
Solved! Go to Solution.
You're missing a step.
You return the first entry of the filtered feature set, which is the first inspection for that asset.
What you want to do:
//Foreign key in child table
var fk = $feature.GUID
//Load all inspections of the feature
var inspections = FeatureSetByName($datastore, "CHILD_TABLE_NAME",['GUID'],false)
var related_inspections = Filter(inspections, 'GUID = @fk')
// (if you have a relationship class, you can also do it like this)
var related_inspections = FeatureSetByRelationshipName($feature, "RelationshipName", ["GUID"], false)
// (also, you have a trigger on delete. if you delete an inspection, it will
// still be in the table when the rule is executed, so you should filter it out:)
if($editcontext.editType == "DELETE") {
var pk = $feature.PrimaryKeyOfChildTable
related_inspections = Filter(related_inspections, "PrimaryKeyOfChildTable <> @pk")
}
//Filter the most recent inspection date of the feature
var last_inspection = First(OrderBy(related_inspections, "DATE_INSPECTION DESC"))
You're missing a step.
You return the first entry of the filtered feature set, which is the first inspection for that asset.
What you want to do:
//Foreign key in child table
var fk = $feature.GUID
//Load all inspections of the feature
var inspections = FeatureSetByName($datastore, "CHILD_TABLE_NAME",['GUID'],false)
var related_inspections = Filter(inspections, 'GUID = @fk')
// (if you have a relationship class, you can also do it like this)
var related_inspections = FeatureSetByRelationshipName($feature, "RelationshipName", ["GUID"], false)
// (also, you have a trigger on delete. if you delete an inspection, it will
// still be in the table when the rule is executed, so you should filter it out:)
if($editcontext.editType == "DELETE") {
var pk = $feature.PrimaryKeyOfChildTable
related_inspections = Filter(related_inspections, "PrimaryKeyOfChildTable <> @pk")
}
//Filter the most recent inspection date of the feature
var last_inspection = First(OrderBy(related_inspections, "DATE_INSPECTION DESC"))
Hello,
I'm looking to replicate this but for a simple Yes/No attribute field called 'Action_reqd'. All steps seem to be working and the code validates fine, however when updating a child record, I'm getting the "Update table row failed. Invalid column value [Invalid column value] [Action_reqd]"
I'm not sure how to resolve - any pointers gratefully recevived 🙂
Please post the rule your code.
Hello, thanks for the reply - my code is as per below
Parent table = Asset_Car_Park
Child table = Asset_Site_External_Inspection
I'm trying to update the parent table Action_reqd field with the Action_reqd record from the latest inspection survey in the related child table
//Foreign key in child table
var fk = $feature.parentglobalid
if (IsEmpty(fk))
return fk;
fk = Upper(fk);
//Load all inspections of the feature
var inspections = FeatureSetByName($datastore, "Spatial.PDNPA_GEO.Asset_Site_External_Inspection",['parentglobalid', 'Action_reqd'],false)
var related_inspections = Filter(inspections, 'parentglobalid = ')
// (also, you have a trigger on delete. if you delete an inspection, it will
// still be in the table when the rule is executed, so you should filter it out:)
if($editcontext.editType == "DELETE") {
var pk = $feature.GlobalID
related_inspections = Filter(related_inspections, "GlobalID <> ")
}
//Filter the most recent inspection date of the feature
var last_inspection = First(OrderBy(related_inspections, "Inspection_date"))
//Updating Action_reqd field in parent feature class
return {
'result': fk,
'edit': [{
'className' : "Spatial.PDNPA_GEO.Asset_Car_Park",
'updates': [{
//selecting the parent feature to update
'GlobalID' : last_inspection.parentglobalid,
//Update the Action_reqd field in parent feature class
'attributes' : {"Action_reqd": last_inspection.Action_reqd
}
}]
}]
};
Wonderful ! It works perfectly! Thank you for the "delete trigger" script, very useful