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)
}
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Matthew Clark . I tried your shorter version and while I was able to get the field show and hiding as I desired, I cant create a ticket because I’m now getting the following error:
Data residing on destination server: Data residing on destination server is required.
Do you have any idea what would cause this and how to resolve it? Please advise. thanks. ejs
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Eric
As far as I can tell from what you have posted, that message is not from ScriptRunner.
My guess is that you have a workflow validator or another plugin throwing an error when you do not fill out a field. Perhaps you are hiding a field with behaviours that is required by a workflow validator.
I suggest you create a new project with its own workflow and fields separate from this project to test with.
You can also check the console log of the browser dev tools and the atlassian log files to see if there are any errors when you try to create an issue. These may provide clues as to what causes your problem.
Kind regards
Matthew
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.