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

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 Join to answer
Community showcase
Emilee Spencer
Published 6 hours ago in Marketplace Apps

Marketplace Spotlight: DeepAffects

Hello Atlassian Community! My name is Emilee, and I’m a Product Marketing Manager for the Marketplace team. Starting with this post, I'm kicking off a monthly series of Spotlights to highlight Ma...

30 views 0 3
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot