script-runner set value in a select list field

William JUTEAU February 5, 2014

Hello,

I use script runner to create subtask on transition, with additional issues actions like following :

1
2
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'GOL Issue Type'}
issue.setCustomFieldValue(cf, 'Foundation data Workbook')

I can't make it work.

'GOL Issue Type' is a select list field, with an option label "Foundation data Workbook"

Can somebody help me to figure this out ?

Thanks

3 answers

1 accepted

15 votes
Answer accepted
Henning Tietgens
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 5, 2014

Select lists need an Option object as parameter for setCustomFieldValue(). Use this to find the value for setCustomFieldValue()

import com.atlassian.jira.component.ComponentAccessor

// ...

def fieldConfig = cf.getRelevantConfig(issue)
value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Foundation data Workbook' }

William JUTEAU February 5, 2014

Thanks a lot Henning ! The subtask is now well created but the customfield is not set to the value 'Foundation data Workbook' on the subtask.

William JUTEAU February 10, 2014

Forget it, I didn't set the value :

issue.setCustomFieldValue(cf, value)

thanks :)

Joshua Giffen June 9, 2016

This works great!

David J March 9, 2018

After searching for 2 days I finally found your answer Henning, and it did the trick for my problem too! Thanks so much!

Joe Del Nano February 12, 2019

Great answer:  Worked like a charm for me! Thanks, @Henning Tietgens

Vladislav January 31, 2020

Hi All,

Currently it is not working when I create issue. No errors but the customfield is not set to the value.

Any thoughts why it may happen?

Scriptrunner Version:5.6.2.1-jira8

Jira Version: 8.1.1

 

But it perfectly works if i set this script to other transition post-function, for example from status 'OPEN' to status 'IN PROGRESS'.

I would appreciate any help!

 

My script:

def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Department'}
def cfConfig = cf.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == ' IM Department' }
issue.setCustomFieldValue(cf, value)

Vladislav January 31, 2020

Hi All,

I resolved my issue.

So I had to move my post-function to the 1st step before 'Creates the issue originally.'

After that it worked as expected.

This is because in my case script is fetching value from User Properties to apply it to custom field Select List (single choice).

0 votes
Sylvain Leduc September 3, 2020

Hello, just adding my 2 cents to this post because I was struggling also.
I'm so newbie at this, I tried with the code in these posts, but ko.

Then I found this adaptavist KB from 2015 : https://www.adaptavist.com/doco/display/SFJ/Set+a+select+list+custom+field+value

(just a small mistake, he wrote "selectCf" instead of "cfSelect")

import com.atlassian.jira.component.ComponentAccessor


def cfSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("My Custom Field name")
def cfConfig = cfSelect.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
it.toString() == 'The Value I want to set'
}
issue.setCustomFieldValue(cfSelect, value)

 

It works like a charm on Jira 8.8.1 + Scriptrunner 6.7.0 ;-)

Kaushik Veluru
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.
December 16, 2020

I didn't see any error when I run this script but the custom field is not updated.

Henning Tietgens
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.
December 16, 2020

I think this depends on the position of the custom field update. Currently I use this code to update custom fields:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def fieldLayoutManager = ComponentAccessor.fieldLayoutManager
issue.setCustomFieldValue(customField, newValue)
if (issue.id) {
def issueChangeHolder = new DefaultIssueChangeHolder()
def fieldLayout = fieldLayoutManager.getFieldLayout(issue)
def fieldLayoutItem = fieldLayout.getFieldLayoutItem(customField)
def modifiedFields = issue.getModifiedFields()
def modifiedValue = modifiedFields.get(customField.id)
customField.updateValue(fieldLayoutItem, issue, modifiedValue, issueChangeHolder)
}

If the issue id exists (that is after the issue was actually created), you have to use customField.updateValue().

Kaushik Veluru
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.
December 17, 2020

Thanks @Henning Tietgens 
The following script worked for me. 

 

IssueService issueService = ComponentAccessor.getComponent(IssueService);

def optionsManager = ComponentAccessor.getComponent(OptionsManager);
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()

def user = ComponentAccessor.getJiraAuthenticationContext().loggedInUser
def issueObject = issueService.getIssue(user, issue.toString())?.issue

def customField = customFieldManager.getCustomFieldObjectByName("Release Onboarding Status")

Options options = optionsManager.getOptions(customField.getConfigurationSchemes().first().getOneAndOnlyConfig());

def selectedOptions = options.findAll {
it.value == "Not Requested"
}.collect {
it.optionId.toString()
}

// Setting none will work if it can't find an option
issueInputParameters.addCustomFieldValue(customField.id, *selectedOptions)

def update = issueService.validateUpdate(user, issue.id, issueInputParameters)
if (update.isValid()) {
issueService.update(user, update)

}
SMAtlassianMber January 29, 2021

Hello - Is anyone else running into the default becoming permanent?

Both these scripts set the default, but then the field section can not be changes. 

 

Example 1 - https://library.adaptavist.com/entity/set-a-default-option-on-a-select-list

Example 2- https://www.adaptavist.com/doco/sfj/how-to-articles/set-a-select-list-custom-field-value

0 votes
RambanamP
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 5, 2014

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events