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

This widget could not be displayed.

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

This widget could not be displayed.
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);
 

Regards, 
Petar 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

477 views 3 17
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