Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Using Behaviors to Show/Hide - Need to Clear old Options

Nathan Parmeter January 29, 2020
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.

1 answer

0 votes
PD Sheehan
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.
January 29, 2020

Hi @Nathan Parmeter 

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"] ) ) {

Suggest an answer

Log in or Sign up to answer