Require a field when another field has a certain value
I've got a custom field called Bug Reason, and one of the values in the Type bundle is Bug. What I want is when Type is set to Bug, Bug Reason is required; for any other value of Type, Bug Reason should be cleared. I can't quite get this to work properly in workflows. I'm using this to clear the value when the Type changes away from Bug:
More problematic, however, is the code to enforce the requirement of a reason when Type is Bug. Here's what I have now:
Any suggestions on how to properly set this up?
rule Clear bug reason if type is no longer bug
when issue.Type.changed && issue.Type != {Bug} {
if (issue.Bug reason != null) {
issue.Bug reason = null;
}
}
This seems to work, but I suspect certain edge cases could be better handled by something else.More problematic, however, is the code to enforce the requirement of a reason when Type is Bug. Here's what I have now:
rule Require bug reason if Type is Bug
when Type == {Bug} && isReported() {
Bug reason.required("Please supply a reason for the bug.");
}
The biggest problem I have with this is that if I change the Type on an existing issue to Bug, I get the alert about the Bug Reason field being required, but the Type is changed back to the previous value, presumably because YouTrack is trying to save it immediately. Thus, the only way the user can change the Type is to first change the Bug Reason field. Not only is this ugly, but as a next step, I'd like to ensure that the Bug Reason field is always clear if the type is not Bug.Any suggestions on how to properly set this up?
Please sign in to leave a comment.
I thinks using the following constructions should help:
- Type.becomes({Bug})
- Type.oldValue == {Bug}
Requirement.
rule Bug reason requirement when Type.becomes({Bug}) { Bug reason.required("Please supply a reason for the bug."); }Clear the bug reason.
rule Clear bug reason when Type.oldValue == {Bug} && Type.changed { if (Bug reason != null) { Bug reason = null; } }At this point, you can go ahead and change the Bug Reason field, but the Type does not auto-switch back to Bug.
I'm also seeing this behavior with creating issues. In that case, given a default Type other than Bug, trying to change the Type to Bug on the create-issue screen results in the same behavior seen when editing issues, namely, the value is switched back, effectively forcing the user to set the Bug Reason field before they're allowed to set the Type to Bug.
But it's planned to show the Command Window with the predefined new values, please vote for the http://youtrack.jetbrains.com/issue/JT-18414