Scripted Field to copy latest comment

Hey all,

I need to create a scripted field [free text] that will copy the last comment made to a ticket whenever comments are made.

Any ideas?

Thanks in advanced :)

3 answers

1 accepted

Added.body and got what I needed.

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()

def comments = commentManager.getComments(issue)

if (comments) {

comments.last().body

}

thx for the code it is working but now I am trying to reverse it so the comments section can copy the custom field... sorry for the question but i am a newbie thx

@Kathryn Allison, @Henning Tietgens (or anyone smile) – do you know if there's a way to show this new scripted field in wiki markup?  Some of our users have been using wiki markup within their comments and this new field is awesome as it allows us not have open each ticket to view the comments.  However it's displaying the raw text, not sure if that can be configurable/scripted or not as my searches aren't coming up with much.  TIA!

@Carol Jones2 

I just managed to get this working based on frankensteining various other scripts here and there. Since this discussion is still on the first page of google results for searches on this topic, I'll add a comment here. Have a look at the current state of my scripted field:

import com.atlassian.jira.component.ComponentAccessor

def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def commentManager = ComponentAccessor.getCommentManager()
def rendererManager = ComponentAccessor.getRendererManager()
def comments = commentManager.getCommentsForUser(issue, user)

if (comments) {
rendererManager.getRenderedContent("atlassian-wiki-renderer", comments.last().body, issue.issueRenderContext)
}

Changing the returned value to one that uses the renderer manager gave me formatted results. It works just fine if you set the field type to "multi-line text", but you can set it to "HTML" if you want it to be more literally correct. The result is the same for both in JIRA 7.4.

Note, if you copy and use my version of the script, it also contains another modification to restrict which comment is returned based on what each user is permitted to see. Kind of defeats the point of comment visibility levels if this field circumvents them, after all. XD

Thanks! Great Script.

@Joshua DeClerck - Thanks for the code - it works GREAT! How would I add the commenter's name and date to the script?  Thanks again!

@Holan Kerrick - Oh man, I am not a developer and as a result, this is not guaranteed to be good quality code, but ok.

First, there are two sets of comment author/date info you can get: (1) creator/created, and (2) updater/updated. If a comment hasn't been modified, (2) will return the same values as (1). I'm opting to use (2) because of the extra versatility. I'm also opting to use the function to retrieve the author's "full name", rather than username.

It's possible to include logic to determine whether the comment has been updated or not, and print the name/date info differently depending on that, but I won't be doing that this time.

Using the same as my earlier example, I changed the if block this way:

if (comments) {
def commentAuthor = comments.last().getUpdateAuthorFullName() // alt = getAuthorFullName()
def commentDate = comments.last().getUpdated() // alt = getCreated()
def commentBody = rendererManager.getRenderedContent("atlassian-wiki-renderer", comments.last().body, issue.issueRenderContext)

return commentBody + "<p><span style=\"font-size:smaller;color:#a9a9a9;font-style:italic\">" + commentDate.format('YYYY-MM-dd') + " | " + commentAuthor + "</span></p>"
}

[Edited for a slightly nicer looking, less English-y outcome, but date only]
[More editing because I used generated HTML and even as a noob I could see it was ugly, oops - maybe a little better now?]

I dumped the original output into a commentBody variable, and made two more variables for the author and date. You can replace getUpdateAuthorFullName() and getUpdated() with other functions as documented in Atlassian's developer docs here.

The last line with the return command is for your template. It works with regular HTML, so add formatting to the text however you like. My example just inserts a simple line break after the body and concats the date and author.

I'm pretty sure my example is riddled with bad habits. I'm not sure if defining variables like that inside an if block is good code, or if using return like that is good code. I'm positive my template is bad code (not internationalized). And the date is just an ugly datetime string with no formatting. But I think this can get you started. I hope so, at least!

Hi All,

 

I am getting below error while I am trying to capture last comments into different custom field.

 

can someone please help me on fixing below issue.

 

Error1.png

Henning Tietgens Community Champion Nov 01, 2017

Hi, it seems like you're writing a listener. The variable "issue" is not directly available, you have to use "event.issue" instead.

Henning

0 vote
Henning Tietgens Community Champion Jan 07, 2014

Use getComments() from CommentManager to get a list of all comments and then identify the last comment.

I tried using those 3 exact pieces, but couldn't get the script just right.

I'm not really groovy savvy, so my attempt may have just been a poor one.

0 vote

Hi Guys,

 

With the codes you have written, were any third party plug-ins used at all?

 

Thanks

Henning Tietgens Community Champion Jan 04, 2018

Hi,

yes, you need the ScriptRunner plugin.

Henning

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,314 views 14 20
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot