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

Copy Custom field value from one field to another

Pushpkant Garg Dec 01, 2016

My requirement is to copy Text field data into USer picker field. Text Field data has single email address.

When I used script runner "Copy custom field" option it gave me type casting error. 

Can anyone please help me with groovy script which will help me to update the same. We have around 30k+ records which need to be updated.

3 answers

2 votes
Jonny Carter Dec 07, 2016

To expand on @Vasiliy Zverev's answer a bit, you could get the results of a JQL query in a console script, then iterate through the list of values to update them.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.web.bean.PagerFilter

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.getIssueManager()
def jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext()
def user = jiraAuthenticationContext.getUser()

// edit this query to suit
def query = jqlQueryParser.parseQuery("summary is not EMPTY")

def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())

log.debug("Total issues: ${results.total}")


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfUser = customFieldManager.getCustomFieldObjectByName("userFiledName")
def cfEmail = customFieldManager.getCustomFieldObjectByName("emaiFiledName")

def emailUserMap = ComponentAccessor.getUserManager().getAllApplicationUsers().collectEntries {
    [(user.getEmailAddress()): user]
}

results.getIssues().each { documentIssue ->
    log.debug(documentIssue.key)

    // if you need a mutable issue you can do:
    def issue = issueManager.getIssueObject(documentIssue.id)

    issue.setCustomFieldValue(cfUser, emailUserMap.get(issue.getCustomFieldValue(cfEmail)))
    UpdateIssueRequest updateIssueRequest = UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build()

    issueManager.updateIssue(user,
        issue,
        updateIssueRequest)
    
    // do something to the issue...
    log.debug(issue.summary)
}
0 votes
Nic Brough [Adaptavist] Community Leader Dec 01, 2016

You've answered your own question really.  Fields have to contain compatible data types for a simple copy to work.  A text string is not a user object.

You'll need to write a bit of code that can look up the email address, and find a user  with that email address (you'll need to check for duplicates with a rule, even if it's just "the first one I find")

 

0 votes
Vasiliy Zverev Dec 01, 2016

Here is script for script console to update single issue

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser

Map<String, ApplicationUser> emailUserMap = new HashMap<>();
for(ApplicationUser user: ComponentAccessor.getUserManager().getAllApplicationUsers()){
    emailUserMap.put(user.getEmailAddress(), user)
}

CustomField cfUser = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("userFiledName")
CustomField cfEmail = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("emaiFiledName")

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issueKey");

issue.setCustomFieldValue(
        cfUser
        , emailUserMap.get(issue.getCustomFieldValue(cfEmail))
)

UpdateIssueRequest updateIssueRequest = UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build()

ComponentAccessor.getIssueManager().updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser()
        , issue
        , updateIssueRequest
)

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

774 views 7 0
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