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

Deleted user August 18, 2015

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

1 vote
Answer accepted
GabrielleJ
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 19, 2015

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?

Deleted user August 19, 2015

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

Deleted user August 21, 2015

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

0 votes
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.
August 19, 2015

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

0 votes
Deleted user August 19, 2015

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 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 18, 2015

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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events