How to populate a custom field select list with multiple choices

Karina Green June 4, 2021

I am creating a new issue via a script in scriptrunner. I am trying to populate a customfield select list with 2 options, I not sure what I need to do to set custom field values to populate the 2 options  in this code, any help would be great.


def location = customFieldManager.getCustomFieldObjectByName("Location")

def fieldConfigLocation = location.getRelevantConfig(issue)
def optionLocation = optionsManager.getOptions(fieldConfigLocation).getOptionForValue(["ODC, RDC"], null)
issue.setCustomFieldValue(location, [optionLocation])

1 answer

1 vote
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 4, 2021

Hi @Karina Green 

Try below code to populate Select list field value according to issue type selection :-

Please make sure value which you want to populate must be added in select list field.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.*


def issuetype = getIssueContext().getIssueType().name


def selectcf = getFieldByName("Sub-type")
selectcf.setFormValue(null)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def customField = customFieldManager.getCustomFieldObject(selectcf.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)


if (issuetype.contains("Story")) {
def optionsMap = options.findAll {
it.value in ["Custom Requests","Survey Project Management","Proposals"]
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}
selectcf.setFieldOptions(optionsMap)
}
else {
def optionsMap = options.findAll {
it.value in ["Job Matching Assistance Only", "Standard Data Extract","Job Matching and Data Extract","Prevailing Wage"]
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}
selectcf.setFieldOptions(optionsMap)


}
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 4, 2021

I forget to add classes :- 

Correct Script --

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.customfields.manager.OptionsManager

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours


def issuetype = getIssueContext().getIssueType().name


def selectcf = getFieldByName("Sub-type")
selectcf.setFormValue(null)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def customField = customFieldManager.getCustomFieldObject(selectcf.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)


if (issuetype.contains("Story")) {
def optionsMap = options.findAll {
it.value in ["Custom Requests","Survey Project Management","Proposals"]
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}
selectcf.setFieldOptions(optionsMap)
}
else {
def optionsMap = options.findAll {
it.value in ["Job Matching Assistance Only", "Standard Data Extract","Job Matching and Data Extract","Prevailing Wage"]
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}
selectcf.setFieldOptions(optionsMap)


}

Suggest an answer

Log in or Sign up to answer