<?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 for maintaining many-to-many relationships in Attribute Rules Questions</title>
    <link>https://community.esri.com/t5/attribute-rules-questions/attribute-rule-for-maintaining-many-to-many/m-p/1691689#M1926</link>
    <description>&lt;P&gt;I have two polygon feature classes, Municipality and District. There is a many-to-many relationship between the two, and therefore an intermediate table called&amp;nbsp;Municipality_District_Rel. The relationships are automatically handled with attribute rules, based on a municipality must overlap a district with 80% or more for them to be related. And if a municipality cover less than 80% percent, the relationship has to be deleted.&lt;/P&gt;&lt;P&gt;The Arcade expression is:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Arrays to store edits
var edits = [];
var adds = [];
var deletes = [];

// Get all District features
var allDistricts = FeatureSetByName($datastore, "District");

// Find District features that intersect this Municipality feature
var intersectingDistricts = Intersects(allDistricts, $feature);

// Determine which District features that this Municipality overlaps with 80% or more
var overlappingDistrictGlobalIds = [];
for (var district in intersectingDistricts) {
    var districtGeometry = Geometry(district);
    var districtArea = Area(districtGeometry);
    var intersectionGeometry = Intersection(Geometry($feature), districtGeometry);
    var intersectionArea = Area(intersectionGeometry);
    if ((intersectionArea / districtArea) &amp;gt;= 0.8) {
        // This district overlaps, so store the GlobalID for it
        Push(overlappingDistrictGlobalIds, district.GlobalID);
    }
}

// Determine which relationships to add (new District features that overlaps with at least 80%)
var relatedDistricts = FeatureSetByRelationshipName($feature, "Municipality_District_Rel", ["GlobalID"]);
for (var index in overlappingDistrictGlobalIds) {
    var districtGlobalId = overlappingDistrictGlobalIds[index];
    Console(districtGlobalId);
    var relatedDistrict = First(Filter(relatedDistricts, "GlobalID = '" + districtGlobalId + "'"));
    if (relatedDistrict == null) {
        // A relationship didn't exist, so add it
        Push(adds, {
            'attributes': {
                'Municipality_GlobalID': $feature.GlobalID,
                'District_GlobalID': districtGlobalId
            }
        });
    }
}

// Get existing relationships between Municipality and District features
var allRelationships = FeatureSetByName($datastore, "Municipality_District_Rel", ["*"]);
var relationships = Filter(allRelationships, "Municipality_GlobalID = '" + $feature.GlobalID + "'");

// Determine which relationships to delete (no longer overlapping with at least 80%)
for (var relationship in relationships) {
	if (!Includes(overlappingDistrictGlobalIds, relationship.District_GlobalID)) {
		// No longer overlapping enough, so delete the relationship
		Push(deletes, {'GlobalID': relationship.GlobalID});
	}
}

// Return edits dictionary if there are changes to the relatio
if (Count(adds) &amp;gt; 0 || Count(deletes) &amp;gt; 0) {
    Push(edits, {
        'className': "Municipality_District_Rel",
        'adds': adds,
        'deletes': deletes
    });
    return {'edit': edits};
}&lt;/LI-CODE&gt;&lt;P&gt;When I click Analyze I get:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Skærmbillede 2026-03-20 133343.png" style="width: 688px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/150077i85F25D9BC2A65B65/image-size/large?v=v2&amp;amp;px=999" role="button" title="Skærmbillede 2026-03-20 133343.png" alt="Skærmbillede 2026-03-20 133343.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;But when I try to save the attribute rule I get this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Skærmbillede 2026-03-20 133514.png" style="width: 457px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/150078i68FC83ED3FBA7636/image-size/large?v=v2&amp;amp;px=999" role="button" title="Skærmbillede 2026-03-20 133514.png" alt="Skærmbillede 2026-03-20 133514.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;What am I doing wrong?&lt;/P&gt;</description>
    <pubDate>Fri, 20 Mar 2026 12:39:54 GMT</pubDate>
    <dc:creator>CarstenAndersson</dc:creator>
    <dc:date>2026-03-20T12:39:54Z</dc:date>
    <item>
      <title>Attribute rule for maintaining many-to-many relationships</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/attribute-rule-for-maintaining-many-to-many/m-p/1691689#M1926</link>
      <description>&lt;P&gt;I have two polygon feature classes, Municipality and District. There is a many-to-many relationship between the two, and therefore an intermediate table called&amp;nbsp;Municipality_District_Rel. The relationships are automatically handled with attribute rules, based on a municipality must overlap a district with 80% or more for them to be related. And if a municipality cover less than 80% percent, the relationship has to be deleted.&lt;/P&gt;&lt;P&gt;The Arcade expression is:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Arrays to store edits
