Forums

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

Scriptrunner Behaviour show a field when value is selected in dropdown

Valiantys Support
Contributor
November 23, 2021

Hi All,

I'm trying to put together a behaviour in scriptrunner, I'm pretty new to creating these so please bare with me. 

In a nutshell, I have a custom field (State) when a value is selected (i.e. CA), I would need that particular custom field to display on screen (i.e. County CA). This is what I've put together so far, but I haven't had any success displaying the field when i select an option. 

initialiser

def State = getFieldByName("State")

 

field

import com.atlassian.jira.component.ComponentAccessor

def State = getFieldByName("State")


//County List
def CountyCA = getFieldById("customfield_18914")
def CountyAL = getFieldById("customfield_18910")
def CountyAK = getFieldById("customfield_18911")
def CountyAR = getFieldById("customfield_18913")
def CountyAZ = getFieldById("customfield_18912")

CountyCA.setHidden(true)
CountyAL.setHidden(true)
CountyAK.setHidden(true)
CountyAR.setHidden(true)
CountyAZ.setHidden(true)


if (State == "CA")
{
//Show Fields
CountyCA.setHidden(false)


//Hide Fields
CountyAL.setHidden(true)
CountyAK.setHidden(true)
CountyAR.setHidden(true)
CountyAZ.setHidden(true)
}

 

 

Any suggestions on where I can go from here?

Thanks, 

Norm 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
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.
November 23, 2021

The main issue is that you are attempting to compare the FormField object (State) with a string value. When you need to compare the value of the Form Field against your string

Maybe it's just a style thing, but I like to abstract repetitive elements like you are showing into a simple data structure and make the actual actions a simple loop.

def stateCountyFieldMap = [
'CA': 'customfield_18914',
'AL': 'customfield_18910',
'AK': 'customfield_18911',
'AR': 'customfield_18913',
'AZ': 'customfield_18912',
]

def stateField = getFieldByName('State')
def selectedState = stateField.value

stateCountyFieldMap.each{state, fieldId ->
def countyField = getFieldById(fieldId)
def isSelectedState = selectedState == state
countyField.setHidden(!isSelectedSate)
}

This way, you can easily just add new state/county fields combination in the map variable and everything else automatically works

Valiantys Support
Contributor
November 24, 2021

Hi @PD Sheehan 

Thank you very much!! That worked perfectly.

There is one more custom field I need to add into this equation. It would be very similar to the County field, where it needs to appear when a specific state is selected as well. 

The custom field name would be FIPS_(state abbrev.), how would I integrate this into what you've provided? 

Would this work?

stateCountyFieldMap.each{state, fieldId ->
def countyField = getFieldById(fieldId)
def fipsField = getFieldById(fieldId)
def isSelectedState = selectedState == state
countyField.setHidden(!isSelectedSate)
fipsField.setHidden(!isSelectedSate)

Thank you for the feedback! 

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.
November 24, 2021

Not quite ... fieldId here would be the same so countyField and fipsField would point to the same thing.

You need to adjust the data structure and flow

def stateFieldMap = [
'CA': ['customfield_18914', 'add fipsField Here'],
'AL': ['customfield_18910', 'add fipsField Here'],
'AK': ['customfield_18911', 'add fipsField Here'],
'AR': ['customfield_18913', 'add fipsField Here'],
'AZ': ['customfield_18912'], //you can leave fipsField out completely if not applicable
]

def stateField = getFieldByName('State')
def selectedState = stateField.value

stateFieldMap.each{state, fields ->
fields.each{ fieldId ->
def field = getFieldById(fieldId )
def isSelectedState = selectedState == state
field.setHidden(!isSelectedSate)
}
}

You'll see that we now have 2 loops. The outer loop goes through each state. Then the inner loop goes through each field that should be visible for that state.

I also renamed some variables since the fields are not long specific to counties.

Just in case that's obscure for you here are some definitions:

/*A map is a list of key:value pairs*/
def emptyMap = [:] //empty map
def mapWithSingleItem = [keyname:"some string value"]
def mapWithManyItems = [keyname:"some string value", otherKey:"othervalue"]

/* A List or Array is a sequence of individual items */
def emptyList = []
def listWithSomeItems = [1,"test",[:], mapWithSingleItem]
//the list can include anything, including variables

//the same is true for maps, the value part of the map can be anything including lists or other maps
def mapOfList = [keyName: ['list','of','string']
Like Valiantys Support likes this
Valiantys Support
Contributor
November 24, 2021

Great! Thank you again @PD Sheehan this is exactly what I've been trying to figure out. 

I appreciate all of the information.

TAGS
AUG Leaders

Atlassian Community Events