<?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 M:N Relationship Class and Updating Intermediary Relationship Table via Attribute Rules in Data Management Questions</title>
    <link>https://community.esri.com/t5/data-management-questions/m-n-relationship-class-and-updating-intermediary/m-p/1360026#M44857</link>
    <description>&lt;P&gt;Hi folks,&lt;/P&gt;&lt;P&gt;Working on an attribute rule that populates a M:N relationship table between two polygon layers in an cases where they intersect. These are in an EGDB.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In this case the intermediary relationship table is RelationshipTable_AB. For some reason the M:N relationship class in EGDBs in Enterprise 10.9.1 is not updated automatically upon creation/update/deletion of intersecting layers and needs to be attributed to update the intermediary relationship class table manually.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Anytime a Project A or Project B polygon is created, it seems to generate a double entry in the relationship table - with one entry containing all of the populated fields in the intermediary table, and the second one having some null values. Rows 1-2 are generated when a new Project_A polygon is created, whereas Rows 3-4 are created when a new Project_B polygon is created. I only want row 1 to remain, and row 3 to remain.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2023-12-12 140922.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/88821iAC7809FCC6480026/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2023-12-12 140922.png" alt="Screenshot 2023-12-12 140922.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Is there logic or a function in Arcade that checks to see if a record exists in a table before inserting/updating the relationship table? Would an if/else statement be the way to go here?&lt;/P&gt;&lt;P&gt;This is a snippet of my attribute rule applied to Projects_A upon Insert and Update triggers:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;var fs = FeatureSetByName($datastore, "Project_B");
var fsInt = intersects(fs, $feature);
var payload = [];
var c = 0;
for (var proj in fsInt)
{
     payload[c++] = {"attributes": { "PROJECT_NAME": $feature.project_name, "PROJA_NUMBER": $feature.proja_proj_number, "PROJB_NUMBER": $feature.projb_proj_number} }
} 


return {
     "edit": [{"className": "RelationshipTable_AB",
                "adds": payload
              }]
} &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a snippet of my attribute rule applied to Projects_B upon Insert and Update triggers:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;var fs = FeatureSetByName($datastore, "Project_A", ["proja_proj_number"]);
var fsInt = intersects(fs, $feature);
var payload = [];
var c = 0;

for (var proj in fsInt) 
  {
    payload[c++] = {"attributes": {"PROJECT_NAME": $feature.projectname, "PROJA_NUMBER": proj.proja_proj_number,"PROJB_NUMBER":$feature.projb_proj_number}}
}
  
