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 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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Published Apr 19, 2018 in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

366 views 0 6
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