Select to view content in your preferred language

Add Parent Geometry to A Related Record

852
16
Jump to solution
03-17-2025 05:53 PM
PhilipShutler
Occasional Contributor

Here is the scenario I am trying to build out (tried to keep it short - included pics to help out): 

I have two polygon feature classes and a nonspatial table.

The first polygon feature class is called "Grid_Test" - it's a tesselation feature of grid cells primarily used for symbology and tracking where crews are in the field.

Then, I have a related nonspatial table called "Work _Items" that stores the different work that is applied in a grid cell. This has a one-to-many relationship with the grid features. One grid cell to many work items. This has an attribute rule that grabs the gird_id from Grid_Test and is set to be the first rule that is run. This rule works perfectly and is shown below for reference.

var rel_guid = $feature.REL_GUID

// search for related features
var Grid_feats = FeatureSetByName($datastore, "Grid_Test", ["GLOBALID", "GRID_ID"], false)

var filtered_grid = Filter(Grid_feats, "GLOBALID= @rel_guid")

// no related features found
if(Count(filtered_grid) == 0) {
  return null
}

// related feature(s) found -> return grid id of the first one

else {
    var first_grid = First(filtered_grid);
    return first_grid["GRID_ID"];
}

 

Finally, I have a second polygon feature called "work_grid_related_polygons". This is where the issue occurs. The intent of this polygon feature class is to be one-to-one with work items and to create polygons as work items are created using the geometry of the parent grid cell. They will be stacked on top of one another as multiple work records are entered for a grid cell, but that's ok; I need this for reporting.  I intended to have an attribute rule take care of the transfer of attributes from work items and retrieve the geometry of the parent grid cell and assign it to each record that is added to the work_grid_related_polygons

Below is a graphic of my intended data model - Hopefully it helps.

datamodel_example.png

 

This is the arcade code I am running to try and accomplish creating the related polygons:

// related guid from parent in nonspatial table
var rel_guid = $feature.REL_GUID

// bring in grid feature class to get grid id
var Grid_feats = FeatureSetByName($datastore, "Grid_Test", ["GLOBALID", "GRID_ID"], false)

// use filter technique to find matching record
var filtered_grid_for_grid_id = Filter(Grid_feats, "GLOBALID= @rel_guid")

var grid_id_value = null;

// check for no related features found - this should not occur cause a record is being added as related feature but it is a check
if(Count(filtered_grid_for_grid_id) == 0) {
  return {
        "result": "No matching grid found for the given GRID_ID - first step."
    };
}


// related feature(s) found -> return grid id of the first one
else {
    var first_grid_for_id = First(filtered_grid_for_grid_id);
    var grid_id_value = first_grid_for_id["GRID_ID"];
}


// Access the grid features agaib to get geometry - see parameter set to true for geometries
var grid_features = FeatureSetByName($datastore, "Grid_Test", ['*'], true);

// Filter the grid features to find the one matching Grid ID
var filtered_grids = Filter(grid_features, "GRID_ID = @grid_id_value");


// Check if a matching grid was found - again this should not occur because these are related features
if (Count(filtered_grids) == 0) {
    return {
        "result": "No matching grid found for the given GRID_ID. - Second Step"
    };
}

// Retrieve the first grid feature and check
var first_grid = First(filtered_grids);
if (first_grid == null) {
    return {
        "result": "First grid feature is null."
    };
}

// Get the geometry of the first grid feature
var grid_geom = Geometry(first_grid);
if (grid_geom == null) {
    return {
        "result": "Grid geometry is null."
    };
}


// Ensure the geometry object is valid before proceeding
if (TypeOf(grid_geom) != "Polygon" && TypeOf(grid_geom) != "Polyline" && TypeOf(grid_geom) != "Point") {
    return {
        "result": "Invalid geometry type: " + TypeOf(grid_geom)
    };
}

// Assemble the edit that creates the related polygon using grid geometry and attributes from the visit
return {
    "edit": [{
        "className": "Grid_Test_RelatePolygons",
        "adds": [{
            "attributes": {
                "grid_id": grid_id_value // value to hold as an attribute transfer as well
            },
            "geometry": grid_geom 
        }]
    }]
};

 

This code runs but no records are produced in work_grid_related_polygons. The result that comes out it is "No matching grid found for the given GRID_ID - first step". This is odd because I used the console to do checks, and they returned valid values for attributes and geometry. Also I am able to get the grid_id populated in my nonspatial table with no issues using the same filter technique from the first attribute rule. I made sure to exclude from application evaluation in the settings.

I'm trying to figure out if i have left out some logic in my scenario to try and produce polygons from related features. I think the issue may be with the sequencing of when the transactions occur in the database for related records. 

What's odd is the grid_id comes over fine, and as the first rule in the order of operations, it should be there to be picked up and then used for the second rule. I know I did a little overkill by using using the relate guid to get the grid_id at first, but I did so to make sure I was getting the grid_id from the source. It feels like a timing issue when it comes to to how the database transfers a primary to foreign key in a relationship class.  

The inspiration came from @HusseinNasser2 examples using geometry in other attribute rules. Any help would be greatly appreciated.

0 Kudos
16 Replies
PhilipShutler
Occasional Contributor

Is it an issue with the sequencing of a transaction of a related features being processed in a geodatabase? The relationship class is in place to make it easy for field crews to add related records in field maps - when we use it on our enterprise gdb. I'm not sure if there is a workaround to build this same functionality in field maps without a relationship class.

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

Enable the update event, and  when $originalfeature.rel_guid == $feature.rel_guid return 

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

If you want to send me a repo case, I can dive in and see what is going on.

mmilller @ esri.com

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

Try this, enable the update trigger and then just check when the REL_GUID changes

 

if ($originalfeature.REL_GUID == $feature.REL_GUID)
  return
/// related guid from parent in nonspatial table
var rel_guid = $feature.REL_GUID;

// bring in grid feature class to get grid id
var grid_fs = FeatureSetByName($datastore, "Grid_Test", ["GLOBALID", "GRID_ID"], True);

// use filter technique to find matching record
var filtered_grid_fs = Filter(grid_fs, "GLOBALID = @REL_guid");

var first_grid_feat = First(filtered_grid_fs);
if (IsEmpty(first_grid_feat))
{
    return;
}
var grid_id_value = first_grid_feat["GRID_ID"];

// Filter the grid features to find the one matching Grid ID
var filtered_grids = Filter(grid_fs, "GRID_ID = @grid_id_value");

// Retrieve the first grid feature and check
var first_grid = First(filtered_grids);
if (IsEmpty(first_grid))
{
    return;
}


// Assemble the edit that creates the related polygon using grid geometry and attributes from the visit
return {
    "edit": [{
        "className": "Grid_Test_RelatePolygons",
        "adds": [{
            "attributes": {
                "grid_id": grid_id_value // value to hold as an attribute transfer as well
            },
            "geometry": Geometry(first_grid) 
        }]
    }]
};
0 Kudos
PhilipShutler
Occasional Contributor

Adding the $originalfeature is what did it! Thanks

My understanding is that the related guids need to equal one another to push the trigger. 

0 Kudos
AkiyoshiKawamura1
Emerging Contributor

Hi,

I have a similar issue.

I would like to create geometry for a child (related) record based on parent feature record.  Where is the script supposed to be run?  I have created a relationship class in Pro and published into AGOL. I would to achieve it in AGOL. Thanks.

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

Attribute Rules are not supported in ArcGIS Online.  Forms support Arcade, but that profile does not allow the ability to insert rows into other classes.

0 Kudos