Script Runner question -- how to copy all option numeric ids and values from a select-list to key/value pairs in a map?

Hi,

I'm curious. How do I get all options from a select list custom field, and add them to a map?

For example:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def optionsManager = ComponentAccessor.getComponentOfType(OptionsManager.class)

def cf = customFieldManager.getCustomFieldObjectByName("Region")
def fieldConfig = cf.getRelevantConfig(issue)
def options = optionsManager.getOptions(fieldConfig).dump()

-- will give me output like so:

<com.atlassian.jira.issue.customfields.option.OptionsImpl@1ecdc903 optionsLookup=[14262:Region1, 14261:Region2, 14260:Region3, 14259:Region4] relatedField=com.atlassian.jira.issue.fields.config.FieldConfigImpl@76c6d7a5 reorderer=com.atlassian.jira.util.CollectionReorderer@62fdce1f optionsManager=com.atlassian.jira.issue.customfields.manager.CachedOptionsManager@42de7804 elementData=[Region4, Region3, Region2, Region1, null, null, null, null, null, null] size=4 modCount=1>

I just want the optionsLookup data, so I could populate a map with the numeric id as key, and the option name as value. For example: 14262 as key, and Region1 as value, etc.

Any tips? Thanks

1 answer

This widget could not be displayed.

This seems to do the trick (got idea from other posts here and elsewhere):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;

IssueManager issueManager = ComponentAccessor.getIssueManager();

myissue = "TESTRFC-8"

Issue issue = issueManager.getIssueObject("$myissue");

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def optionsManager = ComponentAccessor.getComponentOfType(OptionsManager.class)

def cf = customFieldManager.getCustomFieldObjectByName("Region")
def fieldConfig = cf.getRelevantConfig(issue)

def options = optionsManager.getOptions(fieldConfig)
def results= [:]
for (Option option : options) {

    results.put(option.optionId,option.value)
}

return results

-- it returns:

{14259=Region4, 14260=Region3, 14261=Region2, 14262=Region1}

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

425 views 3 17
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you