Updating custom field in issues using script runner doesn't work

My goal: Search for a list of issues. For all issues, update a custom field value to a new value.

The custom field is a select list called "Frequency of Occurrence" with values "Rarely" and "Always".


My code appears to work. It modifies all issues returned by the query which have custom field "Frequency of Occurrence" with value "Rarely" to "Always". When I click on those issues, the page correctly displays "Always".

However, when I re-do the old query in JIRA it still returns the old list. I.e. earching "Frequency of Occurrence" = Rarely still brings up 200 issues, but when you click on the issue the value is set correctly to "Always".


Here is the groovy script I put together based on other examples:

import org.apache.log4j.Category
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.issue.customfields.manager.OptionsManager
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
// The search query
def query = jqlQueryParser.parseQuery("\"Frequency of Occurrence\" = Rarely")

def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())
results.getIssues().each {documentIssue ->
    def issue = issueManager.getIssueObject(documentIssue.id)
    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Frequency of Occurrence"}
    log.debug("Current Value: " + issue.getCustomFieldValue(tgtField))
    def customField = customFieldManager.getCustomFieldObject("customfield_11941");
	def optManager = ComponentAccessor.getOptionsManager()
    def options = optManager.getOptions(customField.getRelevantConfig(issue))
    def newOption = options.getOptionById(11335); 
    tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), newOption),new DefaultIssueChangeHolder());


Any ideas why this isn't working?

4 answers

1 accepted

It might need a re-index since it's a custom field change. Where did you run this script? In a transition? Is the event "index issues automatically" in there?

Hmm, good point. I'll try a reindex. I ran it in the Script Console

The reindex did the trick for me. I wasn't able to post earlier since my account was restricted.

Looks OK. When you uncomment the log line, do you get the right output for newOption? Also you should check that tgtField is present before you go on and try to set it.

Yep, newOption contains the right option. I also did test tgtField, but cleaned up the script before posting it here. It had quite a few log.debug()-s in there :) Regardless, I figured out what the issue was. A reindex fixed it for me (as suggested by Gabrielle).

I think many examples have a line after updateIssue to reindex just that issue

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,416 views 15 19
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