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,300,847
Community Members
 
Community Events
165
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

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! 

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
Community showcase
Published in Jira Service Management

Improving the Create Issue Experience in Jira Service Management Cloud

Hello everyone!  We are very excited to announce some much needed changes to the issue create experience in JSM (the blue "create" button) at the top of the screen.  We have just starte...

208 views 7 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you