Send Custom Email with last comment from specific user

Hello, 

Im having big troubles trying to get the last comment from a specific user to be sent with the custom email. 

Here is the code i currently have:

 

<p><% if (mostRecentComment) 
out << "<p>------Response Message------</p>" << "\n" << "\n" << mostRecentComment << "</p>" 
else if (issue.description)
out << "<p>------Original Message------</p>" << "\n" << "\n" << "<p>" << issue.description << "</p>"
else null %>

 

 

The issue with the above is that it will send the last comment in the ticket, how can i modify this code to send "lastComment" from a specific user?

i tried to use this following code: 

<% out << comments.authorKey[-1] == "customer.care" %> 

But its pulling currentuser name.

Many thanks! and i really appreciate your help. 

 

Cheers, 

Pon

2 answers

1 accepted

Hi Pon,

For a JIRA v6.* and SR v3.* try to use something like the script below in your email template

&lt;% 
def userKey = "admin"
def commentManager = com.atlassian.jira.component.ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)
def lastCommentString = "No comments made from user ${userKey}"
def commentsFromUser = comments?.findAll {it.authorApplicationUser.key == userKey}

if (commentsFromUser) {
    def lastCommentFromUser = commentsFromUser.last()
    lastCommentString = "Author: ${lastCommentFromUser.authorFullName}&lt;br&gt;, Body: ${lastCommentFromUser.body} Created at: ${lastCommentFromUser.created} &lt;br&gt;&lt;br&gt;"
}
%&gt;
    
Last comment from user - ${userKey}: ${lastCommentString}

Please let me know if this does the trick

Hi @Thanos Batagiannis [Adaptavist]

Thanks for your quick reply, and i will do tag correctly next time. 

Here is the following code i implemented, it works fine when previewing, but when i put it into operation within post function, i receive an error:

"Property 'cfValues['Email Address']' not found", when i remove this, it will then force another error "'lastCommentFromUser' not found"

 

&lt;% 
def userKey = "customer.care"
def commentManager = com.atlassian.jira.component.ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)
def lastCommentString = "No comments made from user ${userKey}"
def lastCommentFromUser = comments?.findAll {it.authorApplicationUser.key == userKey}
if (lastCommentFromUser) {
	lastCommentFromUser = lastCommentFromUser.last()
	if (lastCommentFromUser) {
    	lastCommentString = "From: ${cfValues['Email Address']}\nSent:${lastCommentFromUser.created}\nTo: test@testenviroment.com.au\nSubject: ${cfValues['Email Subject']}\n\n${lastCommentFromUser.body}"
	} 	
} 
%&gt;
    
${lastCommentString}

Also more information about JIra, im currently using JIRA  6.3.3 and scriptrunner version 3.0.5

 

Many many thanks for your help. 

Pon

@Thanos Batagiannis [Adaptavist]

 

I GOT IT!

Changed HTML to plain text (will convert to HTML later)

See following code: 

 

&lt;% 
def userKey = "customer.care"
def commentManager = com.atlassian.jira.component.ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)
def lastCommentFromUser = comments?.findAll {it.authorApplicationUser.key == userKey}
def issueDesc = issue.description
if (lastCommentFromUser) 
	lastCommentFromUser = out &lt;&lt; lastCommentFromUser.last().body      
	else if (issueDesc)
        lastCommentFromUser = out &lt;&lt; issueDesc
		else lastCommentFromUser = out &lt;&lt; "" 
%&gt;

 

Thanks Thanos smile

I succeeded in scripting the condition parameters:

&lt;% if (comments.authorKey[-1] == "customer.care")
    out &lt;&lt; comments.authorKey.getBody() == "customer.care" 
	else null %&gt;

But still stuck in getting the body of the comment, help please smile

 

many thanks, 

Pon

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

Mission-critical battery manufacturer fulfills FAA software requirements with Commit Policy Plugin

EaglePicher Technologies is a leading manufacturer of battery systems for diverse industries like defense, aviation, space or medical. As they operate in highly regulated industries, keeping a clear ...

150 views 0 2
Read article

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