Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner - Set a select list custom field value

MIN_MWO012 November 16, 2022

I need to update a select list (single choice) custom field as part of a workflow transition so I have tried the code provided in the link below

https://docs.adaptavist.com/sfj/set-a-select-list-custom-field-value-11468840.html

Unfortunately I get the error message belowerror.PNG

Does anyone know how to fix this?

My code is

 

import com.atlassian.jira.component.ComponentAccessor
def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Change Request Priority")
def cfConfig = cfSelect.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
    it.toString() == '5 - Urgent'
}
issue.setCustomFieldValue(cfSelect, value)

 

We are using Jira Software Data Center 8.20.8 and ScriptRunner 7.1.0

 

Thank you for your help

1 answer

4 votes
PD Sheehan
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.
November 16, 2022

Your error is on this line:

def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Change Request Priority")

Notice the 's' in getCustomFieldObjecsByName 

This, and the error message indicating that a "getRelevantConfig"  is no application to "Collection" means that you have a collection of custom fields. Not a single custom field object

Change that line to

def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Change Request Priority")[0]

 But also, it's better to use an explicit method/property to compare objects rather than the generaic "toString"

Here is a fully revised version of your script:

import com.atlassian.jira.component.ComponentAccessor
def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Change Request Priority")[0]
def cfConfig = cfSelect.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.value == '5 - Urgent'
}
issue.setCustomFieldValue(cfSelect, value)
MIN_MWO012 November 25, 2022

Dear @PD Sheehan 

Thank you for your help. It is highly appreciated. 

I will test the new script as soon as possible.

Like PD Sheehan likes this
Roger Hughes
Contributor
May 8, 2023

Worked nicely for me too.

Like PD Sheehan likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events