Automatically populating related table field with parent table data?

1399
3
05-09-2022 03:14 PM
NoahWasserman
New Contributor III

I have a point layer with a SiteID field linked to a related table with SiteKey (similar setup to the ESRI Hydrant Inspection tutorial).  For each point there are multiple rows in the related table ("1:M"; e.g. hydrants and inspection records). I can't figure out how to use FeatureSetByRelationshipName (or another function?) to simply pull a number from the parent point layer into each inspection record.  I have a constant in the point attribute table (different for each point) that I need to use in an expression when recording new records in the related table. I keep getting the objectID along with the value I want which won't populate an integer field.

0 Kudos
3 Replies
JohannesLindner
MVP Frequent Contributor

You're halfway there!

The FeatureSetBy*() functions return feature sets, collection of features. You have to select 1 of those features (often the first one) and return 1 of its attributes.

 

// load all related features
var related_features = FeatureSetByRelationshipName($feature, "SepticLocations", ["inspect_freq"], false)

// select one of those features, here we just grab the first one
var related_feature = First(related_features)

// if there are no related features, related_feature will be null and we will
// get an error down the line, so we need to check that and return early
if(related_feature == null) {
    return null  // if there are no related features, we return a default value (null in this case)
}

// return an attribute
return related_feature.inspect_freq

Have a great day!
Johannes
RenM000
New Contributor II

Reading through your code, I can't tell what of my own Globals to fill in. 

simplified, I have a layer called Plants. Then I have a related table called Plant Inspections. I am trying to calculate the Site Name (and other fields) for my related table by what is already in the layer, as OP was. 

The green font (Septic Locations) came from where? Is that the Layer name? And if so, is the [inspect_freq] the equivalent of Site Name? Would this site name be from under the layer or from under the table? 

Thank you. 

0 Kudos
JohannesLindner
MVP Frequent Contributor

This code uses FeatureSetByRelationshipName(), which returns the related features based on a relationship class.

SepticLocations is the name of the relationship class, inspect_freq is the name of a field in the related table.

If you haven't built a relationship class, you can load the related table with FeatureSetByName() or one of the other FeatureSetBy*() functions and then Filter() it.


Have a great day!
Johannes
0 Kudos