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

ScriptRunner workflow function - Send a custom email - Send last comment

Hi,

I am trying to send at least the last comment in an email on transition.

I have tried the 3 solutions from :   https://jamieechlin.atlassian.net/wiki/spaces/GRV/pages/33030148/Built-In+Scripts#Built-InScripts-CommentsinEmails

But every time, if there is only one comment, the email sent doesn't include the comment. (I tried lastComment, mostRecentComment & latestPriorComment)

And if I create a second comment, it's sending the last comment as expected.

I can't find a way to send the comment if there is only one.

 

Any clue?

Dear $issue.reporter.displayName,

Your request has been refused for the following reason :
<% if (lastComment)
out << "Last comment: " << lastComment
%>

Don't hesitate to contact us for more explanation.

Best regards,

3 answers

1 accepted

0 votes
Answer accepted

I finally figured it out. Still combine your solution with mine. Here is what I did (I had to change "def lastComment" by "config.lastComment") :

For "Condition and Configuration" on the send custom email :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.ApplicationUser

CommentManager commentMgr = ComponentAccessor.getCommentManager()

config.lastCustomComment = commentMgr.getComments(issue)?commentMgr.getComments(issue).last().body: ""

 

and for email template :

Dear $issue.reporter.displayName,

Your request has been refused for the following reason :
<% if (mostRecentComment)
out << mostRecentComment
else
out << lastCustomComment
%>
Don't hesitate to contact us for more explanation.

Best regards,
0 votes

Hi @Arthur SALMON 

You can fetch last comment using something like this.

CommentManager commentMgr = ComponentAccessor.getCommentManager()
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().loggedInUser

def lastComment = commentMgr.getComments(issue)?commentMgr.getComments(issue).last().body: ""

You can take a look at this page to get more idea about how to fetch the last comment. There is a code there. Just modify the bits you don't need and use it in the post function.

Ravi

Thanks for the quick reply.

So I have set that in "Condition and Configuration" on the send custom email :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.ApplicationUser

CommentManager commentMgr = ComponentAccessor.getCommentManager()
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().loggedInUser

def lastComment = commentMgr.getComments(issue)?commentMgr.getComments(issue).last().body: ""

 

And that in the email template :

 Dear $issue.reporter.displayName,

Your request has been refused for the following reason :
<% if (lastComment)
out << lastComment
%>

Don't hesitate to contact us for more explanation.

Best regards,

But that sends the first comment posted and not the last one...

I will try to make something using your function if only one comment and the mostRecentComment otherwise.

@Ravi Sagar -Adaptavist-  Thanks to your answer combine with what I had before, I made it work !!

I made it this way :

For "Condition and Configuration" on the send custom email :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.ApplicationUser

CommentManager commentMgr = ComponentAccessor.getCommentManager()

def lastComment = commentMgr.getComments(issue)?commentMgr.getComments(issue).last().body: ""

 

and for email template :

Dear $issue.reporter.displayName,

Your request has been refused for the following reason :
<% if (mostRecentComment)
out << mostRecentComment
else
out << lastComment
%>
Don't hesitate to contact us for more explanation.

Best regards,

 

Thank you

Actually it's not working...

It's working on the preview mode but when published, still sending "null" when there is only one comment...

Ok, you can try to setup logging to know more about why it is not working. You will get details where exactly it is failing.

Oh, you don't need to add the code in the Condition and Configuration. 

hmmm... Where should I put the first part then? 

Suggest an answer

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

The add-in you’ve been waiting for: Jira Cloud for Excel 🙌

Introducing Jira Cloud for Excel Here at the product integrations team at Atlassian, we are thrilled to announce the new Jira Cloud for Excel add-in! This add-in lets you export Jira data directly ...

932 views 8 25
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