<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Attribute Rule: Automatically update related table based on intersections? in Attribute Rules Questions</title>
    <link>https://community.esri.com/t5/attribute-rules-questions/attribute-rule-automatically-update-related-table/m-p/1406089#M1384</link>
    <description>&lt;P&gt;I have a polygon feature class related to a table via Relationship Class (one-to-many, GlobalID in polygon related to a GUID in the table, field called REL_ZONE_ID). I am updating this related table manually. The related table lists the segments that intersect the polygons (polygon labels shown here are a shorthand version of the full Globals, full version listed in the REL_ZONE_ID field). So basically it's a table that keeps track of the lines intersecting a given polygon.&lt;/P&gt;&lt;P&gt;I am moving between Pro 3.1 and 3.2, so I can't yet reliably use "FeatureSetByRelationshipClass".&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="The table shown is the related table, storing a record of each line that intersects a polygon." style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/100130iD81C4A8AAB5E2F1F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="VinceE_0-1712265135365.png" alt="The table shown is the related table, storing a record of each line that intersects a polygon." /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;The table shown is the related table, storing a record of each line that intersects a polygon.&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I would like to do this automatically with a Calculation Attribute Rule, at &lt;/SPAN&gt;&lt;STRONG&gt;INSERT&lt;/STRONG&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;STRONG&gt;UPDATE&lt;/STRONG&gt;&lt;SPAN&gt;, and &lt;/SPAN&gt;&lt;STRONG&gt;DELETE&lt;/STRONG&gt;&lt;SPAN&gt;. My approach:&lt;/SPAN&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Get a FeatureSet of the records that are already related to the original polygon.&lt;/LI&gt;&lt;LI&gt;Mark them for deletion by adding to a "delete" array.&lt;/LI&gt;&lt;LI&gt;Review&amp;nbsp;&lt;EM&gt;all&lt;/EM&gt;&amp;nbsp;line segments (time consuming?); if any intersect the newly updated polygon, append segment ID record to a new "adds" array.&lt;/LI&gt;&lt;LI&gt;Return an "edit" record to delete the originally related records, and add the newly intersected records.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;The below does this, but it's a hack--I needed to add in the if &lt;STRONG&gt;(editType != "DELETE")&amp;nbsp;&lt;/STRONG&gt;because otherwise when a polygon was deleted, the rule would remove the currently related records as expected, but then add the same records back in (except with a NULL related ID, since the polygon would have been deleted, and not have an ID anymore).&lt;/P&gt;&lt;P&gt;I thought I wouldn't need to do the&amp;nbsp;&lt;STRONG&gt;if DELETE&lt;/STRONG&gt;&amp;nbsp;because I thought the "Intersect()" was being called on the&amp;nbsp;&lt;STRONG&gt;$feature&lt;/STRONG&gt;, and not the &lt;STRONG&gt;$originalFeature&lt;/STRONG&gt;. So I assumed that if a feature was deleted, there couldn't be any intersections with the&amp;nbsp;&lt;STRONG&gt;$feature&lt;/STRONG&gt;, and therefore no records would be added to the "adds" array.&lt;/P&gt;&lt;P&gt;To reiterate, the code below does exactly what I want, I just can't do it without the special "DELETE" caveat.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;/*MANAGE RELATED SEGMENTS*/
//---------------------------------------------------------------------//
// INPUTS
var proVersion = 3.1
var editType = $editContext.editType 

// Get original/current polygon feature.
var zone = $originalFeature
var newZone = $feature

// Get all polylines in SEGMENTS feature class. Geometry required for 'Intersects()'.
var segmentsFS = FeatureSetByName($datastore, "SEGMENT", ["SEG_ID"], true)

if (proVersion &amp;gt;= 3.2) {
    // Get table records that are related to the current POLYGON feature.
    var relRecordsFS = FeatureSetByRelationshipClass(zone, "ZONE_SEG_RELATION", ["*"], false)
} else {
    // Get all records from the related table.
    // Filter when related ID matches GlobalID of orignal POLYGON feature.
    var relRecsFS = FeatureSetByName($datastore, "SEG_TBL", ["*"], false)
    var zoneGlobalID = $originalFeature.GlobalID
    var relRecordsFS = Filter(relRecsFS, 'REL_ZONE_ID = @zoneGlobalID')
}

