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

  • Attribute rules are only supported on enterprise geodatabase datasets.

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

  • 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 ArcGIS Desktop.

Syntax

AddAttributeRule_management (in_table, name, type, {script_expression}, {is_editable}, {triggering_events}, {error_number}, {error_message}, {description}, subtype, {field}, {exclude_from_client_evaluation})
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. 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. These rules are applied based on the triggering events specified.
  • 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.
String
script_expression
(Optional)

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.

  • 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 attribute rule type and not applicable for the calculation rule type.

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 256 characters. This parameter is required for the constraint attribute rule type and not applicable for the calculation rule type.

String
description
(Optional)

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

String
subtype

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).

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

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)

Environments

This tool does not use any geoprocessing environments.

Licensing information

  • ArcGIS Desktop Basic: No
  • ArcGIS Desktop Standard: Yes
  • ArcGIS Desktop Advanced: Yes

Related topics