The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
  • Community
  • Q&A
  • Jira
  • Questions
  • bulk copy custom field values from text field to number field using scriptrunner through JQL filter

bulk copy custom field values from text field to number field using scriptrunner through JQL filter

Azfar Masut
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
September 17, 2020

I want to bulk copy using JQL, a text field to a number field using scriptrunner

The OOB built in scriptrunner bulk copy function doesnt support text to number field

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

1 vote
Answer accepted
Azfar Masut
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
September 17, 2020

here's how, use this in the script console on scriptrunner side:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.search.SearchQuery
import com.atlassian.jira.issue.MutableIssue


log.warn("--- Start script -----------------------------------")

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager

//sourceCF
def customFieldOld = customFieldManager.getCustomFieldObject(13304)
//destinationCF
def customFieldNew = customFieldManager.getCustomFieldObject(15401)
def issueChangeHolder = new DefaultIssueChangeHolder()

// edit this JQL query for issue set that you want to change
def query = jqlQueryParser.parseQuery("cf[13304] is not EMPTY")
def searchResults = searchProvider.search(SearchQuery.create(query, user), PagerFilter.unlimitedFilter)

log.warn("Total issues: ${searchResults.total}")
searchResults.results.each { documentIssue ->
def key = documentIssue.document.fields.find { it.name() == "key" }.stringValue()
def issue = issueManager.getIssueObject(key)

//parsing from string of customFieldOld to double
def newValue = Double.parseDouble(issue.getCustomFieldValue(customFieldOld).toString())

//set new field with newValue int
customFieldNew.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customFieldOld), newValue), new DefaultIssueChangeHolder())
}

log.warn("------------------------------------------- End script ---")