I'm totally new to ScriptRunner and have been tasked to search through a project's issues' change history and find where a now-deleted old custom field was set and set the value to a brand new custom field. Can anyone help me on this?
Thanks!
The below code was tested on JIRA Server V8.20.8,
import com.atlassian.jira.component.ComponentAccessor;import com.atlassian.jira.issue.ModifiedValueimport com.atlassian.jira.issue.util.DefaultIssueChangeHolderimport com.atlassian.jira.issue.Issueimport com.atlassian.jira.bc.issue.search.SearchServiceimport com.atlassian.jira.jql.parser.JqlQueryParserimport com.atlassian.jira.web.bean.PagerFilter//Load needed Managersdef issueManager = ComponentAccessor.getIssueManager()def customFieldManager = ComponentAccessor.getCustomFieldManager()def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()//Load JQL Managersdef jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)def searchService = ComponentAccessor.getComponent(SearchService)def user = ComponentAccessor.getJiraAuthenticationContext().getUser()//Get your project issues based on with JQLdef query = jqlQueryParser.parseQuery("Your JQL Here ")def results = searchService .search(user , query, PagerFilter.getUnlimitedFilter())//Loop on Issues to set the value of the new custom field based on the old one valueresults.getResults().each{item ->def issue = issueManager.getIssueObject(item.key)// Load the new custom field object of the current issuedef newCustomField = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Your New Custom Field Name'}// Get the latest value of the old custom field of the current issuedef oldCustomFieldHisotry = changeHistoryManager.getChangeItemsForField(issue, "Your Old Custom field Name").sort{it.getCreated()}def oldCustomFieldValue = oldCustomFieldHisotry.size()>0?oldCustomFieldHisotry.last().to:null//Set the new custom field with the old custom field valueif(oldCustomFieldValue != null){newCustomField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(newCustomField), oldCustomFieldValue),new DefaultIssueChangeHolder())}}
Thank you! I'll try it out.
@Brian I'm having the exact issue. I tried above code but it did nothing.
Did you find the solution?
Did it do *nothing*, or did it throw an error?
Here's a version of the script with a lot of logging thrown in. Can you tell us what the log says?
Did anyone figure out how to accomplish this in Jira Cloud? I tried using the original script but the "imports" aren't a thing in cloud.
It looks like you're new here. Sign in or register to get started.