Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Updating a field for all issues matching a condition

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?

2 answers

1 accepted

0 votes
Answer accepted

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)




} 

 

 

@Suhas Patil Thanks :-)

I'm guessing the update code would be as simple as:

issueToUpdate.setCustomFieldValue(cf, "myvalue");

If its just an assignee the following should work 

def userManager = ComponentAccessor.getUserManager();
def user = userManager.getUserByName(userId)


issue.setAssignee(user);

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.

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

aha, it should be :)

it.setAssignee(user)

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

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 still need to change issue.setAssignee to it.setAssignee. Here 'it' is an issue object from the list of issues search result.

Also replace Issue with it in 

ComponentAccessor.getIssueManager().updateIssue(currentUser, Issue, EventDispatchOption.ISSUE_UPDATED, false)
Like David Harkins likes this

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

ThinkTilt is joining the Atlassian Family!

This morning, Atlassian announced the acquisition of ThinkTilt , the maker of ProForma, a no-code/low code form builder with 700+ customers worldwide. ThinkTilt helps IT empower any team in their or...

829 views 26 33
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you