Hello,
I want to save the person who transition a ticket in a custom field but I need to do for our existing tickets...
How can I do with script console?
Thanks all for your help!
Hi,
That would be possible if source and target statuses are sufficient to identify the transition since you cannot retrieve the transition name in history manager.
Update the JQL request, user_key, sourceStatus and targetStatus (you can use id if needed).
Let me know if you need help updating the custom field.
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.web.bean.PagerFilter
// Your jql
jqlSearch = "key in (ABC-123, ABC-456)"
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
def userManager = ComponentAccessor.getUserManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()
ApplicationUser user = userManager.getUserByKey('user_key')
List<Issue> issues = null
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)}
} else {
log.error("Invalid JQL: " + jqlSearch);
}
def changeHistoryManager = com.atlassian.jira.component.ComponentAccessor.getChangeHistoryManager()
for (Issue issue:issues){
def ch = changeHistoryManager.getChangeHistories(issue)
for(int i=ch.size() - 1; i >= 0; i--){
for (ChangeItemBean bean:ch.get(i).getChangeItemBeans()){
if (bean.getField() == "status" && bean.getFromString() == "sourceStatus" && bean.getToString() == "targetStatus"){
log.error("getAuthorDisplayName() : " + ch.get(i).getAuthorDisplayName())
log.error("getAuthorKey() : " + ch.get(i).getAuthorKey())
}
}
}
}
ji @Antoine Berry ,
thank you for your answer. Just one question...I see an error in the code but I don't really know who to fix...in issues=searchResult.issues.collect {issueManager.getIssueObject(it.id)}
has this error: [static type checking] - incompatible generic argument types. Cannot assign java.util.List <com.atlassian.jira.issue.MutableIssue> to: java.util.List<issue>
you know how to fix it?
so thank you!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
I tried this code and it has been working fine. Are you getting the error when you run the code or is it highlighting 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.
Highligh in the script console. But I try without modify anything in the code but dont return anything in logs....
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Well, I modify jqlsearch, userkey, sorucestatus and targetstatus
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Absolutely, that is what you should modify !
Do not worry about the highlights in script console, they are often not blocking (it is groovy and things are not always declared so it is suspicious).
I would advise to start with a very simple JQL (key = ABC-123) and use an issue you know will work. Also, check for the source status and target status case, it has to be exactly the status name.
If you are not sure you can use
bean.getToString().toLowerCase() == "status in lower case"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If I add a wrong JQL, the script show the error in logs, but dont so anything in log about the transition....
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.
You're welcome ! I guess it was the status names that were in upper cases ?
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.
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.