need a script to copy a custom field to comments with scriptrunner

JIRA - com.onresolve.jira.groovy.groovyrunner

We want to eliminate a custom field and need to get its value added to the comments on each issues.  The custom field is a multi-line text field.  We have over 8,000 issues, each with unique values in the custom field and need to update all of them at the same time.  We would like to have the name of the field prepended to the field value in each comment.

2 answers

1 accepted

Hey Michael, we meet again!

There are really multiple ways that you can approach this issue, but what I've done is created a script that runs through all of the issues in a specified project.

I set it up this way so that you could test this script project-by-project to make sure that it's doing what you expect it to, and to ensure that you don't make too large of a change to your instance in one go. But you are completely welcome modify my script to simply go through every issue in your JIRA instance if you would like. 

You will find the script below:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

//Grab necessary components
def projectManager = ComponentAccessor.projectManager
def issueManager = ComponentAccessor.issueManager
def cfm = ComponentAccessor.customFieldManager
def commentManager = ComponentAccessor.commentManager

//Get the project you wish to run this script on and retrieve all of the relevant issues
def projectID = projectManager.getProjectByCurrentKey("Project Key").id
def issueIDs = issueManager.getIssueIdsForProject(projectID)
//Get logged in user that will act as the commenter
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

//Go through every issue ID and transfer the desired text custom field to a comment
    def currentIssue = issueManager.getIssueObject(it)
    def currentCustomField = cfm.getCustomFieldObjectByName("Custom Field Name")
    def currentCFValue = currentIssue.getCustomFieldValue(currentCustomField)

    //Check for null custom field on the current issue
        //define the comment
        def valueToComment = "$ \n $currentCFValue"

        //Add the comment to the issue, delete the custom field value, and persist those changes to the DB
        commentManager.create(currentIssue, loggedInUser, valueToComment, true)
        currentIssue.setCustomFieldValue(currentCustomField, null)
        issueManager.updateIssue(loggedInUser, currentIssue, EventDispatchOption.ISSUE_UPDATED, false)

When writing this script, I worked under the assumption that every issue in the project had this custom field present on it. However, I do some checking in the closure to skip the step of commenting if the custom field is actually null. 

To run this, be sure to replace the "Project Key" and the "Custom Field Name" parameters where necessary. Then copy and past it into your Script Console and run to your hearts content! :)

Let me know if you have any additional questions!

Hope that does the trick!                                                                          Aidan 

Hello Michael.

This seems like a trivial script to me.

First of all you need a list of all your issues that you need to migrate, and the user which will be posting the comment, which I suppose should be an admin user.

You should create a closure that runs through each issue, and fetches the customField value like this:

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_id")
def cFieldValue = issue.getCustomFieldValue(cField)

Then you should store the Name of the customField and its value in a single string like this:

def comment = "[CustomFieldName] : " + cFieldValue 

After that, you have your comment, and you can add a comment to a function as this user posts in this community question.

Please do tell if you need further assistance.



Suggest an answer

Log in or Sign up to answer
Community showcase
Published Wednesday in Marketplace Apps

Marketplace Spotlight: Marketing apps for Confluence to keep your teams working on the same page


199 views 0 4
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