var edits = [];
var adds = [];
var deletes = [];

// Get all District features
var allDistricts = FeatureSetByName($datastore, "District");

// Find District features that intersect this Municipality feature
var intersectingDistricts = Intersects(allDistricts, $feature);

// Determine which District features that this Municipality overlaps with 80% or more
var overlappingDistrictGlobalIds = [];
for (var district in intersectingDistricts) {
    var districtGeometry = Geometry(district);
    var districtArea = Area(districtGeometry);
    var intersectionGeometry = Intersection(Geometry($feature), districtGeometry);
    var intersectionArea = Area(intersectionGeometry);
    if ((intersectionArea / districtArea) &amp;gt;= 0.8) {
        // This district overlaps, so store the GlobalID for it
        Push(overlappingDistrictGlobalIds, district.GlobalID);
    }
}

// Determine which relationships to add (new District features that overlaps with at least 80%)
var relatedDistricts = FeatureSetByRelationshipName($feature, "Municipality_District_Rel", ["GlobalID"]);
for (var index in overlappingDistrictGlobalIds) {
    var districtGlobalId = overlappingDistrictGlobalIds[index];
    Console(districtGlobalId);
    var relatedDistrict = First(Filter(relatedDistricts, "GlobalID = '" + districtGlobalId + "'"));
    if (relatedDistrict == null) {
        // A relationship didn't exist, so add it
        Push(adds, {
            'attributes': {
                'Municipality_GlobalID': $feature.GlobalID,
                'District_GlobalID': districtGlobalId
            }
        });
    }
}

// Get existing relationships between Municipality and District features
var allRelationships = FeatureSetByName($datastore, "Municipality_District_Rel", ["*"]);
var relationships = Filter(allRelationships, "Municipality_GlobalID = '" + $feature.GlobalID + "'");

// Determine which relationships to delete (no longer overlapping with at least 80%)
for (var relationship in relationships) {
	if (!Includes(overlappingDistrictGlobalIds, relationship.District_GlobalID)) {
		// No longer overlapping enough, so delete the relationship
		Push(deletes, {'GlobalID': relationship.GlobalID});
	}
}

// Return edits dictionary if there are changes to the relatio
if (Count(adds) &amp;gt; 0 || Count(deletes) &amp;gt; 0) {
    Push(edits, {
        'className': "Municipality_District_Rel",
        'adds': adds,
        'deletes': deletes
    });
    return {'edit': edits};
}&lt;/LI-CODE&gt;&lt;P&gt;When I click Analyze I get:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Skærmbillede 2026-03-20 133343.png" style="width: 688px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/150077i85F25D9BC2A65B65/image-size/large?v=v2&amp;amp;px=999" role="button" title="Skærmbillede 2026-03-20 133343.png" alt="Skærmbillede 2026-03-20 133343.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;But when I try to save the attribute rule I get this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Skærmbillede 2026-03-20 133514.png" style="width: 457px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/150078i68FC83ED3FBA7636/image-size/large?v=v2&amp;amp;px=999" role="button" title="Skærmbillede 2026-03-20 133514.png" alt="Skærmbillede 2026-03-20 133514.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;What am I doing wrong?&lt;/P&gt;</description>
      <pubDate>Fri, 20 Mar 2026 12:39:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/attribute-rule-for-maintaining-many-to-many/m-p/1691689#M1926</guid>
      <dc:creator>CarstenAndersson</dc:creator>
      <dc:date>2026-03-20T12:39:54Z</dc:date>
    </item>
    <item>
      <title>Re: Attribute rule for maintaining many-to-many relationships</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/attribute-rule-for-maintaining-many-to-many/m-p/1691690#M1927</link>
      <description>&lt;P&gt;Try this, my only guess is that adds or deletes is an empty array and maybe that is causing an issue&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Arrays to store edits
var edits = [];
var adds = [];
var deletes = [];

// Get all District features
var allDistricts = FeatureSetByName($datastore, "District");

// Find District features that intersect this Municipality feature
var intersectingDistricts = Intersects(allDistricts, $feature);

