Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Add a new option to a select list when an issue is created

Created 1 year ago, Updated 2 month(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.0 - 8.19)
compatibility bullet
ScriptRunner For Jira (7.10.0)
Language |
groovy
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.context.IssueContext

def optionsManager = ComponentAccessor.getOptionsManager()

def companyName = issue.getCustomFieldValue('Company Name')
if (!companyName) {
    return
}

def companyField = ComponentAccessor.customFieldManager.getCustomFieldObjects().find { it.name == 'Company' }
def fieldConfig = companyField.getRelevantConfig(IssueContext.GLOBAL)
def options = optionsManager.getOptions(fieldConfig)

if (options.any { it.value == companyName }) {
    return
}

options.addOption(null, companyName)

// Get a fresh copy of the options object, as the one we have won't have been updated when we added the company name
optionsManager.getOptions(fieldConfig).sortOptionsByValue(null)
Having an issue with this script?
Report it here