Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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

Arthur SALMON
Contributor
October 18, 2019

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

1 vote
Answer accepted
Arthur SALMON
Contributor
October 21, 2019

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
Arthur SALMON
Contributor
October 18, 2019

@Ravi Sagar _Sparxsys_  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

Arthur SALMON
Contributor
October 18, 2019

Actually it's not working...

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

Ravi Sagar _Sparxsys_
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.
October 18, 2019

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.

Ravi Sagar _Sparxsys_
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.
October 18, 2019

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

Arthur SALMON
Contributor
October 18, 2019

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

0 votes
Ravi Sagar _Sparxsys_
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.
October 18, 2019

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

Arthur SALMON
Contributor
October 18, 2019

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.

Suggest an answer

Log in or Sign up to answer