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!
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.