Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

script-runner set value in a select list field

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

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' }

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.

Forget it, I didn't set the value :

issue.setCustomFieldValue(cf, value)

thanks :)

This works great!

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

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

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)

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).

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 ;-)

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

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().

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)

}

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

358 views 3 15
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you