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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Script to Show/Hide Custom Text Field Based on Field with Checkboxes not working completely Edited

Hi Everyone,

I am using this code in order to show a textField based on checking the value of Other in my checkbox field.  However, I want to be able to select any of the checkboxes in my formField and show the textField.  I have tried to place || between each value in the 'if' statement, but I cannot get multiple values to show/require the textField.  It only shows when 'Other' is checked.  I would like to show/require the textField if any of the checkboxes are checked. 

I have tried this, but it doesn't work:

if (FormFieldValue == "On unit" || FormFieldValue == "Other, Please specify:")

Thank you for any help!!!

Lesley

 

def FormField2 = getFieldById("customfield_15754")
def FormField = getFieldById("customfield_15833")

def FormFieldValue = FormField.getValue()

if (FormFieldValue == "Other, Please specify:") {
FormField2.setRequired(true)
FormField2.setHidden(false)
} else {
FormField2.setRequired(false)
FormField2.setHidden(true)
}

1 answer

Hi Lesley

When you use the behaviours method "FormField.getValue()" on a Checkbox field it changes the data type of the returned value depending on the number of checkbox options selected.

1 checkbox ticked = String
2 or more = List of Strings

So you just need to make sure you handle the data types correctly.


A few people have had issues with this so I wrote an example to show how you can code this in behaviours.


I will get this added to our Script library soon but for now, you can access the example here.

 

The important part of the code relevant to your problem is:

List<String> chosenValuesList = []
if (checkBoxFieldValue instanceof String) {
    chosenValuesList.add(checkBoxFieldValue)
} else if (checkBoxFieldValue instanceof ArrayList) {
    chosenValuesList.addAll(checkBoxFieldValue)
}

 

Regards

Matthew

Thanks, Matthew!

Your script works great for making sure only ‘None’ can be selected, if it is selected!  However, I still want to be able to select the value, “Other,” (which is one of the available options) and have a text box open up for the user to add more information if necessary.  Can I use the following code to show/hide this other field as part of this script, too?  If “Other” (or any value but 'None') is selected, then the user should always see the text box.  Is that possible?  Thank you again!

 

def textField = getFieldById("customfield_15752")

def FormField = getFieldById("customfield_15821")

 

def FormFieldValue = FormField.getValue()

 

if (FormFieldValue == "Other") {

    textField.setRequired(true)

    textField.setHidden(false)

} else {

    textField.setRequired(false)

    textField.setHidden(true)

}

If you only want to hide the other field if the user chooses "None" from the checkbox and do not care if they also choose another value as well as "None" then you can use this shorter example here.

 

Regards

Matthew

Hi Matthew,

 

I apologize for the delay.  I tried your code, then, I tweaked it a bit to get the results that I am trying to achieve (note the 'Other' in the second to last paragraph).  However, I am still having this problem - is there any way around it?  Thanks!

Lesley

 

If the 'Other' checkbox is selected, then de-selected, it still requires the user to type in the Text Box and leaves the text in the Text Box.

I would prefer - if 'Other' is de-selected, then the Text Box should clear and no longer be required and be hidden again.

 

import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)

def otherField = getFieldById("customfield_15752")
def checkBoxField = getFieldById(getFieldChanged())
def checkBoxFieldValue = checkBoxField.getValue()

log.debug("""

checkbox field value = $checkBoxFieldValue
checkBoxFieldType = ${checkBoxFieldValue.getClass()}

""")

List<String> chosenValuesList = []
if (checkBoxFieldValue instanceof String) {
chosenValuesList.add(checkBoxFieldValue)
} else if (checkBoxFieldValue instanceof ArrayList) {
chosenValuesList.addAll(checkBoxFieldValue)
}

//if the user has None selected and nothing else:
if("Not applicable" in chosenValuesList && chosenValuesList.size() == 1){
otherField.setRequired(false)
otherField.setHidden(true)
checkBoxField.clearError()

//if the user has selected None and attempts to select another value:
}else if("Not applicable" in chosenValuesList && chosenValuesList.size() > 1){
checkBoxField.setError("You can't select another value if \"Not applicable\" is selected.")

//if the user has NOT selected None and the list size is greater than or equal to 1
}else if ("Other" in chosenValuesList && chosenValuesList.size() >= 1) {
checkBoxField.clearError()
otherField.setHidden(false)
otherField.setRequired(true)

//if the user has no value selected at all in the checklist field force them to populate the otherField.
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

5 mobile apps for Jira Cloud to boost productivity

  It’s very important to have access to the workflow process from anywhere. Especially if you manage the work of others. There is no difference whether you’re out of office, or drive a ca...

225 views 2 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you