Show some options from a list based on issue type

PaulaA July 28, 2021

Hi, 

I would like to create a behavior that will show some options for the sub-issue type based on the issue type. I have 3 issue types and 9 options, this is what I have found so far but it doesn't make any change at all :(

customfield_15303is the selectable list with the values for the sub-types.

Can someone help pls?

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


@BaseScript FieldBehaviours fieldBehaviours

int selectListId = 15303

def selectList = getFieldById("customfield_15303" + selectListId)
String issuetype = getIssueContext().getIssueType().getName()
def map

switch (issuetype){
case "Production":
map = ["Defect", "RFS", "RFI"]
break;
case "Change Request":
map = ["CR", "Additional Requirement", "Sub-task"]
break;
case "Defect":
map = ["Pre-UAT defect","UAT defect", "SIT defect"]
break;
}

if (map != null){
def optionsManager = ComponentAccessor.getOptionsManager()
def selectListCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(selectListId)
def fieldConfig = selectListCustomField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(fieldConfig)
def optionsMap = options.findAll {
it.value in map // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()) : it.value
]
}
selectList.setFieldOptions(optionsMap)
}

1 answer

0 votes
Helmy Ibrahim _Adaptavist_
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.
July 28, 2021

Hey PaulaA,

You did not properly grab your field:

int selectListId = 15303

def selectList = getFieldById("customfield_15303" + selectListId)

Where it should be:

int selectListId = 15303

def selectList = getFieldById("customfield_" + selectListId)

Now everything should work :)

Also, if you have ScriptRunner 6.26.0 and above, there is a better way to do this. We have introduced a better way to work with behaviour: Behaviour Enhancement

Now you can pass a list of String instead of the option IDs!

Ultimately, your script could be simplified to this:

int selectListId = 15303
def selectList = getFieldById("customfield_" + selectListId)

String issuetype = getIssueContext().getIssueType().getName()
def map

switch (issuetype) {
    case "Production":
        map = ["Defect", "RFS", "RFI"]
        break;

    case "Change Request":
    map = ["CR", "Additional Requirement", "Sub-task"]
        break;

case "Defect":
    map = ["Pre-UAT defect","UAT defect", "SIT defect"]
        break;
}

if (map) {
    selectList.setFieldOptions(map)
}

I hope this helps! 

Cheers,
Helmy

PaulaA July 29, 2021

Hi @Helmy Ibrahim _Adaptavist_ , 

thanks a lot! it works and you version is definitely easier.

Just another question, the user wants to have sub-task on the secondary subtype, but I cannot select it but sub-task is actually like a separate issue type instead, do you know if there's an option to select it properly?

I'm not even sure if it makes any sense to have sub-task, I'd use task but just in case it's possible so we can have all options.

Thanks again!! :)

Helmy Ibrahim _Adaptavist_
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.
July 29, 2021

Hi PaulA,

I am sorry for not following. But could you please add some screenshots and a use case for a better understanding?

Cheers,
Helmy

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events