Missed Team ’24? Catch up on announcements here.

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

Trying to copy the last EXTERNAL comment to a custom field

Samuel Carter September 15, 2022

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
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 17, 2022

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

Samuel Carter September 20, 2022

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
AUG Leaders

Atlassian Community Events