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 calculation and constraint attribute rules are triggered on specific editing events (insert, update, and delete operations). For update events, you can optionally specify a list of triggering fields. 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.
For example, 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 the 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;

Validate and evaluate relationship class rules

Validation or constraint attribute rules can be created and applied to enforce relationship class rules.

For example, you have a relationship class setup between the poles (origin) feature class and the inspections (destination) table. Each pole must have at least one or more inspections recorded.

You can create a one-to-one (1:1) or one-to-many (1:M) relationship class rule between this related data to accomplish this. Once the relationship class rule is created, you can use the following script to write a validation attribute rule that would be applied to the poles (origin) feature class to enforce that each pole must pass one or more inspections:

//A validation rule that ensures each pole passes one or more inspections.

var fsInspections = FeatureSetByRelationshipClass($feature, "poles");
if (count(fsInspections) == 0) return false;
return true;

After adding this validation attribute rule to the data, the evaluation process reviews the rules on the data to ensure that the rules are followed. The evaluation method used depends on the rule type and properties.

Validation rules are evaluated at a user-specified time using the Error Inspector view or the Evaluate Rules tool. During the evaluation, a validation rule creates error features that highlight features violating rules. The error features for these rules can be reviewed through the Error Inspector view.

Note:

You can author these rules by using either the FeatureSetByRelationshipName or FeatureSetByRelationshipClass Arcade function. FeatureSetByRelationshipClass allows you to evaluate rules through a service on the client side and will require ArcGIS Pro 3.2 or later and is explained in the Read related records section above.

To modify this attribute rule to enforce that a pole must have only one or no inspections, you can rewrite the script as follows:

//A validation rule that ensures each pole has one or fewer inspections.

var fsInspections = FeatureSetByRelationshipClass($feature, "poles");
if (count(fsInspections) > 1 ) return false;
return true;

You can also create and apply a constraint attribute rule to the data. Constraint rules specify permissible attribute configurations and general relationships on a feature. Unlike calculation rules, constraint rules are not used to populate attributes; they are used to ensure that specific conditions are met on a feature and can also prevent the edit if a condition is not met.

Constraint rules are evaluated during editing events (insert, update, and delete operations). For update events, you can optionally set a list of triggering fields. Constraint rules return a true or false result representing whether a rule is satisfied as follows:

  • True—The script expression provided is satisfied, and the feature will be created.
  • False—The script expression provided is not satisfied, and the feature will not be created.

When creating and applying constraint attribute rules, pay close attention to the order of the editing trigger.

For example, a constraint attribute rule is created and applied to the poles feature class to enforce that at least one inspection event must exist for each pole in the poles feature class. If the constraint attribute rule is set to trigger on an insert, such as when a new pole is added, the pole will fail to be created. In this scenario, creating the constraint attribute rule to trigger on an update instead of an insert is recommended.

Note:

The Ready to Use Rules button Ready To Use Rules provides access to a gallery of configurable checks that support the creation of constraint and validation rules. This is available with an ArcGIS Data Reviewer license.

Learn more about how to create attribute rules using ArcGIS Data Reviewer

Learn more about creating and managing attribute rules