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

1 vote

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

Regards, 
Petar 

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

82 views 0 5
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