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,589
Community Members
 
Community Events
168
Community Groups

Use Scriptrunner to email body of comment after Issue Commented

Edited

Hi, so I'm inexperienced and don't know where to look. Basically I am setting up emails to trigger when a comment is left on a particular Project. I would like the emails to have the body text of the comments left.

I am using Scriptrunner and setting an event with Script Listeners. I can successfully call the JIRA issue into the subject line via $issue, a la:

Issue $issue requires changes

In the body of the email, I can use ${issue.description} to pull in the entire description, but I'm interested in the comments. I've shot in the dark with ${issue.comments} and ${getComments($issue).last()} and the like, but I realize I need documentation and help. Could someone point me in the correct direction?

4 answers

2 accepted

1 vote
Answer accepted

Hi Jonathan

Try the following in the email template

<%
def comments = com.atlassian.jira.component.ComponentAccessor.commentManager.getComments(issue)?.collect {
    "Comment body: ${it.body} | Author: ${it.authorFullName} | Created: ${it.created}"
}?.join("<br>")
%>

$comments

It will add in the email all the comments for the particular issue. 

Please let me know if this does the trick.

regards, Thanos

Thanks, Thanos! That definitely works wonders, and I love that I can have all the components in order to reformat as I like!

"Author: ${it.authorFullName}\n  Comment body: ${it.body}\nCreated: ${it.created}"

Author: Davidson, Jonathan
  Comment body: What I want changed is this.... blah
Created: Tue May 09 11:29:32 EDT 2017

I marked this as the answer because you are awesome, but if possible, could you expand on how to select only particular comments? If there are multiple comments, your .collect() grabs them all and then spits out the format provided for each one, with the join() stringed between them.

What if I want to select a particular one or the last one?

For anyone interested in the last comment, this also appears to work for pulling only the last comment:

def comments = commentManager.getComments(sourceIssue)

comments.last().body

Hi Johnathan, I also need the last comment. I've tried your suggestion but I can't get it to work. Since I have very little experience scripting, would you be able to give me the exact code to put in my email template to pull only the last comment in an issue?

Hi Randy,

Ultimately I set the comments into a newly created Issue's description field, rather than sending via email:

import com.atlassian.jira.component.ComponentAccessor
def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(sourceIssue)
if (comments) {
    def desc = sourceIssue.description
issue.description = '*Manager comment:* ' + comments.last().body + '\r\n \r\n \r\n----\r\n' + desc } else { issue.description = "No comment set upon transitioning task" }

I can't help you troubleshoot, but probably wrap it up in tags and then call whatever you name the variable.

Best,

Jonathan

0 votes
Answer accepted
<%
def comments = com.atlassian.jira.component.ComponentAccessor.commentManager.getComments(issue).last()?.collect {
    "Comment body: ${it.body} | Author: ${it.authorFullName} | Created: ${it.created}"
}?.join("<br>")
%>

$comments

Think it's the addition of .last() in the code that Thanos provided

Thanks Jonathan, that definitely helps and I'm able to include the comments in email notifications that gets sent as a post-function. One thing, an issue with no comments prevents the email notification from ever going out. Any ideas on that? Thanks again for your help.

0 votes
Monique vdB Community Manager Jul 12, 2017

@Jonathan Davidson I'm sorry some of your posts got caught in our spam quaratine, I have released them all now. 

Thanks @Monique vdB, I apologize that there's now a lot of similar posts with approximately the same content. Can I delete them? When editing them, I only see the option to change text, not to delete the post altogether.

Monique vdB Community Manager Jul 13, 2017

@Jonathan Davidson thanks for changing the text -- now it's obvious which ones to delete. I can do that for you :) 

Here's the code in cloning an issue, if you want the last comment, which is where I'd ultimately ended up by the time I wrote my last reply:

import com.atlassian.jira.component.ComponentAccessor
def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(sourceIssue)
if (comments) {
    def desc = sourceIssue.description 
    issue.description = '*Manager comment:* ' + comments.last().body + '\r\n \r\n \r\n----\r\n' + desc
}
else {
    issue.description = "No comment set upon transitioning with Remove access prompt"
}

Suggest an answer

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

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

196 views 1 6
Read article

Atlassian Community Events