ScriptRunner - How to grab the ID of an option

Roberto L
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 21, 2018

Hello Community,

I am trying to figure out how to grab the ID of an option from a select list.

I am able to grab the textual value from the field but now I want to map out what is the ID for that specific textual value.

Below is my current code that allows me to grab the textual value:

// Get the issue
/*MutableIssue currentIssue = getIssueContext() as MutableIssue

//Get the custcom Field
def custom_field = customFieldManager.getCustomFieldObjectByName("Country Priority");

// Get the Config for the field
def fieldConfig = custom_field.getRelevantConfig(currentIssue)

// Find the option to set, grab the value in country priority
def value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == "Priority 1"}

In the above code I grab the customfield, grab the options and filter out on the specific one I want. This for somereason returns me "Priority 1". What I need it to return is the ID/Value of Priorty 1, which happens to be 11322.

Does anyone know who I can fetch that value?

Thank you!

-Roberto

2 answers

1 accepted

6 votes
Answer accepted
Ivan Tovbin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 21, 2018

Try this:

ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find{ it.getValue() == "Priority 1"}.getOptionId()
Roberto L
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 22, 2018

@Ivan Tovbin

This worked perfectly!

Thank you!

-Roberto

Rajaravikiran S September 30, 2020

How to get OptionId for child in cascading dropdown

2 votes
Danyal Iqbal
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 21, 2018

If you want to get the optionID of the option that is already set in the CustomField you can use:

getCustomFieldValue(customfieldObject).getClass().name

Get the class and use https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/issue/customfields/option/Option.html to get ID

or

epic.getCustomFieldValue(cfTeam)?.optionId

If you want to set the customfield, use

def value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == "Priority 1"}


issue.setCustomFieldValue(cf, value)

Why do you need the ID?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events