Adding a comment when a custom field changes to a certain value

Charles de Beaux February 10, 2020

Honestly not really sure where to start here. Had a look at writing a custom listener but wasn't sure how to get context for the issue. The listener would need to rigger on CustomFieldUpdatedEvent events and add a comment to the issue that contained the assignee and the current user. The customfield is a single choice dropdown list and I only want the comment added when it is set to a specific value. Any help would be greatly appreciated! Thanks

1 answer

0 votes
Florian Edian February 10, 2020

Hi Charles,

Have you looked into solution like Automation for Jira ?

Regards,
Florian

Charles de Beaux February 11, 2020

Thanks for a quick reply.

Looked into it yes, however, I already have a scriptrunner license and set up (with some other scripts) so I was looking for a way of achieving this with scriptrunner as well.

Florian Edian February 20, 2020

Hi Charles,

I've just found this script from Aidan Derossett on another Community post (https://community.atlassian.com/t5/Marketplace-Apps-Integrations/need-a-script-to-copy-a-custom-field-to-comments-with/qaq-p/606253.)

It might help.

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)
    }
}

Regards,
Florian

Suggest an answer

Log in or Sign up to answer