Skip To Content

Add Attribute Rule

Summary

Adds an attribute rule to a dataset.

Attribute rules are user-defined rules that can be added to a dataset to enhance the editing experience and help enforce data integrity. These rules can be used to populate attribute values or constrain permissible feature configurations, and are enforced during feature editing. If a rule is violated when editing a feature, an error message will be returned.

Learn more about rules.

Learn more about the Arcade scripting language.

Usage

  • The dataset must reside in an enterprise geodatabase.

  • Validation and batch calculation rules are only supported for data with branch versioning enabled.

  • The dataset must have a GlobalID field.

  • Adding an attribute rule requires an exclusive lock on the dataset. You must close any active connections to the dataset, which may include stopping any services.

  • The expression used to define an attribute rule is limited to the functionality available using the Arcade language.

  • You can reference a database sequence created by the Create Database Sequence tool in the Script Expression parameter by using the NextSequenceValue Arcade data function.

  • To transfer existing attribute rules to another dataset, you can use the Export Attribute Rules tool and Import Attribute Rules tool instead of running this tool multiple times.

  • Note:

    Once you have added attribute rules to a dataset, the minimum client version for the dataset is ArcGIS Pro 2.1. This means that the dataset will no longer be available for use in ArcMap.

Syntax

AddAttributeRule_management (in_table, name, type, script_expression, {is_editable}, {triggering_events}, {error_number}, {error_message}, {description}, {subtype}, {field}, {exclude_from_client_evaluation}, {batch}, {severity}, {tags})
ParameterExplanationData Type
in_table

The table or feature class that will have the new rule applied.

Table View
name

A unique name for the new rule.

String
type

The type of attribute rule to add.

  • CALCULATIONAutomatically populate attribute values for features when another attribute is set on a feature. These rules are applied based on the triggering events specified. Long running calculations can be set to run in batch mode and will be evaluated at a user defined time.When adding multiple calculation rules, the order in which the rules are added is important if there are circular dependencies. For example, Rule A calculates Field1 is equal to the value of $feature.Field2 + $feature.Field3, and Rule B calculates Field4 is equal to $feature.Field1 + $feature.Field5, the results of the calculation may be very different depending on which order the rules are added.
  • CONSTRAINTSpecify permissible attribute configurations on a feature. When the constraint rule is violated, an error is generated and the feature is not stored. For example, if the value of Field A must be less than the sum of Field B and Field C, an error will be generated when that constraint is violated.
  • VALIDATIONChecked against existing features with a batch validation process. Rules are evaluated at a user defined time. When a rule is violated, an error feature is created. The type of rule can only be used for data that has been set up for branch versioning.
String
script_expression

An Arcade expression that defines the rule.

Calculator Expression
is_editable
(Optional)

Attribute rules can be configured to either block or allow editors to edit the attribute values of the field being calculated. This parameter is only applicable for the calculation attribute rule type.

  • EDITABLEAllow editors to edit the attribute value. This is the default.
  • NONEDITABLEEditors will not be able to edit the attribute value.
Boolean
triggering_events
[triggering_events,...]
(Optional)

The editing events that will trigger the attribute rule to take effect. Valid for calculation and constraint rule types only. At least one triggering event must be provided for calculation rules that have the Batch parameter set to false. Triggering events are not applicable for calculation rules that have the Batch parameter set to true.

  • INSERTThe rule will be applied when a new feature is added.
  • UPDATEThe rule will be applied when a feature has been updated.
  • DELETEThe rule will be applied when a feature has been deleted.
String
error_number
(Optional)

Provide an error number to return when this rule is violated. This value is not required to be unique, so you may have the same custom error number returned for multiple rules.

This parameter is required for the constraint and validation rules; it is optional for calculation rules.

Long
error_message
(Optional)

Provide an error message to return when this rule is violated. It is recommended to use a descriptive message that may help the editor understand the violation when it occurs. The message is limited to 2000 characters.

This parameter is required for the constraint and validation rules; it is optional for calculation rules.

String
description
(Optional)

A description of the new attribute rule. The description is limited to 256 characters.

String
subtype
(Optional)

If the dataset has subtypes, specify the subtype that the rule will be applied to.

String
field
(Optional)

The name of an existing field that the rule will be applied to. This parameter is required for the calculation attribute rule type and not applicable for the constraint rule type.

String
exclude_from_client_evaluation
(Optional)

