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

Aitor Bermejo Cacho March 25, 2019

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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 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())
}
}
}
}
Aitor Bermejo Cacho March 27, 2019

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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 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 ?

Aitor Bermejo Cacho March 27, 2019

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

Aitor Bermejo Cacho March 27, 2019

Well, I modify jqlsearch, userkey, sorucestatus and targetstatus

testscript.PNG

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 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"
Aitor Bermejo Cacho March 27, 2019

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

Aitor Bermejo Cacho March 27, 2019

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

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 27, 2019

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

Aitor Bermejo Cacho March 27, 2019

just a problem with userkey ;P.

Suggest an answer

Log in or Sign up to answer