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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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.

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
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 :) 

Suggest an answer

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

New feature: Slack notifications for Statuspage

We're excited to announce the release of a long-requested feature on Statuspage. Now visitors to your status page can subscribe to get notified in Slack when you report an incident or maintenance. Th...

51 views 0 8
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you