A on-change rule defines the action that is performed when a change is applied to an issue. These rules replace the stateless rules that were used in older workflows.
Sample On-change Rule
This rule performs the following actions when a change is applied to an issue:
YouTrack checks for a change to the value in the Subsystem field.
If the value in the Subsystem changes, the issue is automatically assigned to the owner of the subsystem.
The components that define this on-change rule are as follows:
All scripts start with a list of
requirestatements. In this rule, we declare a local variable
entitiesand reference the entities module in the workflow API. This means that everything that is contained in this module can be accessed in this script with the
Each script exports one rule to the
exports.ruleproperty. The rule is declared with the
Issue.onChangemethod, which exports the script that follows the declaration as an on-change rule.
The body of the rule itself contains three properties, as follows:
An optional human-readable title. The title is only visible in the administrative interface.
A function that determines the conditions for executing the rule. If the guard condition is not met, the action specified in the rule is not applied to an issue.
The actions that should be applied to each issue. The action is declared as a function that accepts a context as an argument.
In this example, we check to see if the Subsystem field was set to a non-null value that has a non-null owner. If so, the rule sets the value of the Assignee field.
Determines which issue events trigger the on-change rule. When not specified, rules are triggered on issue update. This property is extended with the following parameters:
true, the rule is triggered on issue change.
true, the rule is triggered when an issue is deleted.
As the rule in this example is triggered when updates are applied, we don't need to specify values for this property.
The list of entities that are required for the rule to execute without errors. This property ensures that rules can be attached to projects safely.
In this example, the requirements ensure that both the Subsystem and Assignee fields store the correct types and are available in the project to which the rule is attached. If either field is absent, an error is shown in the Workflows list. The rule cannot be enabled until the required fields are attached.