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:

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; 
  } 
}
prettyPrint(); 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."); 
}
prettyPrint(); 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?
3 comments
Comment actions Permalink
Hi Bob,

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."); 
}
prettyPrint();
Clear the bug reason.
rule Clear bug reason 
 
when Type.oldValue == {Bug} && Type.changed { 
  if (Bug reason != null) { 
    Bug reason = null; 
  } 
}
prettyPrint();
0
Comment actions Permalink
I'm still seeing the same behavior switching issues to Type Bug. Basically:

  1. Pull up an existing issue to edit it. Pick one whose Type is something other than Bug.
  2. Change the Type to Bug. Immediately,
    • You get the error from the require() that the Bug Reason must be set.
    • The Type changes back to the previous value.

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.
0
Comment actions Permalink
So it is, in previous YouTrack versions, this field value had been preserved until the workflow requirement was matched. Due to architectural  restrictions (no more so-called 'long transactions') we decided to switch this behavior to the current.

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
0

Please sign in to leave a comment.