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

hutching August 6, 2016

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

6 votes
JamieA
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.
August 7, 2016

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.

MattS
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 12, 2016

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

JamieA
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 12, 2016
0 votes
Peter Geshev
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.
August 7, 2016
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
TAGS
AUG Leaders

Atlassian Community Events