Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to hide custom fields when Version Picker is empty?

Diana
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.
October 31, 2022

I've used this trick for Behaviours so many times without fail, but for some reason I can't get it to work for a custom field Version Picker (single select).

If any option is selected for Version Picker, another custom field "Text Multi-line" appears.

But if Version Picker is empty, I want the text field to hide. I've tried variations:

def field = getFieldByName("Version Picker")
def fieldValue = field.getValue()
def text = getFieldByName("Text Multi-line")

if (fieldValue != null) {
text.setHidden(false)
}
if (!fieldValue) {
text.setHidden(false)
}
def fieldValue = field.getValue() as String

if (fieldValue != "Unknown") {
text.setHidden(false)
}

But none of them work. If Version Picker is null/Empty, then the text field needs to hide.

(Or in the case of this script logic: If Version Picker IS NOT null/empty (a value was selected), then Text field IS NOT hidden.)

Does anyone know what I'm missing? 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 31, 2022

I like to use the helptext for debugging behaviours

Add this to your script:

field.setHelpText("$fieldValue (${fieldValue.getClass() - ${(fieldValue instanceof List) ? fieldValue[0].getClass() : ''})")

This will give you some clues as to what kind of data you are dealing with for each situation.

This should show you that when the version field is empty, a empty string value is returned. When one or more version is selected, you get a list of "VersionImpl" objects.

So armed with this information, you should be able to adjust your script accordingly.

def field = getFieldByName("Version Picker")
def fieldValue = field.getValue()
def text = getFieldByName("Text Multi-line")

text.setHidden(!fieldValue || fieldValue.first().name != 'Unknown')
Diana
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.
October 31, 2022

hi @Peter-Dave Sheehan thank you for the quick response, and the debugging trick is neat!

I added a } between () and - because nothing was showing up at first.

field.setHelpText("$fieldValue (${fieldValue.getClass()} - ${(fieldValue instanceof List) ? fieldValue[0].getClass() : ''})")

I end up going back to the logic "if version picker is empty, then hide text field" and this is what I end up that worked for me

def field = getFieldByName("Version Picker")
def fieldValue = field.getValue()
def text = getFieldByName("Text Multi-line")

text.setHidden(fieldValue.toList().first() == null)

 Thanks again!

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 31, 2022

Ah yes, nice catch on my typo.

TAGS
AUG Leaders

Atlassian Community Events