Select to view content in your preferred language

Attribute rule to update parent attribute from most recent child record

279
2
04-24-2025 09:50 AM
johnchewitt77
Emerging Contributor

Hi - I would love some help please. I'm trying to write an attribute rule on a relationship class that will update an attribute on the parent record if the most recent child record is updated. The parent class is sampling sites and the child class is samples. So if an attribute on the most recent sample is set (Public_sample_date attribute), then the attribute on the site itself should be updated too. But I'm getting an error.... 

I am getting this error...

johnchewitt77_0-1745513357583.png

 

My code is below...

//Check if the updated Sample Record is valid
if ($feature.invalid_result == 'Yes') {
return;
}

// Get the parent feature (should be just one)
var parent = First(FeatureSetByRelationshipName($feature, "Bathing_Water_Sites_SampleResults",["water_quality_indicator"], false));

// Exit if no parent found
if (IsEmpty(parent)) {
return;
}

// Get all children related to that parent
var children = FeatureSetByRelationshipName(parent, "Bathing_Water_Sites_SampleResults",["Public_sample_date","water_quality_indicator"], false);

// Initialize most recent timestamp and result
var latestDate = Date(1900, 1, 1); // Arbitrary old date
var latestWQI = null;
var thisfeaturedate = $feature.Public_sample_date

// Loop through children to find the most recently updated one
for (var child in children) {
if (!IsEmpty(child.Public_sample_date) && child.Public_sample_date > latestDate) {
latestDate = child.Public_sample_date;
latestWQI = child.water_quality_indicator;
}
}

// Exit if the updated feature is not the latest one
if (thisfeaturedate < latestDate) {
return;
}

// Proceed if parent exists
if (!IsEmpty(parent)) {
// Update the parent's water_quality_indicator with the water_quality_indicator of the most recent sample record
return {
'edit': [{
'className': 'Bathing_Water_Sites',
'updates': [{
'objectID': parent.OBJECTID,
'attributes': {
'water_quality_indicator': latestWQI
}
}]
}]
};
}

Tags (2)
0 Kudos
2 Replies
DavidSolari
MVP Regular Contributor

I think your listing missed the line where you grab the child's parent, but either way: that error is a sign that you have a parent to child update going on — either from an attribute rule on the parent or a composite relationship class — that retriggers the child, which retriggers the parent, and so on. Check what you have going on in the parent and look for potential loops. In some cases you can fix this by comparing fields in the $feature with matching fields in the $originalfeature and returning early if there were no changes.

johnchewitt77
Emerging Contributor

Hi David - I really appreciate the reply, thanks a lot. You were right, I'd omitted the first few lines by mistake so they are now added. If it helps, I commented out the last part that updates the attributes and the code worked i.e. it does populate the latestWQI value, so I'm guessing the problem is with the last bit that updates the parent attribute.

0 Kudos