Attribute rules and relationship classes

Attribute rules can be used to read and edit related records for datasets that participate in relationship classes in a geodatabase. Rules can be applied on feature classes and tables that participate in relationship classes, which include attachments and feature-linked annotation. Attribute rules can also be used to intercept edit events from updates made to the origin and destination classes in a relationship; that is adding, removing or deleting a related record. For example, an immediate calculation attribute rule can update an attribute field when a feature is added to a related feature class.

Triggering events with relationship classes

Immediate attribute rules are triggered on editing events such as insert, update, and delete operations. When adding a related record with ArcGIS Pro using the Add New To Relationship or Add Selected To Relationship context menu options, multiple editing events are triggered.

When using the Add New To Relationship context menu operation, the following occurs:

  • An empty row in the destination table of the relationship class is created.
    • Insert trigger on the destination row.
  • The foreign key is set on the destination row.
    • Update trigger on the destination row.
When you add an attribute rule on the destination table with a triggering event of update, the rule will capture the foreign key and query the associated row in the origin table.

The Add Selected To Relationship and Remove From Relationship context menu operations can also be used.

The following table lists the specific functions that can be distinguished on the update event by comparing $originalFeature.key and $feature.key.

FunctionEventArcGIS Arcade expression

Add a feature to a relationship

Update

Isempty($originalfeature.key) == true And isempty($feature.key) == false

Add selected features to a relationship

Update

Isempty($originalfeature.key) == true And isempty($feature.key) == false

Remove a feature from a relationship

Update

Isempty($originalfeature.key) == false And isempty($feature.key) == true

Delete operation performed on a related record

Delete

Read related records

When working with relationship classes, you can read related records using Arcade functions that take input features and return the related records.

  • FeatureSetByRelationshipName—Running is limited to server context. When using this function for attribute rules, ensure that the Exclude from application evaluation option is set to true.
  • FeatureSetByRelationshipClass—Supports running on both server and client side. Rules that use this function can be run locally in the application (for example, ArcGIS Pro and ArcGIS Maps SDKs for Native Apps) before the edit is sent to the data source.

You can also read related records using the FeatureSetByName Arcade function and Filter on the foreign key.

Read related records for a relationship class using the FeatureSetByName Arcade function.

A calculation rule is created on the InspectionCount field in the pole feature class that participates in a one-to-many relationship class with the inspection table. The FeatureSetByName function can be used to read the inspection class directly and retrieve the related records on update. Using this Arcade function requires knowledge of the relationship class field. For this relationship class, the origin primary key is the globalID field of the pole feature class and the origin foreign key is the poleguid of the inspection standalone table.

//A calculation rule that returns the count of a pole inspection records.
//When a pole feature is updated, the calculation rule reads all its related inspections records from the comments field and returns the total inspection count for that feature. 

Var fs = FeatureSetByName($datastore, “Inspection”, [“comments”], false)
Var poleGuid = $feature.poleguid 
Var fsinspected = Filter(fs, “POLEGUID= @poleguid”);
Return count(fsinspected)

Read a related record for a relationship class using the FeatureSetByRelationShipName Arcade function.

The same script to read related records for a relationship class can be rewritten with the FeatureSetRelationshipName function. Using this function, you don’t have to know the foreign key, only the relationship name, pole_inspection.

Caution:

When using this function to create an attribute rule, ensure that the Exclude from application evaluation option is set to true.

//A calculation rule that returns the count of a pole inspection records.
//When a pole feature is updated, the calculation rule reads all its related inspections records from the comments field and returns the total inspection count for that feature.

Var fsinspected = FeatureSetByRelationshipName($feature, “pole_inspection”, [“comments”], false)
Return count(fsinspected)

Read a related record for a relationship class using the FeatureSetByRelationshipClass Arcade function.

The same script to read related records from a relationship class can be rewritten with the FeatureSetByRelationshipClass function. Using this function, you don’t have to know the foreign key, only the relationship name, pole_inspection. This function supports execution for both server and client side, therefore attribute rules that use this function can be executed locally in the application.

//A calculation rule that returns the count of a pole inspection records.
//When a pole feature is updated, the calculation rule reads all its related inspections records from the comments field and returns the total inspection count for that feature.

Var fsinspected = FeatureSetByRelationshipClass ($feature, “pole_inspection”, [“comments”], false)
Return count(fsinspected)

Create, update, and delete related records

Related records from relationship classes can also be edited using calculation rules based on the dictionary return.

Here is an example of creating a calculation rule that creates a related record. When a pole comment field is updated by the user, the edit dictionary keyword is used to create an inspection record. Similarly, you can update or delete related records. To learn more, see Attribute rule dictionary keywords.

Add a new related record for a relationship class on an update edit.

A calculation rule is used to create a new related record when a pole comment field is updated by the user. In this expression the edit dictionary keyword is used to create a new inspection record if the comments field changes. Similarly, you can update or delete related records. Refer to Attribute rule dictionary keywords for more information.

//A calculation rule that triggers on an update event when the pole comment field is edited. 
//The expression first checks if the comment field changed and creates a new related record.

If ($originalfeature.comment != $feature.comment)
{ 
    Return {
                 “edit”: [{
   “className”: “inspection”,
   “adds”: [{ “attributes”: { “comments”: $feature.comment } }] 
}] 

}
Return;