Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,560,165
Community Members
 
Community Events
185
Community Groups

Scriptrunner Behaviour show a field when value is selected in dropdown

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

0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 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

Hi @Peter-Dave 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! 

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 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

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

I appreciate all of the information.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events