Hi Community,
I'm currently trying to setup a Scriptrunner Behaviours to achieve the following:
The field "Classification" should only appear on the screen if the value of the Fix Version/s field has a specific version (example "Version_1").
I found the article Behaviours Activities (adaptavist.com), which works fine when using only custom fields (like Select List (single choice)).
However, for system fields like Fix Version/s it does not work.
My current code looks like the below:
I suspect that since the System field "Fix Version/s" could contain multiple values, that I need to tweak it somehow (but I'm not sure how).
Can anyone gove me a hint on how to achieve this?
Kind Regards,
Stephan
You can debug this by adding:
def descriptionField = getFieldById("description")
descriptionField.setFormValue(fixVersions.getValue().toString() + "; " + fixVersions.getValue().class.getSimpleName()+"<"+fixVersions.getValue()[0].class.getSimpleName()+">")
And then you will see the actual value you are trying to compare to. This should return in description:
[Version_1]; ArrayList<VersionImpl>
This does show static type checking errors, because groovy does not know if "fixVersions.getValue()" is a list, an array, a string, or a number. But, since we know it's a list, we can afford to use an index (0) based getter and ignore the error in the editor.
The second thing is that you are casting that value "as String", so your actual value will be:
def selectedOption = fixVersions.getValue() as String
// [Version_1]
// String
So either, you would need to do a regular expression whether this String matches Version_1, such as
if (selectedOption.contains("Version_1") {
// do something
}
Which would work - strictly speaking, but it's generally suboptimal because [Version_1.1] would end up validating the behaviour eventually.
Ideally, I would say, let's work with the base types without casting them to String. Example:
import com.atlassian.jira.project.version.Version
def fixVersions = getFieldById(getFieldChanged())
List<Version> fixVersionsValue = (List<Version>) fixVersions.getValue()
if (fixVersionsValue != null && !fixVersionsValue.isEmpty() && fixVersionsValue*.getName().contains("Version_1")) {
getFieldById("description").setFormValue("It's a match!")
}
else {
//Do something else
getFieldById("description").setFormValue("Nope") //such as emptying our debug description
}
fixVersionsValue*.getName() is a groovy syntax - for each VersionImpl (which is the actual type in that list), it will call a .getName() method, which gets the version name. So this results in a new collection containing version names, ["Version_1", "Version_2"], but because this is a list, and not a String all by itself, we can then call .contains() on it to see if it contains the version (where contains will iterate over members in that list of strings whether any of them equals to the value).
Hi Radek,
Thank you for your response.
I tried it out and adjusted the code to the below:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Stephan,
The field is not hidden on the create screen (it should only appear if the desired Version is selected)
Add the same script as an Initializer with the exception of:
def fixVersions = getFieldById(getFieldChanged())
becomes
def fixVersions = getFieldById("fixVersions-textarea")
When removing the version again, the field "Classification" stays mandatory (although it should be not) and remains on the screen (see first point)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Radek,
Thank you very much for all your help on this one. :-)
I will go ahead and raise a ticket with Scriptrunner Support to check in this.
Have a good day. :-)
Kind Regards,
Stephan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.