This allows the client to choose whether to evaluate the rule prior to applying edits. Since not all clients may have the capability to run all of the rules that are available, you can choose to flag a rule as meant for simple clients only. For example, some rules may refer to data that has not been made available to all clients (reasons can include the data is offline, size, security), or some rules may depend on the user or context (that is, a lightweight field update in Collector for ArcGIS may not execute some rule that requires additional user input or knowledge. However, a different client like ArcGIS Pro could support it). This parameter is not applicable for validation rules or calculation rules if the batch parameter is true.

  • EXCLUDEThe rule should be excluded from client evaluation.
  • INCLUDEThe rule should be executed for all clients. This is the default.
Note:

In ArcGIS Pro 2.3 and later, this parameter is labeled in the tool dialog as Server only.

Boolean
batch
(Optional)

Indicates whether the rule is executed in batch mode.

  • BATCHThe rule evaluation will be executed at a later time by executing validate.
  • NOT_BATCHTriggering events are used to determine when the rule is evaluated for insert, update, or delete operations. This is the default.

Calculation rules can be set as true or false. Validation rules are always true for this property and constraint rules are always false.

Batch rules are only supported for data with branch versioning enabled.

Boolean
severity
(Optional)

Define the severity of the error.

A value within the range of 1-5 can be chosen to define the severity of the rule. A value of 1 is high, being the most severe and value of 5 is low, being the least severe. For example, you could decide to provide a low severity for a specific attribute rule and ignore the error during data production workflows or set a high severity where the error would need to be fixed for accuracy of data collected.

This property is only applicable to validation rules.

Long
tags
(Optional)

A set of tags to identify the rule (searchable and indexable) as a way to map to a functional requirement in a data model. To enter multiple tags, use a semi-colon delimiter. For example: Tag1;Tag2;Tag3.

String

Derived Output

NameExplanationData Type
out_table

The updated input table with an attribute rule added.

Table View

Code sample

AddAttributeRule example 1 (Python window)

Add a calculation rule that calculates the labeltext field of GasPipes feature class.

'''****************************************************************************
Name:        AddAttributeRule_example1.py
Description: This script adds a calcualtion rule to a feature class
Created by:  Esri
****************************************************************************'''

# Import required modules        
import arcpy

# Set local variables
in_table = "C:\\MyProject\\sdeConn.sde\\progdb.user1.GasPipes"
name = "calculateRuleLabel"
script_expression = 'if ($feature.material == 5) {return "Plastic"} else {return "Other"}'
triggering_events = "INSERT;UPDATE"
description = "Populate label text"
subtype = "Coated Steel"
field = "labeltext"

# Run the AddAttributeRule tool
arcpy.AddAttributeRule_management(in_table, name, "CALCULATION", script_expression, "EDITABLE", triggering_events, "", "", description, subtype, field)
AddAttributeRule example 2 (Python window)

Add a constraint rule that limits the operating pressure attribute of the GasPipes feature class to 300.

'''****************************************************************************
Name:        AddAttributeRule_example2.py
Description: This script adds a constraint rule to a feature class
Created by:  Esri
****************************************************************************'''

# Import required modules        
import arcpy

# Set local variables
in_table = "C:\\MyProject\\sdeConn.sde\\progdb.user1.GasPipes"
name = "constraintRuleOP"
script_expression = '$feature.OPERATINGPRESSURE < 300'
triggering_events = "INSERT;UPDATE"
description = "Operating pressure must be less than 300"
subtype = "ALL"
error_number = 2001
error_message = "Invalid operating pressure. Must be less than 300."

# Run the AddAttributeRule tool
arcpy.AddAttributeRule_management(in_table, name, "CONSTRAINT", script_expression, "EDITABLE", triggering_events, error_number, error_message, description, subtype)
AddAttributeRule example 3 (Python window)

Add a validation rule to the GasPipes feature class to flag any existing features with an operating pressure greater than 300.

'''****************************************************************************
Name:        AddAttributeRule_example3.py
Description: This script adds a validation rule to a feature class
Created by:  Esri
****************************************************************************'''

# Import required modules        
import arcpy

# Set local variables
in_table = "C:\\MyProject\\sdeConn.sde\\progdb.user1.GasPipes"
name = "validationRuleMaxOP"
rule_type = "VALIDATION"
script_expression = "$feature.OPERATINGPRESSURE < 300"
error_number = 3001
error_message = "Maximum operating pressure exceeded"
description = "Validation rule: max operating pressure value"
subtype = "Coated Steel"
batch = "BATCH"
severity = 3
tags = "OP;MAXOP"

# Run the AddAttributeRule tool
arcpy.AddAttributeRule_management(in_table, name, rule_type, 
                                  script_expression, "", "", 
                                  error_number, error_message, 
                                  description, subtype, "", "", 
                                  batch, severity, tags)

Environments

This tool does not use any geoprocessing environments.

Licensing information

  • Basic: No
  • Standard: Yes
  • Advanced: Yes

Related topics