Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

dynamic display a single select list base on issue type

ClaireYang
Contributor
May 5, 2019

Hi all, here is the backgroud.

We have a single select list named "service category", which has many options. And I hope it can be dynamic displayed base on issue type change when user submit or edit issues.

For example, when user submit case and choose issue type A, the "service category" field only show A,B and C options. But when user choose issue type B, the field will show other options D and E. I hope it can also work when user edit issue type and "service category" field in a existed issue.

I know it can be done by Behaviours, but I'm a script newbie, could someone tell me how to write the script?

Thank you very much!

1 answer

0 votes
PD Sheehan
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 7, 2019

Here is how I would do this...

import com.atlassian.jira.component.ComponentAccessor

// some apis we'll use
def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

// first, define a variable to contain all your mapping,
// this way you just need to update this in the future and
// not have to worry about the code logic
// by convention, I like to use the lowercase everywhere
def typeCategoryMap = [
issuetypea : ['optiona','optionb', 'optionc'],
issuetypeb : ['optiond','optione'],
]

//find the category list based on the current issuetype
def categories = typeCategoryMap.find{ it.key == issueContext.issueType.name.toLowerCase()}

//build an option map based on the full list of option values
//first we need to get a custom field object
def cfServiceCategory = customFieldManager.getCustomFieldObjectByName("Service Category")
//next we need a configuration obejct based on the current issue context
def config = cfServiceCategory.getRelevantConfig(issueContext)
//finally we can get the full list of options for that context
def allOptions = optionsManager.getOptions(config)

//now we filter the allOptions for just the list we want
def applicableOptions = allOptions.findAll{it.value.toLowerCase() in categories}


def categoryFld = getFieldByName('Service Category')
//to protect against field name changes, the following is moresecure, you need to lookup your field id
//def categoryFld = getFieldById(123456)

categoryFld.setFieldOptions(applicableOptions)
ClaireYang
Contributor
May 7, 2019

@PD Sheehan  Thank you Peter! I'll try it in my test env and let you know the result.

ClaireYang
Contributor
May 9, 2019

Hi @PD Sheehan , it doesn't work on the test env. When submit case, there is nothing under ServiceCategory field.

 

Script below:

import com.atlassian.jira.component.ComponentAccessor

// some apis we'll use
def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

// first, define a variable to contain all your mapping,
// this way you just need to update this in the future and
// not have to worry about the code logic
// by convention, I like to use the lowercase everywhere
def typeCategoryMap = [
Purchasing : ['LCD','Laptop', 'RAM'],
Misc : ['Others'],
]

//find the category list based on the current issuetype
def categories = typeCategoryMap.find{ it.key == issueContext.issueType.name}

//build an option map based on the full list of option values
//first we need to get a custom field object
def cfServiceCategory = customFieldManager.getCustomFieldObjectByName("ServiceCategory")
//next we need a configuration obejct based on the current issue context
def config = cfServiceCategory.getRelevantConfig(issueContext)
//finally we can get the full list of options for that context
def allOptions = optionsManager.getOptions(config)

//now we filter the allOptions for just the list we want
def applicableOptions = allOptions.findAll{it.value in categories}


def categoryFld = getFieldByName('ServiceCategory')
//to protect against field name changes, the following is moresecure, you need to lookup your field id
//def categoryFld = getFieldById(123456)

categoryFld.setFieldOptions(applicableOptions)
PD Sheehan
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 10, 2019

It's hard to know why it's not working without actually being there and trying.

The way I would try to investigate the reason is to output several debug messages:

import com.atlassian.jira.component.ComponentAccessor

//make sure debuging missages are visible
log.setLevel(org.apache.log4j.Level.DEBUG)

log.info "Start of behavior debugging"
// some apis we'll use
def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

// first, define a variable to contain all your mapping,
// this way you just need to update this in the future and
// not have to worry about the code logic
// by convention, I like to use the lowercase everywhere
def typeCategoryMap = [
Purchasing : ['LCD','Laptop', 'RAM'],
Misc : ['Others'],
]

log.debug "Issue Type Detected: $issueContext.issueType.name"
//find the category list based on the current issuetype
def categories = typeCategoryMap.find{ it.key == issueContext.issueType.name}
log.debug "Categories found: $categories"

//build an option map based on the full list of option values
//first we need to get a custom field object
def cfServiceCategory = customFieldManager.getCustomFieldObjectByName("ServiceCategory")
log.debug "ServiceCategory custom field object: $cfServiceCategory"
//next we need a configuration obejct based on the current issue context
def config = cfServiceCategory.getRelevantConfig(issueContext)
//finally we can get the full list of options for that context
def allOptions = optionsManager.getOptions(config)
log.debug "Full list of ServiceCaegory options: ${allOptions*.value}"
//now we filter the allOptions for just the list we want
def applicableOptions = allOptions.findAll{it.value in categories}
log.debug "Applicable options: ${applicableOptions*.value}"

def categoryFld = getFieldByName('ServiceCategory')
//to protect against field name changes, the following is moresecure, you need to lookup your field id
//def categoryFld = getFieldById(123456)

categoryFld.setFieldOptions(applicableOptions)

Then review or tail your [jira-home]/log/atlassian-jira.log for messages, that should help pinpoint where things went wrong.

Suggest an answer

Log in or Sign up to answer