Map Custom Field Values From One Field To Another

Hello,

We have a JIRA instance which has many administrators.  As a result, we often have situations where users have created custom fields which have the same names.

I would like to merge similar custom fields into a single field.  To do so, I need to be able to map the current values in the fields to be merged into the new values of the new merge field.

I thought I could do this by writing individual queries to locate issues that use a particular old field value and, then, use a Bulk Update to set the new field value.  Once all of the values have been mapped into the new field, I can delete the old, redundant fields. It's a manual process, but it's doable because the number of old field values is not too great.

Unfortunately, the Bulk Update doesn't work because some of the issues are in a state that does not allow further updates.

I was thinking that the script runner may help.   I noticed that there is a script runner script which copies values from one custom field to another.  I'm assuming that script will bypass the restrictions against changing issues.  How hard would it be to extend that script to perform a mapping operation instead of a direct copy?  Is that something that anyone has considered?

(NOTE: I'm a complete script runner newbie, so the more detail, the better.  Thanks!) 

1 answer

0 vote
Vasiliy Zverev Community Champion Jan 17, 2017

Here you are. Define custom field names and provide issue keis list. Magic! and all done!

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager
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

IssueManager issueManager = ComponentAccessor.getIssueManager();
CustomField cf_from = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("custom_from_name")
CustomField cf_to = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("cf_to_name")

MutableIssue issue;
UpdateIssueRequest request = UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build();
ApplicationUser curUser = ComponentAccessor.getJiraAuthenticationContext().getUser()

for(String issueKey: getIssues()){
    issue = issueManager.getIssueObject(issueKey);
    issue.setCustomFieldValue(
            cf_to,
            issue.getCustomFieldValue(cf_from)
    )
    issueManager.updateIssue(
            curUser
            , issue
            , request
    )
}

//place issue keyis list here
List<String> getIssues(){
    return Arrays.asList(
        "key1"
        , "key2"
    )
}

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

56 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you