Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,360,942
Community Members
 
Community Events
168
Community Groups

Trying to copy the last EXTERNAL comment to a custom field

We're using this script to set up a custom field called Last comment (script field):

/**
* Field name: Last Comment
* Template: Text Field (multi-line)
*
* Description:
* Returns the last comment on an issue.
*
*/

import com.atlassian.jira.component.ComponentAccessor

// get the latest comment
def commentManager = ComponentAccessor.getCommentManager()
def comment = commentManager.getLastComment(issue)

// check to see if comments exist - return comment body if so
if (comment != null) {
return comment.body
}

return null

This returns the last comment, whether it's internal or external. 

We need this script to return ONLY the last EXTERNAL comment.

I found this link; https://community.atlassian.com/t5/Jira-Service-Management/Scriptrunner-Latest-public-comment-in-custom-email/qaq-p/1406868

But we get an error:

groovy.lang.MissingPropertyException: No such property: transientVars for class: Script8473 at Script8473.run(Script8473.groovy:7)

How can we set transientVars as a script field?

1 answer

1 accepted

2 votes
Answer accepted

Hi @Samuel Carter

For your requirement, it would be best to use the ScriptRunner Listener with the Issue Commented event.

Below is a working sample code for your reference:-

import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.Comment
import groovy.json.JsonSlurper

final SD_PUBLIC_COMMENT = 'sd.public.comment'

def issue = event.issue as MutableIssue
def issueEvent = event as IssueEvent
def user = issueEvent.user
def comment = issueEvent.comment

def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)


def sampleMultiLine = customFieldManager.getCustomFieldObjectsByName('Sample Multi Line Text').first()

def isInternal = { Comment c ->
def commentProperty = commentPropertyService.getProperty(user, c.id, SD_PUBLIC_COMMENT).entityProperty.orNull

if (commentProperty) {
def props = new JsonSlurper().parseText(commentProperty.value) as Map
props['internal'].toString().toBoolean()
}
else {
null
}
}

if (comment && !isInternal(comment)) {
issue.setCustomFieldValue(sampleMultiLine, comment.body)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
return false

Please note that the sample code provided is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Listener Configuration for your reference:-

listener_config.png

Below are a few test screenshots for your reference:-

1. When I first add a new public comment, the comment is copied to the text field as shown below:-

test1.png

2. If I try to include a private comment, the text field is not updated as expected

test2.png

3. If I add a new public comment, the text field is updated as expected.

test3.png

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Hi Ram,

I've implemented this code as a listener and it seems to do exactly as we require.

 

Thanks so much for your help!

Kind regards,

Sam

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

An unofficial way to monitor a JSM mail handler for errors

...eturns true if any content is returned for the webResponse.body.data.first s...

688 views 2 20
Read article

Atlassian Community Events