return {
    "edit": [{
        "className": "RelationshipTable_AB",
        "adds": payload
    }]
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been trying my hand with the below scripts, each to no avail &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;var fs = FeatureSetByName($datastore, "Project_A", ["proja_proj_number"]);
var fsInt = intersects(fs, $feature);
var payload = {};
var uniqueEntries = [];
var c = 0;

for (var proj in fsInt) {
    var projb_proj_num = $feature.projb_proj_number;
    var proja_proj_num = proj.proja_proj_number;

    // Check if projb_proj_number already exists in the payload
    if (!payload[projb_proj_number]) {   
        payload[projb_proj_number] = {
            "PROJECT_NAME": $feature.projectname,
            "PROJA_NUMBER": proja_proj_number,
            "PROJB_NUMBER": projb_proj_number
        };
        uniqueEntries.push(payload[projb_proj_number]);
    }
}

return {
    "edit": [{
        "className": "RelationshipTable_AB",
        "adds": uniqueEntries
    }]
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;// Query to retrieve existing records from RelationshipTable_AB table
var existingRecords = FeatureSetByName($datastore, " RelationshipTable_AB", ["project_name"]);
var fs = FeatureSetByName($datastore, "Project_A", ["proja_proj_number"]);
var fsInt = intersects(fs, $feature);
var payload = [];
var c = 0;

for (var proj in fsInt) {
    // Check if the project_name attribute is populated in existing records
    var projectNamePopulated = existingRecords.project_name != null &amp;amp;&amp;amp; record.project_name != '’;

    if (projectNamePopulated) {
        payload[c++] = {
            "attributes": {
                "PROJECT_NAME": $feature.project_name,
                "PROJA_PROJ_NUMBER": proj.proja_proj_number,
                "PROJB_PROJ_NUMBER": $feature.projb_proj_number
            }
        };
    } else {
        // If project_name is not populated in existing records, do not insert the record
        console.log("project_name attribute is not populated in existing records. Skipping record insertion.");
    }
}

return {
    "edit": [{
        "className": "RelationshipTable_AB",
        "adds": payload
    }]
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be very much appreciated, thanks so much.&lt;/P&gt;</description>
    <pubDate>Tue, 12 Dec 2023 22:23:04 GMT</pubDate>
    <dc:creator>Boomer1187</dc:creator>
    <dc:date>2023-12-12T22:23:04Z</dc:date>
    <item>
      <title>M:N Relationship Class and Updating Intermediary Relationship Table via Attribute Rules</title>
      <link>https://community.esri.com/t5/data-management-questions/m-n-relationship-class-and-updating-intermediary/m-p/1360026#M44857</link>
      <description>&lt;P&gt;Hi folks,&lt;/P&gt;&lt;P&gt;Working on an attribute rule that populates a M:N relationship table between two polygon layers in an cases where they intersect. These are in an EGDB.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In this case the intermediary relationship table is RelationshipTable_AB. For some reason the M:N relationship class in EGDBs in Enterprise 10.9.1 is not updated automatically upon creation/update/deletion of intersecting layers and needs to be attributed to update the intermediary relationship class table manually.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Anytime a Project A or Project B polygon is created, it seems to generate a double entry in the relationship table - with one entry containing all of the populated fields in the intermediary table, and the second one having some null values. Rows 1-2 are generated when a new Project_A polygon is created, whereas Rows 3-4 are created when a new Project_B polygon is created. I only want row 1 to remain, and row 3 to remain.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2023-12-12 140922.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/88821iAC7809FCC6480026/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2023-12-12 140922.png" alt="Screenshot 2023-12-12 140922.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Is there logic or a function in Arcade that checks to see if a record exists in a table before inserting/updating the relationship table? Would an if/else statement be the way to go here?&lt;/P&gt;&lt;P&gt;This is a snippet of my attribute rule applied to Projects_A upon Insert and Update triggers:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;var fs = FeatureSetByName($datastore, "Project_B");
var fsInt = intersects(fs, $feature);
var payload = [];
var c = 0;
for (var proj in fsInt)
{
     payload[c++] = {"attributes": { "PROJECT_NAME": $feature.project_name, "PROJA_NUMBER": $feature.proja_proj_number, "PROJB_NUMBER": $feature.projb_proj_number} }
} 


return {
     "edit": [{"className": "RelationshipTable_AB",
                "adds": payload
              }]
} &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a snippet of my attribute rule applied to Projects_B upon Insert and Update triggers:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;var fs = FeatureSetByName($datastore, "Project_A", ["proja_proj_number"]);
var fsInt = intersects(fs, $feature);
var payload = [];
var c = 0;

for (var proj in fsInt) 
  {
    payload[c++] = {"attributes": {"PROJECT_NAME": $feature.projectname, "PROJA_NUMBER": proj.proja_proj_number,"PROJB_NUMBER":$feature.projb_proj_number}}
}
  
return {
    "edit": [{
        "className": "RelationshipTable_AB",
        "adds": payload
    }]
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been trying my hand with the below scripts, each to no avail &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;var fs = FeatureSetByName($datastore, "Project_A", ["proja_proj_number"]);
var fsInt = intersects(fs, $feature);
var payload = {};
var uniqueEntries = [];
var c = 0;

for (var proj in fsInt) {
    var projb_proj_num = $feature.projb_proj_number;
    var proja_proj_num = proj.proja_proj_number;

    // Check if projb_proj_number already exists in the payload
    if (!payload[projb_proj_number]) {   
        payload[projb_proj_number] = {
            "PROJECT_NAME": $feature.projectname,
            "PROJA_NUMBER": proja_proj_number,
            "PROJB_NUMBER": projb_proj_number
        };
        uniqueEntries.push(payload[projb_proj_number]);
    }
}

return {
    "edit": [{
        "className": "RelationshipTable_AB",
        "adds": uniqueEntries
    }]
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;// Query to retrieve existing records from RelationshipTable_AB table
var existingRecords = FeatureSetByName($datastore, " RelationshipTable_AB", ["project_name"]);
var fs = FeatureSetByName($datastore, "Project_A", ["proja_proj_number"]);
var fsInt = intersects(fs, $feature);
var payload = [];
var c = 0;

for (var proj in fsInt) {
    // Check if the project_name attribute is populated in existing records
    var projectNamePopulated = existingRecords.project_name != null &amp;amp;&amp;amp; record.project_name != '’;

    if (projectNamePopulated) {
        payload[c++] = {
            "attributes": {
                "PROJECT_NAME": $feature.project_name,
                "PROJA_PROJ_NUMBER": proj.proja_proj_number,
                "PROJB_PROJ_NUMBER": $feature.projb_proj_number
            }
        };
    } else {
        // If project_name is not populated in existing records, do not insert the record
        console.log("project_name attribute is not populated in existing records. Skipping record insertion.");
    }
}

return {
    "edit": [{
        "className": "RelationshipTable_AB",
        "adds": payload
    }]
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be very much appreciated, thanks so much.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2023 22:23:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/data-management-questions/m-n-relationship-class-and-updating-intermediary/m-p/1360026#M44857</guid>
      <dc:creator>Boomer1187</dc:creator>
      <dc:date>2023-12-12T22:23:04Z</dc:date>
    </item>
  </channel>
</rss>

