Copy comment value from specific user to customfield

Hello! 

 

Im currently doing a project to replace an email inbox using the scriptrunner send custom email function.

i require to copy a comment value from a specific user e.g customer.care to a customfield where i will use this customfield value to email out as a thread with an email body.

 

any suggestions on how to do this? 

 

Many thanks, 

Pon

1 answer

Hi Pon,

Is there a specific reason you want to copy it in a custom field first ? I mean you can extract the comments of a specific user for an issue at the moment you will call the listener/post function.

So an example (and for the latest SR version) of a listener that sends email with body all the comments of a specific user for an issue would be. 

Condition and Configuration field

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
 
// in a post function would be just - issue
def commentManager = ComponentAccessor.getCommentManager()
 
//this will be a List of comments made by user with key 'admin'
def commentsFromAdmin = commentManager.getComments(issue).findAll {it.getAuthorApplicationUser().key == "admin"}

def commentsString = "No comments made from user"

if (commentsFromAdmin) {
def mostRecentCommentFromAdmin = commentsFromAdmin.last()
commentsString = "Comment body: ${mostRecentCommentFromAdmin.getBody()} <br>" +
    "By ${mostRecentCommentFromAdmin.authorFullName} <br>" +
    "Created at ${mostRecentCommentFromAdmin.created} <br>"
}
config.put ("commentsFromUser" , commentsString)    

return true

Email Body

${commentsFromUser}

please let me know if you need further assistance. 

regards, Thanos

Hi Thanos, 

 

Thank you for your reply!

 

Sorry im abit new to this, so am i suppose to put the above code in postfunction before i call to send custom email and place "${commentsFromUser}" in?

I currently have the following code in postfunction send customer email which works fine, but i need it to send from a specific user called "customer.care": 


<% out << cfValues['Email Body - Full'] %>

This communication may contain privileged or other confidential information.  If you are not the intended recipient, or believe that you have received this communication in error, please do not print, copy, retransmit, disseminate, or otherwise use the information.  Also, please indicate to the sender that you have received this email in error, and delete the copy you received.
<% if (mostRecentComment != null)    
   out << "-----Response Message-----" << "\n" << "\n" << mostRecentComment
 %>


Any help would be much appreciated. 


Cheers, 

Pon

Hi Pon, 

Updated the script above, in order to return to you the last comment of a specific user. So if it is for a post function then your send custom email configuration will look like 

send-last-comment.png

PS. This is for a JIRA v7.*  

regards, Thanos

Hi Thanos, 

 

Thank you so much for your reply. 

 

Not sure if i have a older version but i assume it work, i put in the following code into post function and received the following error: "Property "

 

Here is the code i put in: 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
 
// in a post function would be just - issue
Issue issue
def commentManager = ComponentAccessor.getCommentManager()
def auser = ComponentAccessor.getUserManager().getUserByKey("customer.care")
def commentsFromAdmin = commentManager.getComments(issue).findAll {it.getAuthorApplicationUser().key == "customer.care"}
def mostRecentCommentFromAdmin = commentsFromAdmin?.last()
 
def commentsString = "Comment body: ${mostRecentCommentFromAdmin.getBody()} <br>" +
    "By ${mostRecentCommentFromAdmin.authorFullName} <br>" +
    "Created at ${mostRecentCommentFromAdmin.created} <br>"
 
config.put ("commentsFromUser" , commentsString)
return true

 

send.JPG

I just have a question about the getUserByKey is this an email address or the string name of comment user.

 

Many many thanks for your help. 

 

Regards,

Pon

 

 

 

hmm you have an older version... Could you please let me know which , so I can adjust the script ?

Hi Thanos, 

 

Appreciate your prompt reply. 

 

I have scriptrunner version 3.0.5.

 

Many thanks, 

Pon

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

24,675 views 2 7
Join discussion

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