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?
Hmm, good point. I'll try a reindex. I ran it in the Script Console
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The reindex did the trick for me. I wasn't able to post earlier since my account was restricted.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think many examples have a line after updateIssue to reindex just that issue
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.