How to I set a custom field's value to the latest comment?

Randy
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.
December 5, 2016

I'm trying to store the latest comment in a custom field so that I can easily surface it in confluence.  I'm on JIRA Cloud with SR.

How would I get the latest comment in an issue so that i can set it to a custom field in my ScriptRunner Listener that runs on Create and Update?

3 answers

1 accepted

0 votes
Answer accepted
Randy
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.
December 7, 2016

Thanks for the solution Vasiliy

Since I'm on Cloud, I had to go through the API.

To retrieve i used this call: /rest/api/2/issue/${issue.key}/comment

Mark Crocker February 14, 2020

Can you elaborate how you did this?

0 votes
Thomas Opiolka
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.
February 5, 2019

To get first or last comments (and more comment related fields) as a synchronised custom field you can also use the cloud App Comment Custom Fields for Jira.

The fields build at installation and will stay in sync from then on.

The answer is a bit late, but maybe someone else needs an easy scriptless solution

Disclaimer: I'm the product manager of said App.

0 votes
Vasiliy Zverev
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.
December 6, 2016

You could use script field. Here is code to get last comment (test it before use):

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

Issue issue

CommentManager commentManager ;
List<Comment> commentList = commentManager.getComments(issue)

if(commentList.size() == 0)
    return null

commentList.sort(new Comparator<Comment>() {
    @Override
    int compare(Comment o1, Comment o2) {
        if(o1.getCreated().before(o2.getCreated())) return -1
        else return 1
    }
})

return commentList.get(0).getBody()

Suggest an answer

Log in or Sign up to answer