Set option of select field based on component

Deleted user August 17, 2015

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

1 vote
Deleted user August 19, 2015

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

JamieA
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.
August 19, 2015
Deleted user August 19, 2015

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?

Deleted user August 23, 2015

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

Deleted user September 2, 2015

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

JamieA
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.
September 2, 2015

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

0 votes
Deleted user September 11, 2015

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
    }
}
0 votes
Deleted user September 7, 2015
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
    }
}
JamieA
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.
September 8, 2015

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.

0 votes
Deleted user August 17, 2015

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

0 votes
Mike Friedrich
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.
August 17, 2015

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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events