I'm struggling to get this attribute rule working.
My Setup:
Parent Layer: WNV_StagnantWaterSites (point feature class)
Related Layer: WNV_SiteVisits (point feature class)
Relationship: 1:M (SiteCode as Primary & Foreign Key, short numeric)
Notification: Both directions
Record Count: 628 parent records, 50,000+ related records
Relationship Class: WNV_StagnantWaterSites_TO_WNV_SiteVisits
Goal:
I need to update the SiteRisk field (text: "Other", "Low", "Moderate", "High") in WNV_StagnantWaterSites based on related WNV_SiteVisits records where LarvacideRequest = 1 (Yes) in the last 12 months (SiteVisitDate field).
Logic:
0 records → SiteRisk = Low
1 record → SiteRisk = Moderate
2+ records → SiteRisk = High
Downgrades:
High → Moderate if no Yes records in 12 months
Moderate → Low if no Yes records in 12 months
Issue:
My attribute rule validates and saves, but SiteRisk does not update, no matter how I modify WNV_SiteVisits. Any help from an Attribute Rule Guru would be greatly appreciated! 🙏
// Relationship class name
var relationshipName = "WNV_StagnantWaterSites_TO_WNV_SiteVisits";
// Define the cutoff date for the last 12 months
var today = Now();
var twelveMonthsAgo = DateAdd(today, -12, "months");
// Fetch related site visit records (Child Features)
var relatedVisits = FeatureSetByRelationshipName($feature, relationshipName, ["LarvacideRequest", "SiteVisitDate"], false);
// Debugging: Log how many related visits were found
Console("🔍 Found " + Count(relatedVisits) + " related site visit records.");
// Filter visits where LarvacideRequest = 1 (Yes) AND occurred in the past 12 months
var recentLarvacideVisits = Filter(relatedVisits, "LarvacideRequest = 1 AND SiteVisitDate >= @twelveMonthsAgo");
// Count qualifying visits
var visitCount = Count(recentLarvacideVisits);
// Debugging: Log how many visits met the criteria
Console(":calendar: " + visitCount + " site visits in the past 12 months had LarvacideRequest = 1.");
// Determine new SiteRisk value based on visit count
var newSiteRisk = "";
if (visitCount == 0) {
newSiteRisk = "Low";
} else if (visitCount == 1) {
newSiteRisk = "Moderate";
} else if (visitCount >= 2) {
newSiteRisk = "High";
}
// Fetch the parent feature (WNV_StagnantWaterSites)
var parentFeatures = FeatureSetByRelationshipName($feature, relationshipName, ["SiteRisk"], true);
// Ensure we have at least one parent
if (Count(parentFeatures) > 0) {
var parent = First(parentFeatures);
// Get the existing SiteRisk value
var currentRisk = parent.SiteRisk;
// Debugging: Log the existing SiteRisk before updating
Console("🔄 Current SiteRisk: " + currentRisk);
// Only update if the risk level actually changes
if (currentRisk != newSiteRisk) {
Console("✅ Updating SiteRisk from " + currentRisk + " to " + newSiteRisk);
return {
"edit": [
{
"className": "WNV_StagnantWaterSites",
"updates": [
{
"objectID": parent.OBJECTID,
"attributes": {
"SiteRisk": newSiteRisk
}
}
]
}
]
};
} else {
Console(":warning: No update needed. SiteRisk remains: " + currentRisk);
}
} else {
Console("❌ No parent found for this site visit.");
}
// No update needed
return null;
Solved! Go to Solution.
I saw this issue in my testing. I'm looking to get some answers internally. This was discussed way back when here Attribute Rule Delete Trigger to Update Another Feature with no luck. I tried to modify our code to use, as the article suggests, (line 10 - 14) to check if the edit is DELETE and replace $feature with $originalFeature with no luck.
// Relationship class name
var relationshipName = "WNV_StagnantWaterSites_TO_WNV_SiteVisits";
var parentGlobalID;
var newSiteRisk = "";
// Define the cutoff date for the last 12 months
var today = Now();
var twelveMonthsAgo = DateAdd(today, -12, "months");
var childFeature = $feature
if ($editcontext.editType == "Delete"){
childFeature = $originalFeature
}
// Fetch related parent record (WNV_StagnantWaterSites)
var relatedParent = FeatureSetByRelationshipName(childFeature, "WNV_StagnantWaterSites_TO_WNV_SiteVisits", ["GlobalID"], false);
for (var parent in relatedParent) {
parentGlobalID = parent.GlobalID;
// Fetch related site visit records (Child Features)
var relatedVisits = FeatureSetByRelationshipName(parent, "WNV_StagnantWaterSites_TO_WNV_SiteVisits", ["GlobalID","LarvacideRequest","SiteVisitDate"], false);
// Debugging: Log how many related visits were found
Console(" Found " + Count(relatedVisits) + " related site visit records.");
// Filter visits where LarvacideRequest = 1 (Yes) AND occurred in the past 12 months
var recentLarvacideVisits = Filter(relatedVisits, "LarvacideRequest = 1 AND SiteVisitDate >= @twelveMonthsAgo");
// Count qualifying visits
var visitCount = Count(recentLarvacideVisits);
// Debugging: Log how many visits met the criteria
Console(":calendar: " + visitCount + " site visits in the past 12 months had LarvacideRequest = 1.");
// Determine new SiteRisk value based on visit count
if (visitCount == 0) {
newSiteRisk = "Low";
} else if (visitCount == 1) {
newSiteRisk = "Moderate";
} else if (visitCount >= 2) {
newSiteRisk = "High";
}
}
return {
'edit': [{
'className': 'WNV_StagnantWaterSites',
'updates': [{
'globalID': parentGlobalID,
'attributes': {
"SiteRisk": newSiteRisk
}
}]
}]
}
I took yours above and tried this to see if it would do the trick, but no luck as well! Getting close I think though! I would think it's possible! (hopeful positivity lol!)
// Relationship class name
var relationshipName = "WNV_StagnantWaterSites_TO_WNV_SiteVisits";
var parentGlobalID;
var newSiteRisk = "";
// Define the cutoff date for the last 12 months
var today = Now();
var twelveMonthsAgo = DateAdd(today, -12, "months");
// Determine if the operation is a delete and set the correct reference
var childFeature = $feature;
if ($editcontext.editType == "Delete") {
childFeature = $originalFeature;
}
// Fetch related parent record (WNV_StagnantWaterSites)
var relatedParent = FeatureSetByRelationshipName(childFeature, relationshipName, ["GlobalID"], false);
if (IsEmpty(relatedParent)) {
Console("❌ No related parent found for deleted site visit.");
return;
} else {
Console("✅ Found parent feature. Proceeding with update.");
}
// Process the parent feature
for (var parent in relatedParent) {
parentGlobalID = parent.GlobalID;
// Fetch all remaining site visits **AFTER** deletion
var remainingVisits = FeatureSetByRelationshipName(parent, relationshipName, ["LarvacideRequest", "SiteVisitDate"], false);
Console("🔎 Remaining visits after delete: " + Count(remainingVisits));
// If no visits remain, reset SiteRisk to "Low"
if (Count(remainingVisits) == 0) {
Console("🛑 No remaining site visits. Resetting SiteRisk to Low.");
newSiteRisk = "Low";
} else {
// Filter visits where LarvacideRequest = 1 AND occurred in the past 12 months
var recentLarvacideVisits = Filter(remainingVisits, "LarvacideRequest = 1 AND SiteVisitDate >= @twelveMonthsAgo");
var visitCount = Count(recentLarvacideVisits);
Console(":bar_chart: Recalculating SiteRisk: " + visitCount + " valid site visits remain.");
// Assign SiteRisk based on visit count
if (visitCount == 0) {
newSiteRisk = "Low";
} else if (visitCount == 1) {
newSiteRisk = "Moderate";
} else if (visitCount >= 2) {
newSiteRisk = "High";
}
}
}
// Apply the SiteRisk update to the parent feature
return {
'edit': [{
'className': 'SDEDB1.GISADMIN.WNV_StagnantWaterSites',
'updates': [{
'globalID': parentGlobalID,
'attributes': {
"SiteRisk": newSiteRisk
}
}]
}]
};
@JamesBooth I still have not heard back from our internal resources. As soon as I do, I'll post.