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")
log.setLevel(org.apache.log4j.Level.DEBUG)
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 ->
    //log.debug(documentIssue.key)
    def issue = issueManager.getIssueObject(documentIssue.id)
    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Frequency of Occurrence"}
    log.debug("-----------------------")
    log.debug(issue)
    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); 
    //log.debug(newOption)
    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.

0 vote

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

0 vote

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

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
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

222 views 1 18
Join discussion

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