Related Versions
Jira v8.5.1
Scriptrunner v5.6.14.1-p5
I have several Behavior scripts that are showing/hiding fields based on other field selections. This part is working great.
My question is in regards to clearing options if the mainField options changes.
Heres My Code:
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def mainField = getFieldById(getFieldChanged())
def applicationOther = getFieldById("customfield_13737")
def newJiraProject = getFieldById("customfield_13701")
def jiraProjectName = getFieldById("customfield_13702")
def jiraProjectKey = getFieldById("customfield_13703")
def jiraProjectLead = getFieldById("customfield_13704")
def jiraProjectAdmin = getFieldById("customfield_13818")
def businessJustification = getFieldById("customfield_13705")
def copyConfig = getFieldById("customfield_13707")
def selectedOption = mainField.getValue() as String
if (selectedOption != "Jira" || "Other") {
applicationOther.setHidden(true)
newJiraProject.setHidden(true)
jiraProjectName.setHidden(true)
jiraProjectKey.setHidden(true)
jiraProjectLead.setHidden(true)
jiraProjectAdmin.setHidden(true)
businessJustification.setHidden(true)
copyConfig.setHidden(true)
applicationOther.setRequired(false)
newJiraProject.setRequired(false)
jiraProjectName.setRequired(false)
jiraProjectKey.setRequired(false)
jiraProjectLead.setRequired(false)
jiraProjectAdmin.setRequired(false)
businessJustification.setRequired(false)
copyConfig.setRequired(false)
}
if (selectedOption == "Jira") {
newJiraProject.setHidden(false)
newJiraProject.setRequired(true)
}
if (selectedOption == "Other") {
applicationOther.setHidden(false)
applicationOther.setRequired(true)
}
I would like the fields that the selectedOption = Jira / Other exposes/hides to clear values if values are set.
I have a workaround to clear the values on Create but this is not a long term solution.
Any assistance is appreciated.
The solution that's coming to mind seems so simple that I wonder if I'm mis-understanding the question/problem.
Isn't it just a matter of setting the value to null?
formfield.setFormValue(null)
BTW, a little trick I like to share... how do you like this version of your code?
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def mainField = getFieldById(getFieldChanged())
def selectedOption = mainField.value.value
def applicationOther = getFieldById("customfield_13737")
def newJiraProject = getFieldById("customfield_13701")
def detailFields = ['customfield_13702',
'customfield_13703',
'customfield_13704',
'customfield_13818',
'customfield_13705',
'customfield_13707']
if (selectedOption == "Jira") {
newJiraProject.setHidden(false).setRequired(true)
} else if (selectedOption == "Other") {
applicationOther.setHidden(false).setRequired(true)
} else {
detailFields.each{fieldId ->
getFieldById(fieldId).setHidden(true).setRequired(false).setFormValue(null)
}
}
When you do the exact same action on many fields, just put them in a list and iterate over it.
I also happened to notice that this may not do what you expect:
if (selectedOption != "Jira" || "Other") {
It is equivalent to doing this:
if ( (selectedOption != "Jira") || ("Other") ) {
Maybe you meant to have either
if (selectedOption != "Jira" || selectedOption != "Other") {
Or
if ( !(selectedOption in ["Jira","Other"] ) ) {
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.