Display last comment using Script Runner

Jan Sękara May 11, 2017

Hi,

I need to display last comment in customfield using Groovy Script.

Anyone done that?

6 answers

1 accepted

0 votes
Answer accepted
Daniel Yelamos [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 19, 2017

Hello Jan:

In this question you will find an asnwer on how to get a comment from an Issue Object. Afterwards you can iterate to find the latest one, or find the one with the latest date.

If this doesn't work for you, let us know and I'll program a 1 on 1 meeting with you to get it working.

Cheers!

13 votes
Tukaram Bhukya June 2, 2017

Put below code under custom "scripted field" 

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()

def comment = commentManager.getLastComment (issue)

comment.body 

Jan Sękara June 8, 2017

Thanks

 

Ash
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 17, 2017

Hi Tukaram,

 

is it possible to add user name who commented on this along with comment?.

Tukaram Bhukya July 17, 2017

Yes , use below code

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()

def comment = commentManager.getLastComment (issue)

comment.body + " Author: " + comment.authorFullName

Like # people like this
Ash
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 18, 2017

Thank you, Tukaram.

Marc Moroz March 29, 2018

Thanks, Tukaram

Ryan L October 1, 2018

Hello, 


First of all thank you, however how can I add error handling on it? when an issue is created the field starts throwing up a lot of error messages in the logs as there is no comment yet. 

Like vasanth likes this
IT Admin October 1, 2018

Try below one

def comment = commentManager.getLastComment (issue)

if(comment != null)
{
comment.body + " Author: " + comment.authorFullName
}

Like # people like this
Katya Godneva February 19, 2019

Thanks, that worked beautifully.

Like vasanth likes this
Martin Pittner August 23, 2019

It works like a charm! Thank you very much Tukaram Bhukya.

Ryan_McAlevey September 16, 2019

Is there a way to show only the last internal comment?

Like Markus Pöhler likes this
Mark Crocker February 13, 2020

I can't see a scripted field option? How do I make this work with a Script Listener to populate a custom field I made called: "Last Comment"

Joey Klein May 4, 2020

this worked great for me.  is there a way to break up the  "+ comment.authorFullName" to just show first name instead of 'last,first' ?

0 votes
Roshan Fernando June 26, 2023

Hi,

Just want to see is there way to show the last comment without any inline images in the comment body? Thanks.

0 votes
Jacob Francois November 3, 2022

Is it possible to have this script only show the last comment of the reporter or the assignee ? 

0 votes
Itamar Ben Sinai January 24, 2022

Small update

Replace 

import com.atlassian.jira.ComponentManager

with 

import com.atlassian.jira.ComponentAccessor

 

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()

def comment = commentManager.getLastComment (issue)

comment.body 

0 votes
Tihomir Nikolov November 29, 2017

Hi, Any idea if and how can I re-render the wiki style render ?

Lots of comments contain formatting and user mentions , new lines, bullets are not formatted at all.

Ryan L October 2, 2018

Try this, it works but i found that my Database usage spiked after using it. 

https://community.atlassian.com/t5/Jira-questions/Scripted-Field-to-copy-latest-comment/qaq-p/51850

Ramaiah Pendli November 10, 2022

can we get latest comment to the previous comment details. 

Like vasanth likes this

Suggest an answer

Log in or Sign up to answer