Display last comment in post function email

m
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.
April 6, 2014

I want to display the last comment made on an issue in a custom email template.

I have the following:

<b>Last Comment:<br>
<td><% out << componentManager.commentManager.getComments(issue)?.last()?.body %>

which happily shows the last comment, but fails for issue where there are no comments.

I'm sure there is a way to add an 'If' statement to this to handle the error but can't seem to get it to work... any help greatfully appreciated.

3 answers

1 accepted

1 vote
Answer accepted
m
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.
April 6, 2014

found the answer...

<%if (componentManager.commentManager.getComments(issue)?.size() > 0)

{

out << componentManager.commentManager.getComments(issue)?.last()?.body

}

else

{

out << ""

}%>

1 vote
JamieA
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.
April 7, 2014

Matt - your way is fine, but the last comment is placed in the binding, so you can just do as in the example in the docs: https://jamieechlin.atlassian.net/wiki/display/GRV/Built-In+Scripts#Built-InScripts-Sendacustomemail

&lt;% if (lastComment)
    out &lt;&lt; "Last comment: " &lt;&lt; lastComment
%&gt;

Joseph Boulos September 8, 2014

Works perfect. Any way that I can also print out *who* posted the comment? I couldn't find documentation on it, so started guessing things like lastComment?.Author but am getting nowhere.

JamieA
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.
September 9, 2014

lastComment is just the comment body. comments.last().authorKey should give you the username, but make sure there are actually comments, otherwise that will throw an null pointer ex.

0 votes
Tsol
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.
April 6, 2014

Try the following.

&lt;% 
    if (componentManager.getCommentManager().getComments(issue)) { 
        &lt;% out &lt;&lt; componentManager.commentManager.getComments(issue)?.last()?.body %&gt;
    } 
    else {
        out &lt;&lt; "no comment"
    }
%&gt;

Hope it works

m
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.
April 6, 2014

This gives the following error..

groovy.lang.GroovyRuntimeException: Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed: GStringTemplateScript95.groovy: 36: unexpected token: < @ line 36, column 9. <% out << componentManager.commentManager.getComments(issue)?.last()?.body ; ^ 1 error

m
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.
April 6, 2014

thanks for the help..

Robert Mota December 22, 2016

perhaps there are <% and  %>  to remove from the if statement

Suggest an answer

Log in or Sign up to answer