How to set a custom field to an option using Groovy Scriptrunner

We have a custom field ("followUp") that has one value "Yes". This shows as a toggle button when we want to create an issue. However, when we transition it to a certain state we always want to set this "followUp" to "Yes".  So far we have this much code:

CustomFieldManager fieldManager = ComponentAccessor.getCustomFieldManager()
CustomField followUpField = fieldManager.getCustomFieldObjectByName("Follow-up")
def followUp = issue.getCustomFieldValue(followUpField) 
//Now how do we set it to the value to "Yes" ?

Thanks for any hints.

2 answers

I would guess that in this example the "follow up" field is a single checkbox, in which case you want to set it to a Collection<Option>

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager

def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def cf = customFieldManager.getCustomFieldObjectByName("Checkboxes") // Checkboxes is the NAME of my custom field
def fieldConfig = cf.getRelevantConfig(issue)
def option = optionsManager.getOptions(fieldConfig).getOptionForValue("Yes", null)
issue.setCustomFieldValue(cf, [option]) // &lt;1&gt;

This will work if this code runs before the system function to save the issue to the database.

Note that the Checkboxes is a multivalue field which is why the option variable  is passed as an array

Hi Greg, 
Can you try the following code (I assume you store your "Yes" values as 1, you will have to also import com.atlassian.jira.issue.ModifiedValue)


def changeHolder = new DefaultIssueChangeHolder();
followUpField.updateValue(null, issue, new ModifiedValue(followUp, 1 as Integer),changeHolder);


Suggest an answer

Log in or Sign up to answer
Community showcase
Published Feb 07, 2019 in Marketplace Apps

A Timeless Love Story

It started as any story starts, on a normal, rainy day.   Admin meets App, and her name was Klok2, and like any first relationship we were both trying to make it work but neither one knew what...

441 views 8 26
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you