ScriptRunner: Limit values in a single select field based on value selected in second single select

Michael
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.
May 12, 2023

Hi everyone,

New groovy user here, and I've been trying to figure out a scriptrunner behavior for how to limit values within a single select custom field based on the option selected within an above single select custom field.

 

A quick google search brings you to several "answers" for this question; but the current behavior editor returns errors / issues with every "answer" I've found - hence I'm asking here.

 

End Goal Example:

Single Select Field #1: "States" (Values = Michigan, Ohio, New York)

Single Select Field #2: "Cities" (Values = Detroit, Saginaw, Dayton, Columbus, New York City, Buffalo)

I want to limit the available values in "Cities" so that only the "Cities" that are within the corresponding "States" are shown:

Michigan = Detroit, Saginaw

Ohio = Dayton, Columbus

New York = New York City, Buffalo

 

Can someone please help us with this issue?

Thanks,

Mike

3 answers

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 12, 2023

Hi @Michael

This appears to be a very common Behaviour question.

You can find a similar question and solution in this Community Post.

Thank you and Kind regards,

Ram

Michael
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.
May 12, 2023

Hi again Ram,

I spoke too soon in my reply; the editor seemed to be OK with the code input, but when I go to test out the code it just doesn't filter the field to the options I input within "list 2". (All options are still available)

Should I reply within the original thread with my query? Or would you prefer I reply here?

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 12, 2023

Hi @Michael

Could you please share a screenshot of your Behaviour configuration?

I will review it and provide some feedback.

Please continue in thread.

Thank you and Kind regards,

Ram

Michael
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.
May 12, 2023

Hi again Ram,

I believe (and I could be wrong since I've very new here) my issue was a combination of using the same field within another groovy script on the same form as well as changing the scripting for the "null" value within "List 2".

I now have it working, and I'll post my code below for anyone who happens to find their way to this page and wants to use it.

Code Explanation:
Our team uses a very similar code to what was previously provided by you within the linked thread, but with one exception - we wanted to include all values within the second field if the "Null" option was left. This is because we either use a single select list for values, OR a text field for "potential" new values that we might add to "List 1" at a later date.

With that said:

1) I've customized the script so the the "List 1" is defined by the field's Jira "customfield_ID" instead of the "(fieldChanged)" action.

2) I've also incorporated the "null" value within the "else if" statements.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 12, 2023

Hi @Michael

When using a Server-Side Behaviour, you must ensure the the field the Behaviour is configured for is declared as:-

def list1 = getFieldById(fieldChanged)

Else, the Behaviour will not work as expected.

The fieldChanged option is to ensure that the Behaviour will only trigger when a change is made to that field.

In this case only after the first list is selected, will the second list be filtered.

 

Thank you and Kind regards,

Ram

Michael
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.
May 12, 2023

Hi again Ram,

Using the "def list1 = getFieldById(fieldChanged)" code doesn't work for us. If we implement the code as I provided it works as intended, and updates the values as needed in "List 2" when list 1's values change.

We currently already have "List 1" called out by its customfield_ID within another behavior script for another field within the same form.

For example:
customfield #1 = "Request Type"
customfield #2 = "list 1"

the "Request Type" field has a script running that either shows or hides "list 1" depending on which value is selected within that field. (and has "list 1"s customfield_id as the def.

I'm wonder if this is why the (fieldChanged) doesn't work but the "getFieldById("customfield_#####") is... either way we are happy with the result :)

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 12, 2023

Hi @Michael

It's not working for you because your code is incorrect.

As requested earlier could you please share a screenshot of your Behaviour configuration?

And also a screenshot of the type of fields you are using.

I need this to provide a sample code.

Also, please clarify what version of Jira and ScriptRunner are you currently using.

Thank you and Kind regards,

Ram

Michael
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.
May 12, 2023

Hi again Ram,

The coding is the exact same as below - but just with a different customfieldID:List 1 behavior.png

Here are the configs for the example list fields we used for this code:

List 1 config.pngList 2 Config.png

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 13, 2023

Hi @Michael

Below is the working Server-Side Behaviour code that I have tested in my environment.

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
def list1 = getFieldById(fieldChanged)
def list1Value = list1.value.toString()

def list2 = getFieldByName('List2')

def optionManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager

def list2CustomField = customFieldManager.getCustomFieldObject(list2.fieldId)
def list2Config = list2CustomField.getRelevantConfig(issueContext)
def list2Options = optionManager.getOptions(list2Config)

def listBNewOptions = ['Option1', 'Option2', 'Option3', 'Option4', 'Option5']

if(list1Value == 'Zone1') {
listBNewOptions = ['Option1', 'Option2']
} else if(list1Value == 'Zone2') {
listBNewOptions = ['Option3', 'Option4']
} else if(list1Value == "Zone3") {
listBNewOptions = ['Option5']
}

def listBNewOptionsMap = list2Options?.findAll {
it.value in listBNewOptions
}

list2.setFieldOptions(listBNewOptionsMap)

 If you observe in the code above, the field List1 which the Server-Side Behaviour is configured for is declared using:-

def list1 = getFieldById(fieldChanged)


Below is a screenshot of the Behaviour Configuration:-

behaviour_config.png

Also, you don't need to create a separate if/else condition for the None option.

Just set the default list options before the if/else condition. If none of the options is selected, the default options will be displayed in the second list.

Below are a couple of test screenshots for your reference:-

image1.jpg

 

 

image2.jpg

 

image3.jpg

image4.jpg

Thank you and Kind regards,

Ram 

1 vote
Michael
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.
May 12, 2023

Here is the completed code which works correctly for our needs:

import com.atlassian.jira.component.ComponentAccessor

import com.onresolve.jira.groovy.user.FieldBehaviours

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

//the "ID" of the field goes where "12345" is if the field you are referencing is a custom field

def list1 = getFieldById("customfield_12345")


def list1Value = list1.value.toString()

def list2 = getFieldByName("List 2")

def optionManager = ComponentAccessor.optionsManager

def customFieldManager = ComponentAccessor.customFieldManager

def list2CustomField = customFieldManager.getCustomFieldObject(list2.fieldId)

def list2Config = list2CustomField.getRelevantConfig(issueContext)

def list2Options = optionManager.getOptions(list2Config)

def listBNewOptions

if(list1Value == "null") {

    listBNewOptions = ["option 1","option 2","option 3","option 4","option 5"]

} else if(list1Value == "Zone 1") {

    listBNewOptions = ["option 1"]

} else if(list1Value == "Zone 2") {

    listBNewOptions = ["option 2","option 3"]

} else if(list1Value == "Zone 3") {

    listBNewOptions = ["option 1","option 5"]

}

list2.setFieldOptions(listBNewOptions)
Michael
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.
May 15, 2023

.

0 votes
Michael
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.
May 12, 2023

Thanks Ram,

The first time I entered the coding for this within my editor it didn't work when I modified to fit my environment; but for some reason it worked now. Sorry for wasting your time!

Suggest an answer

Log in or Sign up to answer