Set option of select field based on component

Hi, I need some help for the following issue, after trying several ways found on the web without any success.

The first step, that our users have to do, is to choose a component.

As soon as they choose "componentA", we'd like to have 2 options of a select list ("listA") filled automatically with the options "option1" and "option3".

Could you please provide me with a script for that requirement?

Thanks in advance,

Robert

5 answers

This does not look like the right thing to do with Jira. What is the problem you are trying to solve? Maybe we can suggest alternatives if you describe the use cases.

Hi Mike,

we are actually improving the usability of our JIRA instance. In this case, there is one kind of bug issues (issue type "Bug" with the component "componentA") where there is some basic information, which is valid for nearly every issue. For example, we have a multi user picker field, where always the same persons have to be entered. Another field is the described "listA", where in every issue the options "option1" and "option2" should be preselected.

But as I said - only for the issue type "Bug" with the component "componentA".

The Behaviour add-on seems to be perfectly well suited for our use case. But unfortunately I couldn't figure out, what the groovy script should look like.

Regards, Robert

Could anyone help, please? @Jamie Echlin [Adaptavist] maybe?

Thanks so far, Jamie. I didn't know the new example page. I managed to prefil checkbox and list fields. But unfortunately I didn't find any example, which shows how to make a component condition like described above. I need to set the field information automatically, only IF a special component is selected. How would a component condition like this have to look like?

Does anybody have an idea what a component condition in a beaviour would have to look like? @Jamie Echlin [Adaptavist]?

Still didn't figure out a solution. Any more help here, @Jamie Echlin [Adaptavist]?

The closest I have to your original question is here: https://gist.github.com/jechlin/8a0f026ac62091b553be - it's a bit old though.

Thanks so far.
I tried this script now for the field "component/s", but nothing happens at all when selecting "Frontend" ("Specification" is a checkbox field and "Mechanics" a valid checkbox option).
Did I get something wrong? Or is it just not possible?
FormField formComponent = getFieldById(fieldChanged)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def spec = customFieldManager.getCustomFieldObjectByName("Specification")

Object componentFormValue = formComponent.getFormValue()
List componentIds = []


if (componentFormValue instanceof List) {
    componentIds.addAll(componentFormValue as List)
}
else {
    // could be an empty string if all components are deselected
    if (componentFormValue) {
        componentIds.add(componentFormValue)
    }
}


componentIds.each { componentId ->
    try {
        Long.parseLong(componentId as String)
    } catch (NumberFormatException e) {
        log.error("Could not get component Id as Long")
        return
    }
    def component = getProjectComponent(Long.parseLong(componentId as String))
    
	log.debug "name: \"" + component?.name + "\""
    switch (component?.name) {
        case "Frontend":
        	issue.setCustomFieldValue(spec, "Mechanics")
            break
    }
}

You need to set it to the ID of the option rather than the string "Mechanics". Can you try that, you can get the option ID from the page where you configure the options for the custom field. If that works I'll show you a better way where you can get the option ID from the name.

Hi Jamie,

I understand that my last couldn't work. I now define the optionToSelect like described here: https://scriptrunner.adaptavist.com/3.1.5-SNAPSHOT/jira/recipes/behaviours/setting-default-fields.html

But still nothing happens selecting the component "Frontend". But can't find a mistake either. Do you?

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.component.ComponentAccessor

FormField formComponent = getFieldById(fieldChanged)

def componentManager = ComponentManager.instance
def optionsManager = ComponentAccessor.getOptionsManager()

def spec = getFieldByName("Specifications")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
customField = customFieldManager.getCustomFieldObject(spec.getFieldId())
config = customField.getRelevantConfig(getIssueContext())
options = optionsManager.getOptions(config)
def optionsToSelect = options.findAll {it.value in ["Mechanics"]}

Object componentFormValue = formComponent.getFormValue()
List componentIds = []

if (componentFormValue instanceof List) {
    componentIds.addAll(componentFormValue as List)
}
else {
    // could be an empty string if all components are deselected
    if (componentFormValue) {
        componentIds.add(componentFormValue)
    }
}

componentIds.each { componentId ->
    try {
        Long.parseLong(componentId as String)
    } catch (NumberFormatException e) {
        log.error("Could not get component Id as Long")
        return
    }
    def component = getProjectComponent(Long.parseLong(componentId as String))
    log.debug "name: \"" + component?.name + "\""
    switch (component?.name) {
        case "Frontend":
          issue.setFormValue(spec, optionsToSelect)
            break
    }
}

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,932 views 12 18
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot