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

2 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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 @Peter-Dave Sheehan 

Thank you for your help. It is highly appreciated. 

I will test the new script as soon as possible.

Roger Hughes May 8, 2023

Worked nicely for me too.

Suggest an answer

Log in or Sign up to answer