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
Accepted answer

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
Accepted answer
<%
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.

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"
}
0 votes

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

Thanks @Monique van den Berg, 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.

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Friday in Jira Service Desk

Looking for anyone who has switched from Zendesk to Jira Service Desk

Hi Community! The Jira Service Desk marketing team is looking for customers who have successfully switched from Zendesk to Jira Service Desk!   We’d love to hear your thoughts on the pros and ...

36 views 0 1
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you