Map Custom Field Values From One Field To Another

cdemattio January 17, 2017

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

1 accepted

0 votes
Answer accepted
Vasiliy Zverev
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 Leaders.
January 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