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

How can I save this data in Custom field? [script runner]

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!

1 answer

1 accepted

0 votes
Answer accepted
Antoine Berry Community Leader Mar 25, 2019

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!

Antoine Berry Community Leader Mar 27, 2019

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 ?

Highligh in the script console. But I try without modify anything in the code but dont return anything in logs....

Well, I modify jqlsearch, userkey, sorucestatus and targetstatus

testscript.PNG

Antoine Berry Community Leader Mar 27, 2019

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"

testscript.PNGIf I add a wrong JQL, the script show the error in logs, but dont so anything in log about the transition....

Ok, I fixed. So thank you for your help!!!!

Antoine Berry Community Leader Mar 27, 2019

You're welcome ! I guess it was the status names that were in upper cases ?

just a problem with userkey ;P.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Calling all Jira Cloud users! Give us feedback on our exploration of a new navigation.

Hi everyone! My name’s Matt and I’m a product manager at Atlassian. I work in the navigation & findability space for all our Jira Cloud products. We’ve been working on trying to improve the exp...

1,060 views 16 12
Join discussion

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