You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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
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
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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']
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great! Thank you again @Peter-Dave Sheehan this is exactly what I've been trying to figure out.
I appreciate all of the information.
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.