// MAIN ------------------------------------------------------------------------------- #
// Iterate over the currently related table records, add all 'deletes' array.
var deletes = []
for (var relatedRecord in relRecordsFS) {
    Console(`ALREADY RELATED / TO DELETE: ${relatedRecord.SEG_ID} | ${relatedRecord.GlobalID}`)
    Push(deletes, {"globalID": relatedRecord.GlobalID})
}
// For every SEGMENT line, check if it intersects current ZONE. If so, add to 'adds' array.
var adds = []
if (editType != "DELETE") {  // If a feature is deleted, there won't be anything getting added. "newZone" alone isn't working?
    for (var seg in segmentsFS) {
        if (Intersects(newZone, seg)) {
            Console(`FOUND NEW INTERSECTION / TO ADD: ${seg.SEG_ID}`)
            Push(adds, {"attributes": {"REL_ZONE_ID": newZone.GlobalID, "SEG_ID": seg.SEG_ID} })
        }
    }
}

Console(`\nDELETE RECORDS:\n${deletes}\n\nADD RECORDS:\n${adds}`)
return {
    'edit': [{
        'className': 'SEG_TBL',  // This is the TABLE where the edits are occurring.
            'deletes': deletes,  // [{'globalID': XXX}, {'globalID': YYY}]  &amp;lt;-- 2 delete examples
            'adds': adds  // [{"attributes": {"REL_ZONE_ID": newZone.GlobalID, "SEG_ID": seg.SEG_ID}}, {"attr...] &amp;lt;-- 2 adds
    }]
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 04 Apr 2024 21:49:05 GMT</pubDate>
    <dc:creator>VinceE</dc:creator>
    <dc:date>2024-04-04T21:49:05Z</dc:date>
    <item>
      <title>Attribute Rule: Automatically update related table based on intersections?</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/attribute-rule-automatically-update-related-table/m-p/1406089#M1384</link>
      <description>&lt;P&gt;I have a polygon feature class related to a table via Relationship Class (one-to-many, GlobalID in polygon related to a GUID in the table, field called REL_ZONE_ID). I am updating this related table manually. The related table lists the segments that intersect the polygons (polygon labels shown here are a shorthand version of the full Globals, full version listed in the REL_ZONE_ID field). So basically it's a table that keeps track of the lines intersecting a given polygon.&lt;/P&gt;&lt;P&gt;I am moving between Pro 3.1 and 3.2, so I can't yet reliably use "FeatureSetByRelationshipClass".&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="The table shown is the related table, storing a record of each line that intersects a polygon." style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/100130iD81C4A8AAB5E2F1F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="VinceE_0-1712265135365.png" alt="The table shown is the related table, storing a record of each line that intersects a polygon." /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;The table shown is the related table, storing a record of each line that intersects a polygon.&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I would like to do this automatically with a Calculation Attribute Rule, at &lt;/SPAN&gt;&lt;STRONG&gt;INSERT&lt;/STRONG&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;STRONG&gt;UPDATE&lt;/STRONG&gt;&lt;SPAN&gt;, and &lt;/SPAN&gt;&lt;STRONG&gt;DELETE&lt;/STRONG&gt;&lt;SPAN&gt;. My approach:&lt;/SPAN&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Get a FeatureSet of the records that are already related to the original polygon.&lt;/LI&gt;&lt;LI&gt;Mark them for deletion by adding to a "delete" array.&lt;/LI&gt;&lt;LI&gt;Review&amp;nbsp;&lt;EM&gt;all&lt;/EM&gt;&amp;nbsp;line segments (time consuming?); if any intersect the newly updated polygon, append segment ID record to a new "adds" array.&lt;/LI&gt;&lt;LI&gt;Return an "edit" record to delete the originally related records, and add the newly intersected records.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;The below does this, but it's a hack--I needed to add in the if &lt;STRONG&gt;(editType != "DELETE")&amp;nbsp;&lt;/STRONG&gt;because otherwise when a polygon was deleted, the rule would remove the currently related records as expected, but then add the same records back in (except with a NULL related ID, since the polygon would have been deleted, and not have an ID anymore).&lt;/P&gt;&lt;P&gt;I thought I wouldn't need to do the&amp;nbsp;&lt;STRONG&gt;if DELETE&lt;/STRONG&gt;&amp;nbsp;because I thought the "Intersect()" was being called on the&amp;nbsp;&lt;STRONG&gt;$feature&lt;/STRONG&gt;, and not the &lt;STRONG&gt;$originalFeature&lt;/STRONG&gt;. So I assumed that if a feature was deleted, there couldn't be any intersections with the&amp;nbsp;&lt;STRONG&gt;$feature&lt;/STRONG&gt;, and therefore no records would be added to the "adds" array.&lt;/P&gt;&lt;P&gt;To reiterate, the code below does exactly what I want, I just can't do it without the special "DELETE" caveat.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;/*MANAGE RELATED SEGMENTS*/
//---------------------------------------------------------------------//
// INPUTS
var proVersion = 3.1
var editType = $editContext.editType 

// Get original/current polygon feature.
var zone = $originalFeature
var newZone = $feature

// Get all polylines in SEGMENTS feature class. Geometry required for 'Intersects()'.
var segmentsFS = FeatureSetByName($datastore, "SEGMENT", ["SEG_ID"], true)

if (proVersion &amp;gt;= 3.2) {
    // Get table records that are related to the current POLYGON feature.
    var relRecordsFS = FeatureSetByRelationshipClass(zone, "ZONE_SEG_RELATION", ["*"], false)
} else {
    // Get all records from the related table.
    // Filter when related ID matches GlobalID of orignal POLYGON feature.
    var relRecsFS = FeatureSetByName($datastore, "SEG_TBL", ["*"], false)
    var zoneGlobalID = $originalFeature.GlobalID
    var relRecordsFS = Filter(relRecsFS, 'REL_ZONE_ID = @zoneGlobalID')
}

// MAIN ------------------------------------------------------------------------------- #
// Iterate over the currently related table records, add all 'deletes' array.
var deletes = []
for (var relatedRecord in relRecordsFS) {
    Console(`ALREADY RELATED / TO DELETE: ${relatedRecord.SEG_ID} | ${relatedRecord.GlobalID}`)
    Push(deletes, {"globalID": relatedRecord.GlobalID})
}
// For every SEGMENT line, check if it intersects current ZONE. If so, add to 'adds' array.
var adds = []
if (editType != "DELETE") {  // If a feature is deleted, there won't be anything getting added. "newZone" alone isn't working?
    for (var seg in segmentsFS) {
        if (Intersects(newZone, seg)) {
            Console(`FOUND NEW INTERSECTION / TO ADD: ${seg.SEG_ID}`)
            Push(adds, {"attributes": {"REL_ZONE_ID": newZone.GlobalID, "SEG_ID": seg.SEG_ID} })
        }
    }
}

Console(`\nDELETE RECORDS:\n${deletes}\n\nADD RECORDS:\n${adds}`)
return {
    'edit': [{
        'className': 'SEG_TBL',  // This is the TABLE where the edits are occurring.
            'deletes': deletes,  // [{'globalID': XXX}, {'globalID': YYY}]  &amp;lt;-- 2 delete examples
            'adds': adds  // [{"attributes": {"REL_ZONE_ID": newZone.GlobalID, "SEG_ID": seg.SEG_ID}}, {"attr...] &amp;lt;-- 2 adds
    }]
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Apr 2024 21:49:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/attribute-rule-automatically-update-related-table/m-p/1406089#M1384</guid>
      <dc:creator>VinceE</dc:creator>
      <dc:date>2024-04-04T21:49:05Z</dc:date>
    </item>
  </channel>
</rss>