// Determine which District features that this Municipality overlaps with 80% or more
var overlappingDistrictGlobalIds = [];
for (var district in intersectingDistricts) {
    var districtGeometry = Geometry(district);
    var districtArea = Area(districtGeometry);
    var intersectionGeometry = Intersection(Geometry($feature), districtGeometry);
    var intersectionArea = Area(intersectionGeometry);
    if ((intersectionArea / districtArea) &amp;gt;= 0.8) {
        // This district overlaps, so store the GlobalID for it
        Push(overlappingDistrictGlobalIds, district.GlobalID);
    }
}

// Determine which relationships to add (new District features that overlaps with at least 80%)
var relatedDistricts = FeatureSetByRelationshipName($feature, "Municipality_District_Rel", ["GlobalID"]);
for (var index in overlappingDistrictGlobalIds) {
    var districtGlobalId = overlappingDistrictGlobalIds[index];
    Console(districtGlobalId);
    var relatedDistrict = First(Filter(relatedDistricts, "GlobalID = @districtGlobalId"));
    if (relatedDistrict == null) {
        // A relationship didn't exist, so add it
        Push(adds, {
            'attributes': {
                'Municipality_GlobalID': $feature.GlobalID,
                'District_GlobalID': districtGlobalId
            }
        });
    }
}

// Get existing relationships between Municipality and District features
var allRelationships = FeatureSetByName($datastore, "Municipality_District_Rel", ["*"]);
var relationships = Filter(allRelationships, "Municipality_GlobalID = '" + $feature.GlobalID + "'");

// Determine which relationships to delete (no longer overlapping with at least 80%)
for (var relationship in relationships) {
	if (!Includes(overlappingDistrictGlobalIds, relationship.District_GlobalID)) {
		// No longer overlapping enough, so delete the relationship
		Push(deletes, {'GlobalID': relationship.GlobalID});
	}
}

// Return edits dictionary if there are changes to the relatio
if (Count(adds) == 0 &amp;amp;&amp;amp; Count(deletes) == 0) {
    return;
}
var edit_payload =  {
    'className': "Municipality_District_Rel",
}
if (Count(adds)){
    edit_payload['adds'] = adds
}
if (Count(deletes)){
    edit_payload['deletes'] = deletes
}

Push(edits, edit_payload);
return {
    'edit': edits
};&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 20 Mar 2026 12:55:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/attribute-rule-for-maintaining-many-to-many/m-p/1691690#M1927</guid>
      <dc:creator>MikeMillerGIS</dc:creator>
      <dc:date>2026-03-20T12:55:52Z</dc:date>
    </item>
    <item>
      <title>Re: Attribute rule for maintaining many-to-many relationships</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/attribute-rule-for-maintaining-many-to-many/m-p/1691870#M1928</link>
      <description>&lt;P&gt;Thanks Mike,&lt;/P&gt;&lt;P&gt;but unfortunately I get the same error. My initial though was a version issue. I am using ArcGIS Pro 3.3, but I get the same error message on ArcGIS 3.5.&lt;/P&gt;&lt;P&gt;The line that causes the error is:&amp;nbsp;var allRelationships = FeatureSetByName($datastore, "Municipality_District_Rel", ["*"]);&lt;/P&gt;&lt;P&gt;But, it's the only way I can think of to delete the existing relationships.&lt;/P&gt;&lt;P&gt;Could this be a bug?&lt;/P&gt;</description>
      <pubDate>Mon, 23 Mar 2026 05:34:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/attribute-rule-for-maintaining-many-to-many/m-p/1691870#M1928</guid>
      <dc:creator>CarstenAndersson</dc:creator>
      <dc:date>2026-03-23T05:34:26Z</dc:date>
    </item>
    <item>
      <title>Re: Attribute rule for maintaining many-to-many relationships</title>
      <link>https://community.esri.com/t5/attribute-rules-questions/attribute-rule-for-maintaining-many-to-many/m-p/1692194#M1929</link>
      <description>&lt;P&gt;Could be, I never tried to work with a M:N in arcade.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Mar 2026 13:50:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/attribute-rules-questions/attribute-rule-for-maintaining-many-to-many/m-p/1692194#M1929</guid>
      <dc:creator>MikeMillerGIS</dc:creator>
      <dc:date>2026-03-24T13:50:01Z</dc:date>
    </item>
  </channel>
</rss>

