Use Scriptrunner to get selected index of Select List (single choice)

I am trying to get the option position number of the selected option in a Select list (single choice). I have found a lot of posts about how to get the selected text value, but I am worried that if someone changes the text (for some reason), the script breaks. i am assuming that the position will not change, but how can i get this in scriptrunner?

1 answer

Hey Gerben,

There are ways to write your script defensively enough, in order not to break if someone change the name of an option. 

In theory you could get the third option for the SelectListA and issue with key ATG-1 like (run  it your script console)

import com.atlassian.jira.component.ComponentAccessor

def issue = ComponentAccessor.issueManager.getIssueByCurrentKey("ATG-1")
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("SelectListA")
def fieldConfig = cf.getRelevantConfig(issue)
def selectedOption = issue.getCustomFieldValue(cf)
def indexOf = ComponentAccessor.optionsManager.getOptions(fieldConfig).indexOf(selectedOption)

log.debug "Index of selected option is $indexOf"

 But to be honest it doesn't feel right .... If you want to elaborate a bit more we can craft something...

regards, Thanos

Suggest an answer

Log in or Join to answer
Community showcase
Pedro Felgueiras
Asked 32m ago in Jira

Clone a Zephyr issue

When I try to clone a Zephyr issue only a popup showing the issue id and nothing more shows.  On the browser console I see:  RTE not registered Error: No jira/editor/registry at c...

9 views 0 0
View question

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot