Select to view content in your preferred language

Attribute Rule to Related Table

5316
9
02-04-2021 07:47 AM
LindseyStone
Frequent Contributor

I'm not good with attribute rule coding and needing a little help.  I'm trying to set up an attribute rule where it will take a field data from a layer and auto populate a field in the related table with the same data.  I have run across a few threads where you are populating the layer for the table but not this way.  Here is my example (I'm using a SDE SQL)

Hydrant Device Feature Class is Related to Hydrant Maintenance Inspection Table.  I want the Utility_AssetID field in Hydrant Device to auto-populate the Utility_AssetID field in the Hydrant Maintenance Inspection Table upon creation of the inspection.

Tags (1)
0 Kudos
9 Replies
JohannesLindner
MVP Frequent Contributor

If Utility_AssetID is NOT the field relating these tables (i.e. you have another field that is shared by both), you can do it this way:

// rule in Inspection table
// triggers: insert, update
// field: Utility_AssetID


// value of shared field
var key = $feature["SharedField"]

// search for related features in Hydrant Devices FC
var hydrant_devices = FeatureSetByName($datastore, "NameOfHydrantDevicesFC", ["SharedField", "Utility_AssetID"], false)
var filtered_devices = Filter(hydrant_devices, "SharedField = @key")

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

// related feature(s) found -> return Utility_AssetID of the first one
return First(filtered_devices)["Utility_AssetID"]

 

 

If Utility_AssetID IS the field relating your FC and table (i.e. it's the only field telling you to which hydrant an inspection belongs), you can't do this with an Attribute Rule (at least not that I know of).

What you can do in this case:

  • create a Relationship Class between the FC and the table
  • in ArcGIS Pro: select a hydrant
  • open the Attributes pane (Map/Edit --> Selection --> Attributes)
  • below your selected hydrant feature, you should see the name of the Relationship Class
  • right click on that, select "Add new to relationship"
  • ArcGIS creates a new entry in your inspection table, filling the AssetID with the value from the feature

Have a great day!
Johannes
LindseyStone
Frequent Contributor

Just to verify.  I'm really new to these type of things.  

So here is my breakdown of my relationships and fields.  This is all in a SDE database.

Water Device (this contains the hydrants) contains fields GLOBALID (globalID field) and hast_assetid (Utility assetID text field)

Hydrant Inspection table contains fields hyrantid (Guid field) and hast_assetid (Utility assetID text field)

The relationship between the two are Hydrant ID and Global ID with a 1 to many relationship.  I want the hast_assetid to in Water Device to be automatically copied to hast_assetid (Utility assetID text field) in Hydrant Inspection upon creation.

 

So with your above coding would this be right?  With this being in an SDE geodatabase shared out to a feature service for the datastore do I need to fill that out or do water.waterdevice or fill out the hydrant FC differently?

// rule in Inspection table
// triggers: insert, update
// field: Utility_AssetID


// value of shared field
var key = $feature["hydrantid"]

// search for related features in Hydrant Devices FC
var hydrant_devices = FeatureSetByName($datastore, "WaterDevice", ["GLOBALID", "hast_assetid"], false)
var filtered_devices = Filter(hydrant_devices, "GLOBALID= @key")

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

// related feature(s) found -> return Utility_AssetID of the first one
return First(filtered_devices)["hast_assetid"]

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

@LindseyStone wrote:

So with your above coding would this be right?  With this being in an SDE geodatabase shared out to a feature service for the datastore do I need to fill that out or do water.waterdevice or fill out the hydrant FC differently?


  • "WaterDevice" should be "DatabaseName.DataOwner.WaterDevice", the full name you get shown in the Catalog View.

  • $datastore is a global Arcade variable that references the server that hosts your service. You don't need to change it.

 


Have a great day!
Johannes
0 Kudos
LindseyStone
Frequent Contributor

I still can't get this to work.  I'm wondering if this has to do something with the fact that I'm editing to a service instead of directly to the SDE.  I have attached a screenshot of my what the attribute rule looks like via the published service (left) and what it looks like when I created it directly against the SDE (right).

hydrantID.JPG

0 Kudos
JimmyBowden
Frequent Contributor

You could use featuresetbyrelationshipname so you don't have to worry about what field to join on.

var relationship = FeatureSetByRelationshipName($feature, 'Device_Hydrant_To_Inspection', ['hast_assetid'], false);
return First(relationship).hast_assetid

 When I'm working on attribute rules it helps to run the code in a configured popup expression.  This will allow you to see what data is being returned.

JimmyBowden_0-1618510959330.png

 

0 Kudos
MitchJohnson
Regular Contributor

Is it just the two lines?  I get an error on the 2nd line either about "identifier expected" or "Unexpected null".  Enterprise geodatabase relationship class.  I have tried both one to many and one to one.  Do the origin and destination matter?

0 Kudos
JohannesLindner
MVP Frequent Contributor

When there are no related features, you call First() on an empty feature set, which will return null. You just have to check the size of the feature set before calling First() on it:

var relationship = FeatureSetByRelationshipName($feature, 'Device_Hydrant_To_Inspection', ['hast_assetid'], false);

// check size of FeatureSet, return null if empty
if(Count(relationship) == 0) { return null }
return First(relationship).hast_assetid

 


Have a great day!
Johannes
MitchJohnson
Regular Contributor

Looks like this did it.  Thanks Johannes!


Best,

Mitch

0 Kudos
AneteZvaigzneLV
Regular Contributor

I have a very similar, maybe even more simple question/issue. 

I have a parent point feature layer. Simple Relationship Class created with a related table. I want to ''feed'' the related table with a value from parent feature field named "STD". I tried an attribute rule, using Feature Set By name Arcade function but it ignores me. I wonder is it even possible. Basically when I create a record for the address point that is related by GlobalID-GUID, I would also like to populate another field. 

var relationship = FeatureSetByName($datastore, "Adresu_punkti2", ["STD"])

return relationship

 

Any help/comment/direction would be greatly appreciated.