What is the best / fastest way to update a field for all issues matching a condition?
After a bulk data import, we have some temporary fields
i.e. 'Assignee (T)'
I want to update 'Assignee' to myself, where the 'Assignee (T)' field contains my name.
This can be done as a bulk operation, but limited to 1000 Max and there are over 35K. Not all mine, but will need to re-run for various users and with 5 other (T) fields.
I was thinking scriptrunner, is this possible?
Does anyone have a good starting point?
After some more digging, Trial and error in TEST I have a working Solution:
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Level
import org.apache.log4j.Logger
def appUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def linkManager = ComponentAccessor.getIssueLinkManager()
def searchService = ComponentAccessor.getComponent(SearchService.class)
def issueManager = ComponentAccessor.getIssueManager()
def log = Logger.getLogger("Jira Log")
def userManager = ComponentAccessor.getUserManager();
log.setLevel(Level.DEBUG)
def currentUser = userManager.getUserByName("David.Harkins")
def jqlSearch = "KEY = ALOGS-113154"
def NewAssignee = userManager.getUserByName("David.Harkins")
SearchService.ParseResult parseResult = searchService.parseQuery(appUser, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(appUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
def issues = searchResult.results.collect {issueManager.getIssueObject(it.id)}
issues.each { issue ->
log.info("Issue key: "+ issue.getKey())
issue.setAssignee(NewAssignee)
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
log.info("Updated Issue key: "+ issue.getKey())
}
} else {
log.error("Invalid JQL :" + jqlSearch)
}
Hi, Yes, you can do that by running groovy script from script console. You need to first search the issue using JQL and then apply your updates on each issue (set assignee in your case). Check out following code.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.bc.issue.search.SearchService.ParseResult
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Level
import org.apache.log4j.Logger
Logger log = log
log.setLevel(Level.INFO)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
def issueManager = ComponentAccessor.getIssueManager()
def cfm = ComponentAccessor.getCustomFieldManager()
def commentManager = ComponentAccessor.getCommentManager()
def cf = cfm.getCustomFieldObject("customfield_11508") //your CF
log.info("CF Object : "+cf)
def jqlQuery = “YOUR JQL”
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlQuery)
log.info("Parse results: "+parseResult)
List <Issue> issues = null
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.getIssues()
log.info("Search results: "+issues)
} else {
log.error("Invalid JQL: " + jqlQuery);
}
issues.each{
log.info("Issue key: "+it.getKey())
def cfv = cf.getValue(it)
log.info("CF Val : "+cfv)
/***PUT your update CF code here..***/
issueManager.updateIssue(user, issueManager.getIssueObject(it.getKey()), EventDispatchOption.DO_NOT_DISPATCH, false)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Suhas P Thanks :-)
I'm guessing the update code would be as simple as:
issueToUpdate.setCustomFieldValue(cf, "myvalue");
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If its just an assignee the following should work
def userManager = ComponentAccessor.getUserManager();
def user = userManager.getUserByName(userId)
issue.setAssignee(user);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Make sure you try it in non-prod instance first, on small number of issues. confirm the script is working and then use actual JQL in first non-prod and then finally in production.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Making progress :-)
The line
issue.setAssignee(user);
Is causing an error:
2021-04-22 13:51:17,667 ERROR [common.UserScriptEndpoint]: Script console script failed: groovy.lang.MissingPropertyException: No such property: issue for class: Assignee(T)
at Assignee(T)$_run_closure1.doCall(Assignee(T).groovy:51)
at Assignee(T).run(Assignee(T).groovy:44)
issues.each{
log.info("Issue key: "+it.getKey())
def cfv = cf.getValue(it)
log.info("CF Val : "+cfv)
/***PUT your update CF code here..***/
issue.setAssignee(NewAssignee);
log.info("Updated Issue key: "+it.getKey())
issueManager.updateIssue(user, issueManager.getIssueObject(it.getKey()), EventDispatchOption.DO_NOT_DISPATCH, false)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
aha, it should be :)
it.setAssignee(user)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have to change it to:
Issue.setAssignee(NewAssignee)
As
Issue.setAssignee(user)
'User' was being set as the current user to run the JQL, so would assign all issues to the same user.
I set the following line:
def NewAssignee = userManager.getUserByName("David.Harkins")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Current full script is:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.bc.issue.search.SearchService.ParseResult
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Level
import org.apache.log4j.Logger
Logger log = log
log.setLevel(Level.INFO)
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.getIssueManager()
def issueService = ComponentAccessor.getIssueService()
def userManager = ComponentAccessor.getUserManager();
def NewAssignee = userManager.getUserByName("David.Harkins")
def jqlQuery = "KEY = ALOGS-113154"
SearchService.ParseResult parseResult = searchService.parseQuery(currentUser, jqlQuery)
log.info("Parse results: "+parseResult)
List <Issue> issues = null
if (parseResult.isValid()) {
def searchResult = searchService.search(currentUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.getResults()
log.info("Search results: "+issues)
} else {
log.error("Invalid JQL: " + jqlQuery);
}
issues.each{
log.info("Issue key: "+it.getKey())
Issue.setAssignee(NewAssignee)
ComponentAccessor.getIssueManager().updateIssue(currentUser, Issue, EventDispatchOption.ISSUE_UPDATED, false)
log.info("Updated Issue key: "+it.getKey())
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
you still need to change issue.setAssignee to it.setAssignee. Here 'it' is an issue object from the list of issues search result.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Also replace Issue with it in
ComponentAccessor.getIssueManager().updateIssue(currentUser, Issue, EventDispatchOption.ISSUE_UPDATED, false)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I had tried that before :-(
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.DocumentIssueImpl.setAssignee() is applicable for argument types: (com.atlassian.jira.user.DelegatingApplicationUser) values: [David.Harkins(JIRAUSER20502)]
Possible solutions: getAssignee(), getAssigneeId()
at Assignee(T)$_run_closure1.doCall(Assignee(T).groovy:42)
at Assignee(T).run(Assignee(T).groovy:39)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You are passing wrong argument to the method setAssignee. It should be passed object of type ApplicationUser. Here comes the real troubleshooting that you need to do in parts. Open another script console and run the part of the script (without update issue part. Add few more log statements and check whats the output of userManager.getUserByName and type of NewAssignee. I think you have entire code you need, just need to troubleshoot and run :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.