Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

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
issueIDs.each{
    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
    if(currentCFValue)
    {
        //define the comment
        def valueToComment = "$currentCustomField.name: \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 

Hi Adrian,

This is very useful. Can this be adapted to be able to update in a single comment values from multiple custom fields that script will receive as input? 

If not too much trouble, can you please update the script? It will help me in my current task.

Thank you!

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.

Cheers!

DYelamos

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

684 views 7 23
Read article

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