Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,463,706
Community Members
 
Community Events
176
Community Groups

Show some options from a list based on issue type

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

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

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!! :)

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

Atlassian Community Events