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?