Scriptrunner Behavior - Hide / Unhide and Require / Not Require - NOT working correctly

Chris Annal
Contributor
July 23, 2021

I'm using the following script in Scriptrunner Behaviors and it's almost working:

(The Field this is triggered off of is a field named "Products", and here is the script)

 

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def ProductsField = getFieldById(getFieldChanged())
def otherProductField = getFieldByName("Products (Other)")
def selectedOption = ProductsField.getValue() as String
def isOtherSelected = selectedOption == "Other"
otherProductField.setRequired(false)
otherProductField.setHidden(true)
if (isOtherSelected)
{
otherProductField.setRequired(true)
otherProductField.setHidden(false)
}

 

The above is supposed to HIDE a field named "Products (Other)" and initially set it as (required = false) and hidden = true

That works, but when I select "Other" in the "Products" field, the opposite is supposed to execute. That is, the custom field "Products (Other)" should be unhidden and required.

When I select "Other" in the "Products" field and nothing happens until I click the "Create" button for the issue, THEN the "Products (Other)" field becomes unhidden as desired, but it's STILL NOT "required". What's wrong with my code?

(Using Jira 8.13.6, the "Input version" field on this submission form doesn't want me to type in it).

 

1 answer

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 24, 2021

Hi @Chris Annal

I have tested the Behaviour in my environment. However, I was not able to reproduce the issue.

The Jira version I have tested this on is the same as yours, i.e. 8.13.6, along with the latest ScriptRunner release, i.e. 6.30.2.

Below is the code that I have tested with:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def products = getFieldById(fieldChanged)
def productsValue = products.value.toString()

def otherOptions = getFieldByName("Other Options")

otherOptions.hidden = true
otherOptions.required = false

if(productsValue == "Other") {
otherOptions.hidden = false
otherOptions.required = true
}

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a print screen of the Behaviour configuration:-

behaviour_config.png

Below are a few test screens:-

1) The Create Screen:-

other_selected.png

 

2) The Edit Screen:-

edit_screen2.png

If you observe in the edit screen, the Other Options text field is visible when the Other option is selected from the Products list.

Also, please upgrade the current version of ScriptRunner you are using to the latest release, i.e. 6.30.2. Once you have done so, please do a full re-index for your Jira instance.

Another point to check, when you are in the Edit screen, please ensure the field is added to the Edit Screen, as shown below:-

field_visibility.png

 

I hope this helps to solve your question. :)

Thank you and Kind Regards,

Ram

Chris Annal
Contributor
July 26, 2021

Thanks for those suggestions, Ram!!

I wonder if part of my problem is that I'm at the "Create" step in my workflow? The Create screen doesn't seem to react to my entering "Other" as the choice for the field named "Products" until I click the "Create" button to submit the issue. At that point, the field is checked and the "Products - Other" field becomes visible, but still is not checked as "required". I should mention that this project uses a single "Create, Edit, and View" screen for this issue type. 

I have other Behaviors that work fine, but they are later in the workflows. Also, I will take your advice about updating my Scriptrunner version, too. 

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 27, 2021

Hi @Chris Annal

Thank you for getting back.

If the solution I provided earlier works out for you, kindly accept the solution.

Looking forward to your feedback.

Thank you and Kind Regards,

Ram

Chris Annal
Contributor
July 27, 2021

Hi Ram,

Unfortunately, I still have the problem of the action (unhiding and making required) not working as desired. When I select "Other" in my chosen field, the "Products - Other" field remains hidden until I click the "Create" button. Only then does it appear, and it's not marked as required, so the users don't have to enter anything in that field. I have a test environment and installed the latest Scriptrunner version there - still no joy. I am assuming that maybe because I use a single screen for "Create, Edit, and View" that maybe the problems you experienced in the "Edit" screen may be present in my single screen, but I haven't had enough time to investigate. If I find a resolution, I'll definitely share. I think your suggested script should work. I submitted a ticket to Adaptavist in regard to the "Edit" screen behavior.

Suggest an answer

Log in or Sign up to answer