Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Behaviours: How to make a field visible based on the value in field "Fix Version/s"?

Stephan Delhaye March 7, 2023

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:


def classification = getFieldByName('Classification')
def fixVersions = getFieldById(getFieldChanged())
 
def selectedOption = fixVersions.getValue() as String
def isFixVersionSelected = selectedOption == "Version_1"
 
classification.setHidden(!isFixVersionSelected)
classification.setRequired(isFixVersionSelected)

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

1 answer

1 accepted

0 votes
Answer accepted
Radek Dostál
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 7, 2023

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

Stephan Delhaye March 8, 2023

Hi Radek,

Thank you for your response.

I tried it out and adjusted the code to the below:


 

import com.atlassian.jira.project.version.Version

def classification = getFieldByName("Classification")
def fixVersions = getFieldById(getFieldChanged())
 
List<Version> fixVersionsValue = (List<Version>) fixVersions.getValue()

if (fixVersionsValue != null && !fixVersionsValue.isEmpty() && fixVersionsValue*.getName().contains("Version 1")) {
classification.setHidden(false)
classification.setRequired(true)
}
else {
    classification.setHidden(true)
    classification.setRequired(false)
}

This is however only partially working:
- The field is not hidden on the create screen (it should only appear if the desired Version is selected)
- Once the correct version is selected the field "Classification" becomes mandatory
- When removing the version again, the field "Classification" stays mandatory (although it should be not) and remains on the screen (see first point)
What am I doing wrong?
Kind Regards,
Stephan
Radek Dostál
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 8, 2023

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")
Since initializer is not attached to any specific field, so we need to tell the behaviour what we're after.
Once added there the initializer makes sure to do it's thing, and later on when somebody is modifying the field, the second script does it's thing instead.
Although, this is not how it should work, it is a workaround - normally, the script attached to the field should execute when the form loads. But, it doesn't in this case, more in the next point:
When removing the version again, the field "Classification" stays mandatory (although it should be not) and remains on the screen (see first point)
This is what I was hoping would work for you, because it behaves the same way for me as it does for you.
It seems that when fixVersions is empty (or being emptied), behaviours attached to the field are not picking up on it - thus not running any of the code.
When I have the debug turned on with description - the value is null at first (behaviour did not run at all), then, if I choose a version, it goes [Version X], if I add another, it goes [Version X, Version Y], if I remove Y, it goes [Version X], but now if I remove X, it stays Version X - the behaviour is not being triggered, but it should.
This seems like a bug in ScriptRunner to me. I couldn't find specifically one related to Fix Versions, other than an open one for Sprint which describes similar behaviour (https://productsupport.adaptavist.com/browse/SRJIRA-3883).
At this point I'd get in touch with Adaptavist support to confirm that this is a bug - sure is one from my point of view, but could be either fixable in ScriptRunner, or could be a missing something from underlying Jira.
Sorry - seems that this won't work reliably for Fix Version/s, unless this has been fixed in the most recent versions, I don't see a an open bug for it though.
Stephan Delhaye March 9, 2023

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

Suggest an answer

Log in or Sign up to answer